C++ 使用CTime&;asctime为字符串或向量字符串指定时间
我想使用asctime将时间分配给字符串C++ 使用CTime&;asctime为字符串或向量字符串指定时间,c++,string,ctime,C++,String,Ctime,我想使用asctime将时间分配给字符串 time_t rawtime; time ( &rawtime ); vector<string> TTime; TTime.resize(10); TTime = asctime(localtime ( &rawtime )); time\u t rawtime; 时间(&rawtime); 向量时间; t调整时间(10); TTime=asctime(localtime(&rawtime)); 我知道asctime正在
time_t rawtime;
time ( &rawtime );
vector<string> TTime;
TTime.resize(10);
TTime = asctime(localtime ( &rawtime ));
time\u t rawtime;
时间(&rawtime);
向量时间;
t调整时间(10);
TTime=asctime(localtime(&rawtime));
我知道asctime正在返回一个指向字符串的指针。我需要创建自己的字符串并将其指定为asctime的返回值,还是有更简单的方法?看起来您需要的是一个简单的字符串
std::string TTime(asctime(localtime(&rawtime)));
您可以直接从
字符*
构造字符串:
string str = asctime(localtime ( &rawtime ));
这没有道理:
TTime = asctime(localtime ( &rawtime ));
不能将单个字符串指定给字符串向量。您可以做的是:
函数asctime()返回char*
std::string可以从char构造*
std::字符串时间(asctime(localtime(&rawtime))
或
std::字符串时间;
时间=asctime(asctimer(localtimer(&rawtime)) 这确实奏效了。我不知道为什么,但我假设每个字符串只能容纳一个单词&字符串末尾有一个空格。
TTime[0] = asctime(localtime ( &rawtime ));