How to insert value in map? - c++

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;
}

Related

Is it possible to insert a pair with a map type value into another map?

Tried looking this up but can't quite seem to find a detailed answer. Say I have a map:
std::map<int, string> categoryMap;
and I want to create a second map with keys and values that should only be accessed when associated with a specific key of the first map. The second map would also be similar:
std::map<int, string> itemMap;
I have attempted doing some sort of an insert function to try this out
categoryMap.insert(std::pair<int, map<int, string>>(itemValue, itemMap));
The error I receive claims that "no instance of overloaded function matches the argument list." Is there another way to approach this or is this just not possible?
Yes, it is possible but the type (template parameter) int, map<int, string> of the pair you are trying to insert does not match the map type int, string.
To be able to run your insert call:
categoryMap.insert(std::pair<int, map<int, string>>(itemValue, itemMap));
categoryMap has to be defined with the same template type as the item you are inserting, i.e.:
std::map<int, map<int, string>> categoryMap;
See it online.
#include <iostream>
#include <string>
#include<map>
using namespace std;
int main()
{
std::map<int, std::map<int, string>> categoryMap;
std::map<int, std::string> itemMap;
itemMap.insert(std::pair<int, std::string>(1, "abc"));
itemMap.insert(std::pair<int, std::string>(2, "xyz"));
categoryMap.insert(std::pair<int, std::map<int, std::string>>(1, itemMap));
}

Inserting data to somewhat complex data structure - C++

I have a following data structure:
std::map<std::string,std::vector<std::map<std::string,std::vector<std::map<std::string,double> > > > >
So a map containing string as a key and as a value a list of maps, that have again string as a key and list of maps as a value.
Is there a way to insert data using one liner. (Im not aware what this technique is called.
What I mean is that in python i could do:
datas={"key":[{"key2":[{"key3":234}]}]}
I tried this:
ostokset.insert({ketju,{{kauppa,{{tuote,hinta}}}}});
But it didn't work.
Please don't do this! It is completely unreadable and unmaintainable.
That being said, here is the solution to your question:
m["key1"] = {{{"key2", {{{"key3", 234.}}}}}};
To reach this solution I basically created initializers for each type, from the innermost (easiest) to the outermost (most complex):
std::map<std::string, double> map_x = {{"key3", 234.}};
std::vector<std::map<std::string, double>> v_x = {map_x};
std::map<std::string,
std::vector<std::map<std::string, double>>> map_y = {{"key2", v_x}};
std::vector<std::map<std::string,
std::vector<std::map<std::string, double>>>> v_y = {map_y};
And then started to construct the solution backwards by replacing each variable with its initialization:
m["key1"] = v_y;
m["key1"] = {map_y};
m["key1"] = {{{"key2", v_x}}};
m["key1"] = {{{"key2", {map_x}}}};
m["key1"] = {{{"key2", {{{"key3", 234.}}}}}};
You have to consider an extra bracket for the std::pair inside the std::map
#include <cstdio>
#include <map>
#include <string>
#include <vector>
int main() {
std::pair<std::string, double> a{"s1", 1.0};
std::map<std::string, double> b{{"s2", 2.0}};
std::vector<std::map<std::string, double>> c{{{"s1", 3.0}}};
std::map<std::string, std::vector<std::map<std::string, double>>> d{
{"s2", {{{"s1", 4.0}}}}};
std::vector<
std::map<std::string, std::vector<std::map<std::string, double>>>>
e{{{"s2", {{{"s1", 5.0}}}}}};
std::map<std::string,
std::vector<std::map<std::string,
std::vector<std::map<std::string, double>>>>>
f{{"s3", {{{"s2", {{{"s1", 6.0}}}}}}}};
printf("%f\n", b["s2"]);
printf("%f\n", f["s3"][0]["s2"][0]["s1"]);
return 0;
}
And compile with at least C++11.

Push_back map into vector

How can I push_back a std::map element into std::vector?
std::vector<std::map<int, int>> v;
// Error
v.push_back(std::make_pair(0, 1))
What cause this error?
Using
v.push_back(std::make_pair(0, 1))
is a problem since a std::pair cannot be converted to a std::map. You can resolve the problem using one of the following methods that I can think of.
Method 1
If you can use a C++11 compiler,
v.push_back({{0, 1}});
Method 2
std::map<int, int> m;
m.insert(std::make_pair(0, 1));
v.push_back(m);
Method 3
std::map<int, int> m;
v.push_back(m);
v.back().insert(std::make_pair(0, 1));
std::vector<std::map<int, int>> v;
means you are declaring vector of maps!
push_back a map into vector, like this
v.push_back({std::make_pair(0, 1)}); //Needs C++11
OR
std::map<int, int> map1;
map1.insert(std::make_pair(0, 1));
v.push_back(map1);
push_back'ng a pair will obviously result into a compilation error.
You can push a map into your Data Structure, std::vector<std::map<int, int>> v. not a pair i.e. element of map.
So, do as following.
std::map<int,int> t;
t.insert(std::make_pair(0, 1));
v.push_back(t);
or in case of C++11
v.push_back({{1,2}});
Or alternatively you can go for emplace_back too.
void emplace_work_around(
std::vector<std::map<int, int>>& v,
std::initializer_list<std::pair<const int,int>> item
)
{
v.emplace_back(item);
}
int main()
{
std::vector<std::map<int, int>> v;
emplace_work_around(v,{{1,2}});
}

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);