我有一个字符串源容器,我想从源容器中删除与谓词匹配的任何字符串,并将它们添加到目标容器中。
remove_copy_if
等算法只能对容器中的元素进行重新排序,因此必须跟在erase
成员函数之后。我的书 (Josuttis) 说 remove_copy_if
在目标容器中的最后一个位置之后返回一个迭代器。因此,如果我只有一个指向目标容器的迭代器,我该如何在源容器上调用 erase
?我曾尝试使用目标的大小来确定要从源容器的末端往回删除多远,但没有成功。我只提出了以下代码,但它进行了两次调用(remove_if
和 remove_copy_if
)。
有人可以告诉我正确的方法吗?我确定两个线性调用不是 这样做的方法。
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
class CPred : public unary_function<string, bool>
{
public:
CPred(const string& arString)
:mString(arString)
{
}
bool operator()(const string& arString) const
{
return (arString.find(mString) == std::string::npos);
}
private:
string mString;
};
int main()
{
vector<string> Strings;
vector<string> Container;
Strings.push_back("123");
Strings.push_back("145");
Strings.push_back("ABC");
Strings.push_back("167");
Strings.push_back("DEF");
cout << "Original list" << endl;
copy(Strings.begin(), Strings.end(),ostream_iterator<string>(cout,"\n"));
CPred Pred("1");
remove_copy_if(Strings.begin(), Strings.end(),
back_inserter(Container),
Pred);
Strings.erase(remove_if(Strings.begin(), Strings.end(),
not1(Pred)), Strings.end());
cout << "Elements beginning with 1 removed" << endl;
copy(Strings.begin(), Strings.end(),ostream_iterator<string>(cout,"\n"));
cout << "Elements beginning with 1" << endl;
copy(Container.begin(), Container.end(),ostream_iterator<string>(cout,"\n"));
return 0;
}
最佳答案
出于对 Fred 辛勤工作的尊重,让我补充一点:move_if
在抽象层面上与 remove_copy_if
没有什么不同。唯一的实现级别更改是 end()
迭代器。您仍然没有得到任何 erase()
。 接受的答案不会erase()
匹配的元素——OP 问题陈述的一部分。
至于OP的问题:你想要的是就地拼接。这对于列表是可能的。但是,对于 vectors
这将不起作用。了解迭代器何时、如何以及为何失效。您将必须采用两次通过算法。
remove_copy_if
and other algorithms can only reorder the elements in the container,
来自 SGI 关于 remove_copy_if
的文档:
This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).
因此不会发生相对重新排序。此外,这是一个拷贝,这意味着 Source
vector
中的元素正在被复制到 Container vector
中。
how can I call erase on the source container?
您需要使用不同的算法,称为 remove_if
:
remove_if
removes from the range[first, last)
every elementx
such thatpred(x)
is true. That is,remove_if
returns an iteratornew_last
such that the range[first, new_last)
contains no elements for whichpred
is true. The iterators in the range[new_last, last)
are all still dereferenceable, but the elements that they point to are unspecified.Remove_if
is stable, meaning that the relative order of elements that are not removed is unchanged.
因此,只需将 remove_copy_if
调用更改为:
vector<string>::iterator new_last = remove_if(Strings.begin(),
Strings.end(),
Pred);
一切就绪。请记住,您的 Strings vector
的范围不再由迭代器 [first(), end())
定义,而是由 [first (), new_last)
.
如果需要,您可以通过以下方式删除剩余的 [new_last, end())
:
Strings.erase(new_last, Strings.end());
现在,您的 vector
已被缩短并且您的 end()
和 new_last
相同(最后一个元素之后),所以你可以像往常一样使用:
copy(Strings.begin(), Strings.end(), ostream_iterator(cout, "\"));
在您的控制台上打印字符串(stdout
)。
关于c++ - 现在要删除与谓词匹配的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/604024/