I'm having some problems getting std::insert() to work on a 2D map. It works fine when I use the map as normal, but I can't seem to get the syntax quite right when using a map inside another map and was wondering if someone could help.
I would like the 2nd map to be unchanged for the time being:
string prevValue;
std::map <string, map <string, int > > mCounts;
mCounts.insert (std::pair <string, map <string, int>>(prevValue, map <string, int>);
Thanks in advance for any help.
You're missing a "temporary value" set of parentheses:
mCounts.insert (
std::pair <string, map <string, int>>(prevValue, map <string, int>());
// ^^
Here are a few alternatives:
mCounts.insert(std::make_pair("foo", std::map<std::string, int>()));
mCounts.emplace(std::piecewise_construct,
std::forward_as_tuple("bar"), std::forward_as_tuple());
mCounts["quz"];
And in C++17:
mCounts.try_emplace("zip");
Related
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));
}
I have a unordered_map<string, int> freq and I order it transforming it into a
map<int,string> freq2. I use the next function in order to do that:
map<int, string> order(unordered_map<string, int> x) {
map <int, string> map;
for (auto it = x.begin(); it != x.end(); ++it) {
map.emplace(it->second, it->first);
}
return map;
}
the size of the unordered_mapis 2355831 and the returned map is 505, so as you see the loss of data is quite big and i have no idea why....
Any idea why this happens?
Thanks.
EDIT:
Thanks to all, you are all right, I have a lot of int with same value, that´s why i loose the data( really stupid from my part to not see it before)
Most likely this is because there are duplicates among the int values. Try replacing map<int, string> with multimap<int, string>.
The code itself looks fine. However, since you are mapping from string keys to integers, it might be very well that you have multiple keys with the same value.
From the documentation of emplace:
The insertion only takes place if no other element in the container has a key equivalent to the one being emplaced (keys in a map container are unique).
So if a lot of your entries in the first map have the same value (which is the key in the second map), then your dataset will decrease by a lot.
If you need to preserve those elements, then std::map is not the right container.
In c++, I would like to insert a vector in a map.
The key of the map is a pair of string and int, and the value of one is a vector.
I am writing down the following code, however it seems that the vector is not inserted into the map.
Is the syntax of the code is wrong?
If so, could you tell me correct one?
map<pair<string, int>, vector<string> > my_map;
vector<string> v;
v.push_back("abcde");
my_map.insert(make_pair(make_pair("aaa",1),v));
You have used the v_pre while vector is of name v:
my_map.insert(make_pair(make_pair("aaa",1),v_pre));
The correct code should be:
my_map.insert(make_pair(make_pair("aaa",1),v));
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);
I am trying to access a specific element out of a std::map with more than two elements. Here is an example:
std::map <int, CString, CString, CString> map;
//Initialise
map[0] = _T("stuff1"), _T("stuff2"), _T("stuff3");
//now if I just want to access stuff3 is it this:
CString str = map[0][2];
//or something more like this?
CString str = map[0]. ???
Any help would be great thanks.
edit: Thanks sorry about that, first time using maps, I was wondering why I couldn't find any information on std::map 's with more elements inside.
Have you tried to compile this? It shouldn't.
You can create only a map with exactly 1 key and 1 value for each element.
But the value can be compound, so you can write
struct ValueType {
CString v1;
CString v2;
CString v3;
}
std::map <int, ValueType> map;
and access elements like map[somekey].v3;
To insert a value in such a map, you'll have to write
ValueType strings = {"1","2","3"};
map.insert(999, strings);
Or you may create a helper function (i.e. void addToMap(std::map <int, ValueType> &map, CSting const& v1, CString const& v2, CString const& v3) ), which will fill your map in a more convenient way.
std::map <int, CString, CString, CString> map; is illegal.
Either use a std::multimap or a std::map<int,std::vector<CString> >.
I believe this what you are looking for
std::map <int, std::list<CString> > myMap;
then you'll access myMap[0], then access each element in the returned std::list<CString>