名稱空間
變體
操作

iter_swap(std::counted_iterator)

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
(C++20)
(C++20)
(C++20)
工具
(C++20)
迭代器介面卡
範圍訪問
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
template< std::indirectly_swappable<I> I2 >

    friend constexpr void
        iter_swap( const counted_iterator& x, const std::counted_iterator<I2>& y )

            noexcept(noexcept(ranges::iter_swap(x.base(), y.base())));
(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)
將底層迭代器解引用的結果轉換為其關聯的右值引用型別
(函式) [編輯]