Hello,
suppose I have a string temp that I want to push in a vector vec1, using:
[code=cpp]
vec1.pushback(t emp);
[/code]
I noticed that if I change the content of temp, it will change the content of vec1[lastItem], unless I push in another value after temp. Another variant to get vec1 immunized against changes in temp was to use
[code=cpp]
vec1.pushback(t emp.c_str());
[/code]
vec1 is declared like vector<string> vec1.
pushback() awaits for a variable of type "const string&", so should I understand that the last element in the vector always points to another variable, and that only when a new value is pushed that the last value gets copied to a new location? What about the glitch with c_str()?
Borland's 5 help thinness kills me...
Your thoughts on this would be appreciated,
Ras.
suppose I have a string temp that I want to push in a vector vec1, using:
[code=cpp]
vec1.pushback(t emp);
[/code]
I noticed that if I change the content of temp, it will change the content of vec1[lastItem], unless I push in another value after temp. Another variant to get vec1 immunized against changes in temp was to use
[code=cpp]
vec1.pushback(t emp.c_str());
[/code]
vec1 is declared like vector<string> vec1.
pushback() awaits for a variable of type "const string&", so should I understand that the last element in the vector always points to another variable, and that only when a new value is pushed that the last value gets copied to a new location? What about the glitch with c_str()?
Borland's 5 help thinness kills me...
Your thoughts on this would be appreciated,
Ras.
Comment