Search code examples
c++stringctime

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 my own string and assign it the return value of asctime, or is there a simpler way?


Solution

  • 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 ));