• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <map>

    std::map::swap

    void swap(map<Key,T, Compare,Allocator>& st);
    void swap(map& x)
      noexcept(allocator_traits<Allocator>::is_always_equal::value
                && noexcept(swap(declval<Compare&>(),declval<Compare&>()))); // C++17
    

    概要

    コンテナ内のコンテンツを、同じ型の要素を保持する他の map オブジェクトである st 内のコンテンツと交換する。サイズは異なる場合もある。 このメンバ関数の呼び出しの後、呼び出し前にコンテナ内にあった要素は st へ、st 内にあった要素は *this へ移る。全てのイテレータ、参照、ポインタは有効なまま残る。

    パラメータ

    • st *thisとコンテンツを交換する、同じ型の map コンテナ。

    計算量

    定数時間

    #include <iostream>
    #include <map>
    
    template <class Map>
    void print(const char* name, const Map& m)
    {
      std::cout << name << " : {";
      for (const auto& x : m) {
        std::cout << "[" << x.first << "," << x.second << "], ";
      }
      std::cout << "}" << std::endl;
    }
    
    int main()
    {
      std::map<int, char> m1 = {
        {10, 'a'},
        {20, 'b'},
        {30, 'c'}
      };
    
      std::map<int, char> m2 = {
        {5, 'd'},
        {15, 'e'}
      };
    
      // m1とm2を入れ替える
      m1.swap(m2);
    
      print("m1", m1);
      print("m2", m2);
    }
    

    出力

    m1 : {[5,d], [15,e], }
    m2 : {[10,a], [20,b], [30,c], }
    

    バージョン

    言語

    • C++03

    処理系

    参照