gpt4 book ai didi

c++ - 为什么/什么时候我们应该更喜欢使用 std::swap;在 std::iter_swap(&a, &b) 上交换(a,b)?

转载 作者:太空狗 更新时间:2023-10-29 23:20:31 26 4
gpt4 key购买 nike

为什么/什么时候我们应该更喜欢 using std::swap; swap(a, b); over std::iter_swap(&a, &b)?

最佳答案

因为它们完全不同,所以你的问题是错误的二分法。

他们做不同的事情!

  • 一个交换值(value)。
  • 另一个交换迭代器目标的内容。

但是如果你有迭代器。最好使用 std::iter_swap。否则,您可以使用交换(只要您已在本地完成 using std::swap; 调用交换(否则正确交换的类型相关锁定可能会失败)。

std::iter_swap(i1, i2);

// Is equivelent to writing.
using std::swap; // Notice this.
swap(*i1 ,*i2); // Notice the * in front of the iterator.

关于c++ - 为什么/什么时候我们应该更喜欢使用 std::swap;在 std::iter_swap(&a, &b) 上交换(a,b)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293428/

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