我想交换两个迭代器 first
和 second
在容器中的位置,并在运行后保持它们有效。此外,如果我有迭代器 other
指向与 first
相同的值,我也想保持它有效(保持它指向相同的值)。是否可以使用任何 STL 容器实现?
最佳答案
你试过交换它们吗?
#include <algorithm>
// ...
swap(first, second);
(在 C++0x 中,#include
显着较小的 header <utility>
就足够了。)
Moreover if I have iterator
other
pointing at the same value asfirst
I want to keep it valid too (keep it pointing at the same value).
只需重新分配 other
交换后:
swap(first, second);
other = first;
关于c++ - 交换容器中两个迭代器的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023634/