gpt4 book ai didi

c++ - 交换容器中两个迭代器的位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:12:56 26 4
gpt4 key购买 nike

我想交换两个迭代器 firstsecond 在容器中的位置,并在运行后保持它们有效。此外,如果我有迭代器 other 指向与 first 相同的值,我也想保持它有效(保持它指向相同的值)。是否可以使用任何 STL 容器实现?

最佳答案

你试过交换它们吗?

#include <algorithm>

// ...

swap(first, second);

(在 C++0x 中,#include 显着较小的 header <utility> 就足够了。)

Moreover if I have iterator other pointing at the same value as first 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com