Sorting out of a vector of strings
-
i have got a vector of strings. i would like to sort out all the empty strings.. any neater way to do this than this
struct _IsEmpty { operator () (const std::string& s) { return s.empty(); } }; void TransformAbtVStr (std::vector& v_str) { v_str.erase (std::remove_if (v_str.begin(), v_str.end(), _IsEmpty()), v_str.end()); }
Thanks in advance, Bernhard
"Just looking for loopholes." W. C. Fields
American actor, 1880-1946, explaining why he was reading the Bible on his deathbed. -
i have got a vector of strings. i would like to sort out all the empty strings.. any neater way to do this than this
struct _IsEmpty { operator () (const std::string& s) { return s.empty(); } }; void TransformAbtVStr (std::vector& v_str) { v_str.erase (std::remove_if (v_str.begin(), v_str.end(), _IsEmpty()), v_str.end()); }
Thanks in advance, Bernhard
"Just looking for loopholes." W. C. Fields
American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.vector vecStr; vector::iterator iterB, iterE; // put stuff in your vector of strings iterB = vecStr.begin(); iterE = vecStr.end(); sort(iterB, iterE); vecStr.erase(unique(iterB, iterE), iterE); vecStr.resize(vecStr.size()); ;) William Fortes in fide et opere!
-
vector vecStr; vector::iterator iterB, iterE; // put stuff in your vector of strings iterB = vecStr.begin(); iterE = vecStr.end(); sort(iterB, iterE); vecStr.erase(unique(iterB, iterE), iterE); vecStr.resize(vecStr.size()); ;) William Fortes in fide et opere!
I'm curious, what is the reason to call the resize method at the end? David
-
I'm curious, what is the reason to call the resize method at the end? David
Because after you've done the erasing of entries, the size of the vector is no longer what it used to be. Therefore, in order to get it to the new size it has become, you'll need to resize it. And that new size is what "size()" is going to give. Therefore you'll be resizing it to the new size of "size()". ;) William Fortes in fide et opere!
-
Because after you've done the erasing of entries, the size of the vector is no longer what it used to be. Therefore, in order to get it to the new size it has become, you'll need to resize it. And that new size is what "size()" is going to give. Therefore you'll be resizing it to the new size of "size()". ;) William Fortes in fide et opere!
If that is the case for you, then you have a faulty STL implementation. Erase on sequences (and I am prepared to bet money that this is the case for other containter types too), destroys the elements properly and then removes them from the container. The docs says (SGI STL docs): Destroys the elements in the range [p,q) and removes them from a. and: a.size() is decremented by the distance from i to j. The relative order of the other elements in the sequence is unchanged. -- Ich bin Joachim von Hassel, und ich bin Pilot der Bundeswehr. Welle: Erdball - F104-G Starfighter
-
If that is the case for you, then you have a faulty STL implementation. Erase on sequences (and I am prepared to bet money that this is the case for other containter types too), destroys the elements properly and then removes them from the container. The docs says (SGI STL docs): Destroys the elements in the range [p,q) and removes them from a. and: a.size() is decremented by the distance from i to j. The relative order of the other elements in the sequence is unchanged. -- Ich bin Joachim von Hassel, und ich bin Pilot der Bundeswehr. Welle: Erdball - F104-G Starfighter
-
Sometimes what the documentation says, and what is implemented, are not always the same. You do whatever works. William Fortes in fide et opere!
So you are saying that if you log the size before and after the resize() you can provide the proof that they are not equal? Please do.
cout << vecStr.size() << endl;
vecStr.resize(vecStr.size());
cout << vecStr.size() << endl;"No matter where you go, there your are." - Buckaroo Banzai
-pete