Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding array values to std::multimap

Tags:

c++

multimap

I am trying to use a multimap with an integer key and values made of array of integers with 2 elements.

typedef std::multimap<int,int[2]> reverseHeightMap;
reverseHeightMap container;

When I try to add values like this:

container.insert( std::pair<int,int[2]>(5,{1,2}) );

I get:

error C2143: syntax error: missing ')' before '{'

I can't figure if I am failing at defining the data structure or inserting the value, or both. Thanks in advance for the help :)

like image 309
Andrea Casaccia Avatar asked Feb 13 '12 23:02

Andrea Casaccia


People also ask

How do you add values to a multimap?

insert() is used to insert new values to the multimap container and increases the size of the container by the number of elements inserted. Unlike a map container that checks the associated key already present then won't insert the elements but multimap has the feature of associating multiple elements to the same key.

How do you access values in a multimap?

We can find all values of a key in Multimap using is member function equal_range(). It accepts the key as an argument and returns a pair of multimap iterator. This returned pair has a range that represents the entries with given key.

How is multimap sorted?

Multimap is an associative container that contains a sorted list of key-value pairs, while permitting multiple entries with the same key. Sorting is done according to the comparison function Compare , applied to the keys. Search, insertion, and removal operations have logarithmic complexity.


1 Answers

You can't store arrays in containers because one of the requirements for the datatypes stored in STL containers is that they are assignable; arrays are not assignable.

Consider using std::vector or std::array<int, 2>.

like image 61
Seth Carnegie Avatar answered Sep 22 '22 16:09

Seth Carnegie