Domanda

Vorrei utilizzare asctime per assegnare il tempo di una stringa.

time_t rawtime;
time ( &rawtime );
vector<string> TTime;
TTime.resize(10);
TTime = asctime(localtime ( &rawtime ));

capisco asctime sta tornando un puntatore a una stringa. Ho voluto creare il mio stringa e assegnare il valore di ritorno di asctime, o c'è un modo più semplice?

È stato utile?

Soluzione

È possibile costruire una stringa direttamente da un char *:

string str = asctime(localtime ( &rawtime ));

Questo non ha senso:

TTime = asctime(localtime ( &rawtime ));

Non è possibile assegnare una singola stringa di un vettore di stringhe. Quello che possono fare è:

TTime[0] = asctime(localtime ( &rawtime ));

Altri suggerimenti

appare come quello che vi serve è una semplice stringa,

std::string TTime(asctime(localtime(&rawtime)));

La funzione asctime () ritorno char * e la std :: string può costruire da char *

std :: string time (asctime (localtime (& rawtime)));

o

std :: string time; tempo = asctime (asctimer (localtimer (& rawtime)));

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top