How should std::map be used with a value that does not have a default constructor?

前端 未结 3 1698
闹比i
闹比i 2021-01-18 16:58

I\'ve got a value type that I want put into a map. It has a nice default copy constructor, but does not have a default constructor.

I believe that so long as I stay

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 17:45

    There are two things you missed in the interface of map (and the like):

    • insert(value_type) returns a std::pair, the .first member points to the element with the key you tried to insert and the .second member indicates whether it is actually the element you tried to insert or another that previously was in the container.
    • insert(iterator, value_type) allows you to give a hint as to where insert

    The latter is not necessarily useful in your situation though.

    typedef std::map Map;
    
    // insert and check
    std::pair const result =
      map.insert(std::make_pair(5, x));            // O(log N)
    
    if (not result.second)
    {
      result->first.second = x;                    // O(1)
      // OR
      using std::swap;
      swap(result->first.second, x);
    }
    

    If you type does not support assignment and there is no swap, however, you need to bite the bullet:

    // locate and insert
    Map::iterator position = map.lower_bound(5);         // O(log N)
    if (position != map.end() and position->first == 5) 
    {
      position = map.erase(position);                    // O(1)
    }
    map.insert(position, std::make_pair(5, x));          // O(log N) if rebalancing
    

    In C++11, the insert methods are doubled:

    • insert(value_type const&) // insertion by copy
    • insert(P&&) // insertion by move

    and with perfect forwarding we get the new emplace method. Similar to insert, but which construct the element in place by forwarding the arguments to its constructor. How it differentiate arguments for the key and value is a mystery to me though.

提交回复
热议问题