I do not understand what the operator [] does in sorted_vector_map.
value_type(key, mapped_type())
?std::pair
by default?mapped_type()
?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
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;