也许我遗漏了一些非常明显的东西,但我不明白为什么要使用 back_inserter/front_inserter/inserter, 而不仅仅是从容器接口(interface)提供适当的迭代器。 这就是我的问题。
最佳答案
因为容器的“普通”迭代器不能(或至少不能)调用 push_back、push_front 和 insert。
例子:
int main() {
using namespace std;
vector<int> a (3, 42), b;
copy(a.begin(), a.end(), back_inserter(b));
copy(b.rbegin(), b.rend(), ostream_iterator<int>(cout, ", "));
return 0;
}
关于c++ - STL 插入迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2223835/