我創建了一個函數來運行一個字符串矢量並刪除任何長度爲3或更小的字符串。這是使用STL算法庫的教訓。執行remove_if()後擦除()
我遇到了麻煩的功能工作,但不僅它刪除長度爲3或更少的字符串,但它也將字符串「矢量」追加到最後。
輸出應該
This test vector
,而是它是
This test vector vector"
我怎樣才能解決呢?
/*
* using remove_if and custom call back function, write RemoveShortWords
* that accepts a vector<string> and removes all strings of length 3 or
* less from it. *shoot for 2 lines of code in functions.
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
bool StringLengthTest(string test) //test condition for remove_if algo.
{
return test.length() <= 3;
}
void RemoveShortWords(vector<string> &myVector)
{
//erase anything in vector with length <= 3
myVector.erase(remove_if(myVector.begin(),
myVector.end(),
StringLengthTest));
}
int main()
{
//add some strings to vector
vector<string> myVector;
myVector.push_back("This");
myVector.push_back("is");
myVector.push_back("a");
myVector.push_back("test");
myVector.push_back("vector");
//print out contents of myVector (debugging)
copy(myVector.begin(), myVector.end(), ostream_iterator<string>(cout," "));
cout << endl; //flush the stream
RemoveShortWords(myVector); //remove words with length <= 3
//print out myVector (debugging)
copy(myVector.begin(), myVector.end(), ostream_iterator<string>(cout," "));
cout << endl;
system("pause");
return 0;
}
偉大的細節。非常感謝你澄清發生了什麼! – MCP 2012-01-29 14:50:38
如果'myVector'是空的,這會更糟。然後'iter'將等於'myVector.end()',並且使用'erase(iter)'擦除將導致UB。 – Ruslan 2015-08-07 08:48:48