swap Function (unordered_set)

交換兩個容器的內容。

template<class Key, class Hash, class Pred, class Alloc>
    void swap(
        unordered_set <Key, Hash, Pred, Alloc>& left,
        unordered_set <Key, Hash, Pred, Alloc>& right);

參數

  • Key
    索引鍵的型別。

  • Hash
    雜湊函式的物件型別。

  • Pred
    相等比較函式物件型別。

  • Alloc
    配置器類別。

  • left
    要交換的第一個容器。

  • right
    若要交換第二個的容器。

備註

樣板函式會執行left.unordered_set::swap(right)。

範例

 

// std_tr1__unordered_set__u_s_swap.cpp 
// compile with: /EHsc 
#include <unordered_set> 
#include <iostream> 
 
typedef std::unordered_set<char> Myset; 
int main() 
    { 
    Myset c1; 
 
    c1.insert('a'); 
    c1.insert('b'); 
    c1.insert('c'); 
 
// display contents " [c] [b] [a]" 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
    Myset c2; 
 
    c2.insert('d'); 
    c2.insert('e'); 
    c2.insert('f'); 
 
    c1.swap(c2); 
 
// display contents " [f] [e] [d]" 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
    swap(c1, c2); 
 
// display contents " [c] [b] [a]" 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
    return (0); 
    } 
 
  

需求

標頭: <unordered_set>

Namespace: 標準

請參閱

參考

<unordered_set>

unordered_set::swap

其他資源

<unordered_set> 成員