Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pair is required to insert into map?

Though I do not dislike it, but find it inconvinient to declare a pair<X,Y> object, or make a call to make_pair, in order to call map::insert. Why insert doesn't take two arguments instead to specify Key and Value respectively.

While I understand it is for compatibility with other STL containers, that exhibit value_type. But find method takes key_type which breaks this compatibility assertion. map has both key_type and mapped_type, so why cant map have:

iterator insert(const key_type&, const mapped_type&);

Yes, there are overloads of insert taking iterator(s). But this two-argument insert could have been mixed well.

Just one advantage I see is: less call stack usage.

EDIT: Just found out that insert is the only method that takes value_type, that is pair<X,Y>. Many other methods like find, erase, at, count, equal_range, lower_bound, upper_bound and operator[] take key_type.

like image 303
Ajay Avatar asked Jan 20 '14 13:01

Ajay


People also ask

How do you insert a map of pairs?

map insert() in C++ STL. The map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the map container. Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the map container.

Can we use pair as key in map?

Do you mean cout << mymap[make_pair(1,2)] << endl; ? (1,2) is non-sensical, at least in this context. You must have an std::pair to be used as your key, and that means following what @andre just commented. Yes!

Are maps and pairs same?

A pair is a single unit with two members. A map has keys and values in it. So you can use pairs to fill up a map, the elements of the pair becoming key and value.

Can we store pair in map?

A C++ map is a way to store a key-value pair. A map can be declared as follows: #include <iostream> #include <map> map<int, int> sample_map; Each map entry consists of a pair: a key and a value.

How do you insert a pair in a map?

insert (pair): This function inserts the pair in the map. The insertion only takes place when the key passed is not already inset. It returns a pointer pair. First element pointing to the pair already present or newly inserted.

What is the use of insert () function in map?

insert (pair) : This function inserts the pair in map. The insertion only takes place when the key passed is not already in set. It returns a pointer pair. First element pointing to the pair already present or newly inserted.

How to insert the key-value pair in the map?

Using insert (): Insert function is used to insert the key-value pair in the map. After insertion, the reordering of elements takes place and the map is sorted w.r.t the key. This function is implemented in 3 ways: insert (pair): This function inserts the pair in the map. The insertion only takes place when the key passed is not already inset.

How to implement map insertion function in Python?

This function is implemented in 3 ways: insert (pair): This function inserts the pair in the map. The insertion only takes place when the key passed is not already inset. It returns a pointer pair. First element pointing to the pair already present or newly inserted. Second element returning the boolean status “true” or “false”.


1 Answers

All standard library containers define a value_type member type, and their interfaces generically operate in terms of this value_type: insert, push_back, push_front. The new interface emplace adds a way of constructing a value_type object as if by:

value_type(std::forward<Args>(args)...)

Basically, there is no special interface provided for the satellite-data associative containers (i.e. maps) which is aware of the special structure of value_type (which is defined, not perfectly well-known, to bepair<const key_type, mapped_type>), with the exception of find and erase and operator[], which take key_type arguments.

It is perhaps an oversight of the standard, or perhaps it was never deemed to be a problem, since you can always use make_pair, make_tuple or forward_as_tuple, or emplace, to create map value types.

(There is one problem with insert and move-only mapped types that has surfaced and is subject of this recent proposal.)

like image 112
Kerrek SB Avatar answered Sep 19 '22 16:09

Kerrek SB