Using CTime & asctime to assign time to a string or vector string
I would like to use asctime to assign the time to a string.
time_t rawtime;
time ( &rawtime );
vector<string> TTime;
TTime.resize(10);
TTime = asctime(localtime ( &rawtime ));
I understand asctime is returning a pointer to a string. Would i have to create m开发者_开发知识库y own string and assign it the return value of asctime, or is there a simpler way?
You can construct a string directly from a char *
:
string str = asctime(localtime ( &rawtime ));
This doesn't make sense:
TTime = asctime(localtime ( &rawtime ));
You can't assign a single string to a vector of strings. What you can do is:
TTime[0] = asctime(localtime ( &rawtime ));
Looks like what you need is a simple string,
std::string TTime(asctime(localtime(&rawtime)));
The function asctime() return char* and the std::string can construct from char*
std::string time(asctime(localtime(&rawtime)));
or
std::string time; time = asctime(asctimer(localtimer(&rawtime)));
精彩评论