为什么/什么时候我们应该更喜欢 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/