Insert Pair as a key in map using c++ - c++

I want to know how can insert pair in map using c++, here's my code:
map< pair<int, string>, int> timeline;
I tried to insert it using:
timeline.insert(pair<pair<int, string> , int>(make_pair(12, "str"), 33);
//and
timeline.insert(make_pair(12, "str"), 33);
but I got error
\main.cpp|66|error: no matching function for call to 'std::map<std::pair<int, std::basic_string<char> >, int&>::insert(std::pair<int, const char*>, int)'|

std::map::insert expects std::map::value_type as its argument, i.e. std::pair<const std::pair<int, string>, int>. e.g.
timeline.insert(make_pair(make_pair(12, "str"), 33));
or simpler as
timeline.insert({{12, "str"}, 33});
If you want to construct element in-place you can also use std::map::emplace, e.g.
timeline.emplace(make_pair(12, "str"), 33);
LIVE

When in doubt, simplify.
auto key = std::make_pair(12, "str");
auto value = 33;
timeline.insert(std::make_pair(key, value));

Simply use the traditional way:
timeline[key] = value;
For instertion and retrival of pair:
timeline[{1,"stackOverFlow"}] = 69;
for(auto i: timeline)
{
cout<< i.first.first;
cout<< i.first.second;
cout<< i.second;
}

Related

How to insert value in map?

I am having an issue with map.
map<int, map<int , int>> my_map;
I am using insert() like:
my_map.insert(10, my_map.second.insert(20, 30));
but it's not working.
The method to insert into map in dictionary is add(key,value)
Your code my_map.insert(10, my_map.second.insert(20, 30)); will throw an error as `second' is not a method that can be called on map.
Here is what you can do to solve this issue:
map<int, map<int , int>> my_map;
map<int, int> my__second_map = new map<int,int>();
my_second_map.add(20,30);
my_map.add(10,my__second_map);
You need an iterator of map type to call second.
You could also use below code using map insert. You may also use pair data type.
#include <map>
#include <iostream>
int main()
{
std::map<int, std::map<int , int> > my_map;
std::map<int,int> data;
data.insert(std::pair<int,int>(20,30));
my_map.insert(std::pair<int,std::map<int,int> >(10, data));
return 0;
}

map of map initialization

I am trying to initialize a map of map but I am unsure what mistake I am doing. Below is the sample code.
static std::map<std::string, std::map<std::string,std::string>> _ScalingMapVolume ={
{"AA",{"busy_timeout","test"}},
{"BB",{"cache_size","10000"}}
};
The error I am getting is;
error: no match for call to ‘(std::_Select1st<std::pair<const std::basic_string<char>, std::basic_string<char> > >) (const char&)’
{"busy_timeout","test"} is not the value of a map, but a pair. You need {{"busy_timeout","test"}}.
Your code should look like this:
static std::map<std::string, std::map<std::string, std::string>> _ScalingMapVolume = {
{"AA", {{"busy_timeout", "test"}}},
{"BB", {{"cache_size", "10000"}}}
};
init = {{"AA", {"busy_timeout", "test"}}, ...}
You are missing one set of braces, since the value_type of the map is std::pair<const std::string, std::map<std::string, std::string>>. The value_type of the mapped_type is
std::pair<const std::string, std::string>. So you need to use it that way.

insert a std::initializer_list into std::map

I have a method like this:
std::map<std::string, int> container;
void myMap(std::initializer_list<std::pair<std::string, int>> input)
{
// insert 'input' into map...
}
I can call that method like this:
myMap({
{"foo", 1}
});
How I can convert my custom argument and insert into the map?
I tried:
container = input;
container(input);
But don't work because the parameter of map is only std::initializer_list and there's no std::pair there.
Thank you all.
container.insert(input.begin(), input.end());
If you want to replace the contents of the map. do container.clear(); first.
Your problem is that the value_type of std::map<std::string,int> is not
std::pair<std::string,int>. It is std::pair<const std::string, int>. Note the const on the key. This works fine:
std::map<std::string, int> container;
void myMap(std::initializer_list<std::pair<const std::string, int>> input) {
container = input;
}
If you can't change your function's signature, you have to write a loop or use std::copy to convert each input element into the value_type of the container. But I'm guessing you probably can, since it's called myMap and not otherGuysMap :) .

two keys map in C++

I plan to use a map with two keys for my assignment. And I create my map like following:
map<pair<string, string>, int> myMap;
map<pair<string, string>, int>:: iterator it;
I had a hard time on how to use map.find() and map.insert() for finding existing entry in the map or insert a new value if two keys combination is new. Can some one give an example?
It should be the same as with any map, except you have to make pairs for your key.
Insert :
map< pair<string,string>, int > mymap;
pair<string, string> key = make_pair("bob", "sue");
mymap[ key ] = 5; // you can inline make_pair if you prefer.
// or you can use insert method
mymap.insert( key, 5 );
Find :
pair<string, string> key = make_pair("bob", "sue");
auto it = mymap.find( key ); // you can inline make_pair if you prefer.
if ( it != mymap.end() )
{
cout << it->second;
}
Note that using strings as a key in a map can have performance issues. Also, the order of the strings in the pair has significance.
it = myMap.find(make_pair("hi","mike"));
insert is a little awkward because you're inserting a pair whose first component is also a pair:
myMap.insert(make_pair( make_pair("hi","john"), 4 ) );
You should have a look at Boost multiIndex
This works:
typedef pair<string, string> key_type;
map<key_type, int> myMap;
myMap.insert(std::make_pair(key_type("a","b"),1));
map<pair<string, string>, int>::iterator it;
it = myMap.find(key_type("a","b"));
insert can be replaced with emplace in C++11 to shorten the code:
myMap.emplace(key_type("a","b"),1);

Inserting item into map, which holds 2 more map

I am trying to insert an item, into a map, which holds two other map.
map< map<int, int> , map<int, int> > GC;
map<int, int> A;
A.insert(pair<int,int>(1,1));
map<int, int>:: iterator p1 = A.begin();
map<int, int> B;
B.insert(pair<int,int>(2,3));
map<int, int>:: iterator p2 = B.begin();
GC.insert( pair< map<int,int>, map<int,int> > (*p1, *p2) );
Well, as presumed its not working.
How to do it?
EDIT:
It gives the following error:
E:\CB\test.cpp|20|error: no matching function for call to 'std::pair<std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >, std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > > >::pair(std::pair<const int, int>&, std::pair<const int, int>&)'
I am not sure what are trying to achieve here .....
Your key and value needs to be an map object in order to work...
In that case only possibility is
GC.insert( pair< map<int,int>, map<int,int> > (A,B) );
I think you really want a Node class with a list/vector of Edge objects. Each Edge object would contain the edge weight and a pointer to the Node that edge connected to.
There may be better ways to create a graph than I have described, but this should get you thinking in the right direction.
map<keyValue, VertexIndexValue>, map<(EdgeWeight1,VertexIndexValue1), (EdgeWeight2,VertexIndexValue2)......>
I think what you want is to use a structure as the second element of your map. It gives you the chance to hold whatever you want in the map without it getting confusing.
struct sMapValue
{
int mVertIndex;
map<int, int> mVertEdgeMap;
};
map< int, sMapValue> GC;