stl插入迭代器

发布于 2024-08-20 15:32:38 字数 102 浏览 3 评论 0原文

也许我错过了一些完全明显的东西,但我不明白为什么要使用 back_inserter/front_inserter/inserter, 而不仅仅是从容器接口提供适当的迭代器。 这就是我的问题。

Maybe I am missing something completely obvious, but I can't figure out why one would use back_inserter/front_inserter/inserter,
instead of just providing the appropriate iterator from the container interface.
And thats my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花ソ最美的依靠 2024-08-27 15:32:38

因为那些调用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;
}

Because those call push_back, push_front, and insert which the container's "normal" iterators can't (or, at least, don't).

Example:

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;
}
抱猫软卧 2024-08-27 15:32:38

主要原因是常规迭代器会迭代容器中的现有元素,而 *inserter 系列迭代器实际上会在容器中插入新元素。

std::vector<int> v(3);  // { 0, 0, 0 }
int array[] = { 1, 2, 3 };
std::copy( array, array+3, std::back_inserter(v) ); // adds 3 elements
   // v = { 0, 0, 0, 1, 2, 3 }
std::copy( array, array+3, v.begin() ); // overwrites 3 elements
   // v = { 1, 2, 3, 1, 2, 3 } 
int array2[] = { 4, 5, 6 };
std::copy( array2, array2+3, std::inserter(v, v.begin()) );
   // v = { 4, 5, 6, 1, 2, 3, 1, 2, 3 }

The main reason is that regular iterators iterate over existing elements in the container, while the *inserter family of iterators actually inserts new elements in the container.

std::vector<int> v(3);  // { 0, 0, 0 }
int array[] = { 1, 2, 3 };
std::copy( array, array+3, std::back_inserter(v) ); // adds 3 elements
   // v = { 0, 0, 0, 1, 2, 3 }
std::copy( array, array+3, v.begin() ); // overwrites 3 elements
   // v = { 1, 2, 3, 1, 2, 3 } 
int array2[] = { 4, 5, 6 };
std::copy( array2, array2+3, std::inserter(v, v.begin()) );
   // v = { 4, 5, 6, 1, 2, 3, 1, 2, 3 }
自此以后,行同陌路 2024-08-27 15:32:38

迭代器指向一个元素,并且通常不知道它附加到哪个容器。 (迭代器基于指针,您无法从指针判断它与什么数据结构相关联。)

向 STL 容器添加元素会更改容器的描述。例如,STL 容器有一个 .size() 函数,该函数必须更改。由于某些元数据必须更改,因此无论插入什么新元素都必须知道它要添加到哪个容器。

The iterator points to an element, and doesn't in general know what container it's attached to. (Iterators were based on pointers, and you can't tell from a pointer what data structure it's associated with.)

Adding an element to an STL container changes the description of the container. For example, STL containers have a .size() function, which has to change. Since some metadata has to change, whatever inserts the new element has to know what container it's adding to.

痞味浪人 2024-08-27 15:32:38

这完全取决于您真正需要什么。两种迭代器都可以使用,具体取决于您的意图。

当您使用“普通”迭代器时,它不会在容器中创建新元素。它只是将数据一个接一个地写入容器的现有连续元素中。它会覆盖容器中已有的任何数据。如果允许它到达序列的末尾,则任何进一步的写入都会使其“从末尾脱落”并导致未定义的行为。即它崩溃了。

另一方面,每次通过插入器迭代器写入内容时,插入器迭代器都会创建一个新元素并将其插入到当前位置(前面、后面、中间的某个位置)。他们从不覆盖现有元素,而是添加新元素。

It is all a matter of what you really need. Both kinds of iterators can be used, depending on your intent.

When you use an "ordinary" iterator, it does not create new elements in the container. It simply writes the data into the existing consecutive elements of the container, one after another. It overwrites any data that is already in the container. And if it is allowed to reach the end of the sequence, any further writes make it "fall off the end" and cause undefined behavior. I.e. it crashes.

Inserter iterators, on the other hand, create a new element and insert it at the current position (front, back, somewhere in the middle) every time something is written through them. They never overwrite existing elements, they add new ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文