In c++, Insert a vector in a map - c++

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

Related

How to extract the value of the pair retuned by unordered_map::emplace?

I'm trying to make my code 1 line shorter, a noble cause. I have this unordered map
std::unordered_map<std::string, int> um;
and I want to assign the integer to a variable on the same line where I emplace a pair into the unordered map like so
int i_want_132_here = um.emplace("hi", 132).first.???;
problem is, I have no idea what to do with [return value of unordered_map::emplace].first
In the debugger I can see that "first" contains ("hi", 132) but how do I access those values?
emplace returns a pair<iterator, bool>.
So you should do:
int i_want_132_here = (*um.emplace("hi", 132).first).second;
alternative syntax:
int i_want_132_here = um.emplace("hi", 132).first->second;
In general I prefer (*it) form instead of it->.

How to use std::insert in 2d map in c++?

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

Loss of data while ordering an unordered_map c++

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.

Inserting into nested map in C++

Assume I have a nested map of type pointer. Then is there a single line statement to insert into the nested map,
map<int, map<int, int> >* nestedMap;
Currently I am doing this in 2 steps. First creating innermap and then insert into outer map as below,
nestedMap->insert(make_pair(int, map<int, int>)(int, innermap));
If the map is not pointer type, then i can insert easily like this,
nestedMap[int][int] = int;
Is there any simple ways for inserting into nested map of type pointer ?
thanks
Prabu
map::operator[] automatically creates the key/value pair if it doesn't exist.
(That's why it's not const!)
So you don't need to create the inner map manually.
If you want to avoid creating the pair automatically, then use map::find() or map::at().
I believe the simplest one-liner is:
(*nestedMap)[int][int] = int;
If i understand your question properly, you can actually use reference instead of pointer. You are not having issue with nested map, instead your outter map.
See below code, is what you want?
map<int, map<int, int> >* nestedMap = new map<int, map<int, int> >;
map<int, map<int, int> > &nestedMapAlais = *nestedMap;
nestedMapAlais[1][2] = 3;
access the operator[] via ->:
nestedMap->operator[](5)[6] = 7;
This is analogous to
nestedMap[5][6] = 7;
if nestedMap is not a pointer.
Note that in neither case do you have to explicitly insert a map.

C++, Error in Array

I assigned array
char words[100][100];
Now I want to save a word and its pos in the line.
Say line has "hi Iam a programmer". Now I want to save
string word;
while(line){
//called a function to get the word and position.
words[word]["pos"] = pos;
}
I have split the words and saved in the string word, but when I try to save I get error.
"No viable overloaded operator[] for type 'char[100][100]"
What am I doing wrong?
You are trying to use array as a map. You can't use strings as array index. The structure you need is std::map<std::string, std::map<std::string, int> >
std::map<std::string, std::map<std::string, int> > m;
m["foo"]["bar"] = 10;
char[100][100] is a multidimensional array of single characters, which can be used to store fixed-length strings. It can be indexed using integer variables, not strings.
It looks like you want to use std::map<std::string, std::map<std::string, int> > or similar.
You cannot use a string as an index in C++ arrays. What you need is a map:
std::map< std::string, std::map<string, int> > words;
Then you have to:
words[word]["pos"] = pos;
But, what other data would you be saving other than pos? If not, then why do you want to make it a 2 dimensional data structure? Can't you just:
positions[word] = pos;
Where positions is of type std::map<std::string, int>.
EDIT: As pointed out by Mike, not using pointers anymore.