0

I do not understand what the operator [] does in sorted_vector_map.

  • Specifically when the key does not exist, what value is added to the data structure?
  • What is value_type(key, mapped_type())?
  • Is it a constructor call to std::pair by default?
  • What is mapped_type()?
  • Is it also a constructor call?
mapped_type& operator[](const key_type& key) {
    iterator it = lower_bound(key);
    if (it == end() || key_comp()(key, it->first)) {
      return insert(it, value_type(key, mapped_type()))->second;
    }
    return it->second;
}

Code is from the following link...

https://github.com/facebook/folly/blob/master/folly/sorted_vector_types.h#L1097

t.niese
  • 39,256
  • 9
  • 74
  • 101
user1559897
  • 1,454
  • 2
  • 14
  • 27

2 Answers2

2

The answers are in the header file.

Consider:

value_type(key, mapped_type())

One line 743 of the file you linked, you'll see this declaration:

typedef typename Container::value_type value_type;

But what's Container? On line 728 you'll find that Container is a template argument, which is probably a std::pair (unless the user has supplied another one).

class Container = std::vector<std::pair<Key, Value>, Allocator>>

So yes, that line is a constructor call to initialize a std::pair, because that's what this particular data structure uses as its value.

mapped_type() is also a constructor call, with no arguments. It's similar to:

int i = int();

Tumbleweed53
  • 1,491
  • 7
  • 13
1

Container is the template argument that defines what container is used by sorted_vector_map to store the key-value pairs and defaults to a std::vector(std::vector<std::pair<Key, Value>, Allocator>>)

value_type is Container::value_type (typedef typename Container::value_type value_type;) which (for the default template argument) is std::pair<Key, Value> (see std::vector Member types)

mapped_type is Value (typedef Value mapped_type;) so the type of the value stored in the sorted_vector_map

What is value_type(key, mapped_type())?
What is mapped_type()?
Is it also a constructor call?

So value_type(key, mapped_type()) creates a std::pair with key as first, and a default constructed Value (mapped_type()) as second.

Is it a constructor call to std::pair by default?

yes

template <
    class Key,
    class Value, // <<===============
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<Key, Value>>,
    class GrowthPolicy = void,
    class Container = std::vector<std::pair<Key, Value>, Allocator>>   // <<===============
class sorted_vector_map : detail::growth_policy_wrapper<GrowthPolicy> {
  detail::growth_policy_wrapper<GrowthPolicy>& get_growth_policy() {
    return *this;
  }

  template <typename K, typename V, typename C = Compare>
  using if_is_transparent =
      _t<detail::sorted_vector_enable_if_is_transparent<void, C, K, V>>;

  struct EBO;

 public:
  typedef Key key_type;
  typedef Value mapped_type; // <<===============
  typedef typename Container::value_type value_type; // <<===============
  typedef Compare key_compare;
  typedef Allocator allocator_type;
  typedef Container container_type;
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • If i had a sorted_vector_container object called sv and I were to create an object of type mapped_type, can I do sv:: mapped_type() to invoke the constructor? – user1559897 Dec 27 '20 at 22:15
  • 1
    @user1559897 don't ask new questions in the comment. If you have a new question create a new one. You have to use either `decltype` to get the type of `sv` (`decltype(test)::mapped_type`) or use `sorted_vector_container::mapped_type` with `Key` and `Value` being the types you used. – t.niese Dec 28 '20 at 08:39