iter_swap(std::counted_iterator)
来自 cppreference.cn
< cpp | iterator | counted iterator
template< std::indirectly_swappable<I> I2 > friend constexpr void |
(since C++20) | |
交换两个底层迭代器指向的对象。如果 x.count() 或 y.count() 等于 0,则行为未定义。
函数体等效于: ranges::iter_swap(x.base(), y.base());。
此函数模板对于普通的非限定或限定查找不可见,并且只能通过实参依赖查找在 std::counted_iterator<I> 是参数的关联类时找到。
目录 |
[编辑] 参数
x, y | - | 要交换的元素的迭代器适配器 |
[编辑] 返回值
(无)
[编辑] 复杂度
常数。
[编辑] 示例
运行此代码
#include <iostream> #include <iterator> #include <list> #include <vector> int main() { std::vector p{1, 2, 3, 4}, q{5, 6, 7, 8}; std::counted_iterator<std::vector<int>::iterator> ip{p.begin(), 2}; std::counted_iterator<std::vector<int>::iterator> iq{q.begin(), 3}; std::cout << *ip << ' ' << *iq << '\n'; iter_swap(ip, iq); // ADL std::cout << *ip << ' ' << *iq << '\n'; std::list x{0, 1, 3}; std::counted_iterator<std::list<int>::iterator> ix{x.begin(), 2}; // iter_swap(ip, ix); // error: not indirectly swappable }
输出
1 5 5 1
[编辑] 参见
交换两个对象的值 (函数模板) | |
交换两个元素范围 (函数模板) | |
交换两个迭代器指向的元素 (函数模板) | |
(C++20) |
交换两个可解引用对象引用的值 (自定义点对象) |
(C++20) |
将解引用底层迭代器的结果强制转换为其关联的右值引用类型 (函数) |