Hey everyone, I'm currently trying to self teach myself most of the STL algorithms and have come across a weird error - or if it isn't an error then I'm VERY confused! It involves the algorithms unique and remove using MSVC++, which do as they advertise BUT when they take out numbers in a container, they don't shorten the container length! Like so:
Here is the output:Code:#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void displayVec(vector<int> vec)
{
for (int x=0; x<vec.size(); x++)
cout<<vec[x]<<" ";
cout<<endl;
}
void initializeVec(vector<int>& vec)
{
while(vec.size())
vec.pop_back();
vec.push_back(3);
vec.push_back(7);
vec.push_back(3);
vec.push_back(3);
vec.push_back(9);
vec.push_back(4);
vec.push_back(1);
vec.push_back(2);
}
int main()
{
vector<int> vec;
initializeVec(vec);
cout<<"Before unique: ";
displayVec(vec);
unique(vec.begin(), vec.end());
cout<<"After unique: ";
displayVec(vec);
initializeVec(vec);
cout<<"Before remove: ";
displayVec(vec);
remove(vec.begin(), vec.end(), 3);
cout<<"After remove: ";
displayVec(vec);
return 0;
}
It removed the numbers as advertised, and shifted the later numbers to the left also as advertised, but it didn't delete the end or shorten the vector size. Is this supposed to happen? And if so, then of what use are these two algorithms?Code:Before unique: 3 7 3 3 9 4 1 2
After unique: 3 7 3 9 4 1 2 2
Before remove: 3 7 3 3 9 4 1 2
After remove: 7 9 4 1 2 4 1 2
Press any key to continue