iter_swap(std::counted_iterator)
來自 cppreference.com
template< std::indirectly_swappable<I> I2 > friend constexpr void |
(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) |
將底層迭代器解引用的結果轉換為其關聯的右值引用型別 (函式) |