Does vector.erase
resize the vector object so that I can measure the reduced size with vector.size()
?
for example;
vector<int> v(5);
v = {1,2,3,4,5};
and I want to delete 4 by;
v.erase(v.begin()+4);
Does my vector object v
have a size of 4 now. In other words is v.size() == 4
after this operation?
Yes, size is decreased as you erase elements.
Don't be afraid to test yourself though, by writing a minimal example, like this :) :
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v(5);
v = {1,2,3,4,5};
cout << v.size() << endl;
v.erase(v.begin()+4);
cout << v.size() << endl;
return 0;
}
you would get:
gsamaras@gsamaras-A15:~$ g++ -Wall -std=c++0x main.cpp
gsamaras@gsamaras-A15:~$ ./a.out
5
4
And we would expect that right? I mean the ref says:
Return size
Returns the number of elements in the vector.
This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With