执行remove_if()之后的擦除() [英] erase() after performing remove_if()

查看:158
本文介绍了执行remove_if()之后的擦除()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数来遍历字符串向量,并删除所有长度为3或更短的字符串.这是使用STL算法库的一课.

I've created a function to run through a vector of strings and remove any strings of length 3 or less. This is a lesson in using the STL Algorithm library.

我在函数起作用方面遇到麻烦,但它不仅会删除长度为3或更短的字符串,而且还会将字符串"vector"附加到末尾.

I'm having trouble in that the functions work but not only does it delete strings of length 3 or less but it also appends the string "vector" to the end.

输出应为

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;
}

推荐答案

如果分开以下语句,最容易理解这一点:

It is easiest to understand this if you seperate the statements:

auto iter(remove_if(myVector.begin(), myVector.end(), StringLengthTest));
myVector.erase(iter);

这2行与您的单行相同.现在应该清楚什么是错误". remove_if,首先工作.它遍历整个向量,并将所有选定"的条目移到末尾"(更好的说:它将未选定的条目移到最前面).运行后,它将迭代器返回到剩余条目的最后"位置,例如:

These 2 lines do the same as your single line. And it should be clear now what the "bug" is. remove_if, works first. It iterates over the whole vector and moves all "selected" entries "to the end" (better said: it moves the non selected entries to the front). After it has run it returns an iterator to the "last" position of the left over entries, something like:


测试
向量
在此处测试<-迭代器点
向量

this
test
vector
test <- iterator points here
vector

然后使用单个迭代器运行擦除".这意味着您删除了一个指向的元素-因此,您删除了测试"元素. -剩下的就是您所看到的.

Then you run erase with a single iterator. That means you erase a single element pointed at - so you erase the "test" element. - What is left over is what you are seeing.

要解决此问题,只需从remove_if返回到end()的向量中擦除即可.:

To fix it simply erase from the vector returned by remove_if to the end().:

myVector.erase(remove_if(myVector.begin(), myVector.end(), StringLengthTest), myVector.end()); //erase anything in vector with length <= 3

这篇关于执行remove_if()之后的擦除()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆