I'm trying to understand how remove_if
works (the <<
is overloaded) and for that I want to remove_if all the strings which begin with 'C':
vector<string> langs = { "Python", "C++", "C", "Java", "C#" };
cout << "Initial vector: " << langs << " | Size = " << langs.size() << endl;
Output: Initial vector: Python C++ C Java C# | Size = 5
Then I write:
auto it = remove_if(begin(langs), end(langs), [](const string& s)
{ return s[0]== 'C';});
cout << "After remove_if: " << langs << " | Size = " << langs.size() << endl;
Output 2: After remove_if: Python Java C C# | Size = 5
What I understand: elements that should remain are removed to the beginning and the *it
now equals "C"
Question: What happens to "C++"
? If "Java"
replaced it, why
for(auto& _it = it; _it != end(langs); _it++)
cout << *_it << " ";
gives Output 3: C C#
and not "C", "Java", "C#"
?