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

履歴 編集

function

::node_handle::swap(C++17)

void swap(node_handle&)
    noexcept(ator_traits::propagate_on_container_swap::value ||
             ator_traits::is_always_equal::value);

概要

コンテナノードの所有権を入れ替る。

要件

一方のノードハンドルが空である。または、allocator_traits<allocator_type>::propagate_on_container_swaptrueである。または、両方のアロケータが等値である。

効果

swap(ptr_, nh.ptr_) を呼び出す。一方のノードが空であるか、両方のノードが空でなく std::allocator_traits<allocator_type>::propagate_on_container_swaptrue の場合、アロケータも同様に入れ替えられる。

戻り値

なし

例外

noexcept(ator_traits::propagate_on_container_swap::value ||
             ator_traits::is_always_equal::value);

で指定される。

#include <iostream>
#include <map>

int main()
{
  std::map<int, char> m = {
    {10, 'a'},
    {20, 'b'},
    {30, 'c'}
  };

  // ノードを取得
  std::map<int, char>::node_type node1 = m.extract(10);
  std::map<int, char>::node_type node2 = m.extract(20);

  std::cout << "node1 : [" << node1.key() << ", " << node1.mapped() << "]" << std::endl;
  std::cout << "node2 : [" << node2.key() << ", " << node2.mapped() << "]\n" << std::endl;

  // ノードを入れ替える
  node1.swap(node2);

  std::cout << "node1 : [" << node1.key() << ", " << node1.mapped() << "]" << std::endl;
  std::cout << "node2 : [" << node2.key() << ", " << node2.mapped() << "]" << std::endl;
}

出力

node1 : [10, a]
node2 : [20, b]

node1 : [20, b]
node2 : [10, a]

バージョン

言語

  • C++17

処理系

関連項目

参照