我创建了一个函数来运行一个字符串矢量并删除任何长度为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