can u explain me why in this vector:
if i want v[0] to be empty i have to doCode:vector<string> v;
string s = "abc";
v.push_back(s);
and not only one of them?Code:v.clear();
v[0].~string();
Printable View
can u explain me why in this vector:
if i want v[0] to be empty i have to doCode:vector<string> v;
string s = "abc";
v.push_back(s);
and not only one of them?Code:v.clear();
v[0].~string();
What do you mean? Why isn't this doing what you want:
Code:string s = "abc";
vector<string> v;
v.push_back(s);
cout<<v[0]<<endl;
v.clear();
cout<<v[0]<<"<-----"<<endl;
cout<<v.size()<<endl;
have u tried compiling your code? here's the output:
and that's my point, i want v[0] to be empty after clearing itCode:abc
abc<-----
0
Yes, I compile all my code before posting it. :)Quote:
Originally Posted by hiya
Here is the output with MS VC++6:
What compiler are you using? Can you access an element at index position 1 after calling clear(). You may also want to try:Code:abc
<-----
0
Press any key to continue
v.erase(v.begin(), v.end());
however the results should be the same.
im using dev-c++.
Yep...Quote:
however the results should be the same.
it's strange that your compiler removing it and mine don't.
What about:
e.g.:Quote:
Can you access an element at index position 1 after calling clear()
I also wonder what is at v[0] after clear(): "abc" or "xyz"?Code:string s = "abc";
string s2 ="xyz";
vector<string> v;
v.push_back(s);
v.push_back(s2);
cout<<v[1]<<endl;
v.clear();
cout<<v[1]<<"<-----"<<endl;
after clear:
v[1] = xyz
v[0] = abc
it seems that the only thing clear() does is changing the size to 0
I can explain. Your code is completely invalid and therefore completely unpredictable.Quote:
Originally Posted by hiya
v.clear();
This line empties the vector. After this, v[0] doesn't exist anymore. Any access to it is invalid and yields undefined results - that it happens to yield exactly what was there previously is just one interpretation of "undefined". It might also crash your app. (And will do so on VC++7.1.)
v[0].~string();
This line destructs the string at v[0]. After this, the string object mustn't be used anymore, and that includes access.
What you want is
v[0].clear();
You should post the exact code you are using to get those results, and then people can comment on why that is happening.
Does the same thing happen with integers?
CornedBee,
v[0].clear(); will remove the string v[0] contains, but v[0] will remain undeleted.
what i can do is
but what happens when the vector is for ints? i can't use clear() thenCode:for(int i=0; i<v.size(); i++)
v[i].clear();
v.clear();
Maybe...
Code:v.erase(v.begin());
is the same as v.clear(). What you need to do is use v.erase(v.begin());Code:for(int i=0; i<v.size(); i++)
v[i].clear();
v.clear();
To read more about this I recommend http://www.cppreference.com
Huh?
Just what do you want, exactly?
Shakti, that's what i thought but when i use
v[0] is empty, and when i useCode:for(int i=0; i<v.size(); i++)
v[i].clear();
v.clear();
v[0] is "abc" (or any value i give him in the beginning)Code:v.erase(v.begin());
Using [] on a vector to get access to a specific element does not perform any checking to make sure you are pointing to a valid element. Try using the at member function to access that first element after erasing/clearing it and you should get a runtime-error or "abnormal porgram termination" error when you run the program. It's up to you to not attempt to access elements of a vector that do not exist.