Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching and Inserting in a map with 3 elements in C++

Tags:

c++

stl

I need to have a map like this :

typedef std::map<int, float , char> Maptype ;

What is the syntax to insert and searching elements of pair in this map.

like image 359
John Avatar asked Mar 06 '10 10:03

John


People also ask

Can a map have 3 elements in C++?

You cannot have three elements. The STL map stores a key-value pair. You need to decide on what you are going to use as a key.

How do you insert elements in a map?

If you want to insert element in std::map - use insert() function, and if you want to find element (by key) and assign some to it - use operator[].

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.


1 Answers

A map can only map one key type to one data type. If the data contains 2 elements, use a struct or a std::pair.

typedef std::map<int, std::pair<float, char> > Maptype;
...
Maptype m;
m[123] = std::make_pair(0.5f, 'c');
...
std::pair<float, char> val = m[245];
std::cout << "float: " << val.first << ", char: " << val.second << std::endl;
like image 56
kennytm Avatar answered Oct 21 '22 11:10

kennytm