Monday, November 13, 2017

C++17: Node Handle

C++17 added node handles to associative containers that may be used for moves instead of copies. Here is an example:

#include <iostream>
#include <set>

int main()
{
  std::set<int> mySet{1, 2, 3};
  std::set<int> mySet2;

  std::set<int>::value_type myValue;
  //std::set<int>::node_type myNodeHandle = mySet.extract(2);
  //mySet2.insert(std::move(myNodeHandle));
  std::cout << mySet.count(1) << " ";
  std::cout << mySet.count(2) << " ";
  std::cout << mySet.count(3) << " ";

  std::cout << mySet2.count(1) << " ";
  std::cout << mySet2.count(2) << " ";
  std::cout << mySet2.count(3) << " ";

  return 0;
}
// Theoretical Output: 1 0 1 0 1 0
References:
http://en.cppreference.com/w/cpp/container/node_handle
https://stackoverflow.com/questions/39423059/when-do-i-use-node-type-with-stdmapinsert

No comments:

Post a Comment