Use of List inside map C++ - c++

Can I use following syntax:
std::map<int,std::list<int>> mAllData;
Where Key Value(int) will be ID of data, and said data could have multiple types so storing all them against said key value. I am trying to use it.

std::map<int,std::list<int>> my_map;
my_map[10].push_back(10000);
my_map[10].push_back(20000);
my_map[10].push_back(40000);
Your compiler may not support the two closing angle brackets being right next to each other yet, so you might need std::map<int,std::list<int> > my_map.
With C++11 my_map can be initialized more efficiently:
std::map<int,std::list<int>> my_map {{10, {10000,20000,40000}}};
Also, if you just want a way to store multiple values per key, you can use std::multimap.
std::multimap<int,int> my_map;
my_map.insert(std::make_pair(10,10000));
my_map.insert(std::make_pair(10,20000));
And in C++11 this can be written:
std::multimap<int,int> my_map {{10,10000},{10,20000}};

Related

How to insert data into nested map in c++?

//I am reading data from file and storing data into structure.Here "obj" is a object of structure.
Also note that my file have outer map key multiple times means when i am reading from file then some field of structure has common value and i am using that common value as a key of outer loop.
If I have only single value of outer key then it works fine but when more than one value of key then it fails.
typedef std::map<double,Order_Msg,std::greater<double> >InnerMap;
typedef std::map<int, InnerMap> OuterMap;
InnerMap buy_detailsmap;
OuterMap buy_tokenmap;
//one way
buy_tokenmap.insert(make_pair(obj.token,InnerMap()));
buy_detailsmap.insert(make_pair(obj.orderId,obj));
//another way
buy_detailsmap.insert (std::pair<double,Order_Msg>(obj.orderId,obj));
buy_tokenmap.insert(std::make_pair(obj.token,buy_detailsmap));
I tried both but its not working properly.
It isn't clear why you need buy_detailsmap, as it is de-coupled from buy_detailsmap. Unless you really need insert's semantics, you could simply use operator[]:
buy_tokenmap[obj.token][obj.orderId] = obj;

Dictionary in C++ using a Map with no values, only keys

I'm implementing some sort of lookup for words in c++, and while the code for implementing a map is there, I wanna make sure if it works that using a map with keys and values as std::string, and using only keys as lookups without a value to return.
std::vector< std::string> DictionLines;
Reader DictionReader(Dictionary);
DictionLines = DictionReader.getLines();
std::map<std::string, std::string> DictionaryM;
for (int t = 0; t < DictionLines.size(); ++t) {
DictionaryM.insert(std::pair<std::string, std::string>(DictionLines.at(t), DictionLines.at(t)));
}
This code takes in the 349900 words in a Dictionary.txt file, and stores them in the map. Each line of the dictionary is just the word to lookup; no definition or any value to associate. Which is why I think just storing a pair of the same key and value in the map is ok, and using find and first/second would also be fine? Please confirm.
It looks like you want std::set. It is like a map where only keys matter and you never care or use the value. To look in a dictionary represented as a std::set<std::string> for some word after a given prefix, consider lower_bound
You should look more into C++ standard containers. There are not that much choice, and you should somehow know all of them (and choose or combine the right containers for the job)

How do I create an std::map with multiple indices?

The following works:
std::map<std::string, Animal*> animalMap;
animalMap["KillerRabbit"] = new KillerRabit;
But what if I wanted to do this?
animalMap["KillerRabbit"]["White"] = new KillerRabit;
I have no idea what the 'official' name for the indices brackets are, knowing them would help immensely while Googling =p
What you are looking for is a map of maps:
std::map<std::string, std::map<std::string, Animal*>> animalMap;
Now each value stored in animalMap is itself a std::map. The key type for both the outer and inner maps are std::string.
The [...] syntax is the subscript operator. More specifically, though, you subscript a map with keys. Keys are mapped to values.
sftrabbit gives the canonical way to do it. If you don't want multiple map look ups per key you could also use std::pair as a map key.
Here is an example of doing it that way.

using multiple keyvalues for std::map C++

Hi I am trying to write a function which reads data from a file and then saves it in memory.
This memory will need a x and a y value to be identified. It may not be linear, there can be big jumps between the different x and y values and the amount of values are unknown which excludes the use of an multi-dimensional array.
I wanted to use std::map since it does what I need, but it does not support multiple key values. What else could I use to store the data or is there a way to merge the X and Y values so that it will be able to be used in a map container?
Make a pair of the x and y values, and use that as the key:
std::map<std::pair<int, int>, whatever>
Note that as it stands, this will treat the x values as more significant than the y values if you traverse the map in order. If you want the y values to be more significant, you'd want to put them first in the pair.
You should use an std::pair as the key to your map:
std::map<std::pair<int, int>, value_type> m;
You can insert into the map using:
m[std::make_pair(0, 0)] = some_value;
If you don't care about the order of your elements and would like faster retrieval and insertion, try a std::unordered_map instead.
Although you could use std::pair as suggested by the others, I would seriously consider making a simple class that contains data members for your key. It improves readability, and also makes it easier to extend it with a 3rd, 4th, ... member if needed.
If you start with std::pair, and then you want to add a 3rd element, you could be tempted to move to std::tuple, but this can result in unreadable code.
Simply make a key class, and give it a decent constructor (one argument per data member).

Insertion in maps in C++

map<string ,vector<string> > hashes;
hashes.insert(pair<string,vector<string> > (a,b )); //error coming
What is the problem coming when i am using the above statement in C++, where a and b are strings?
How does the insertion takes place in this type (i.e. one container containing more container) of associative container?
Many many thanx in advance
You probably want
hashes[a].push_back(b)
That's if you want b to be appended to the present vector. If you want it to replace it, use
hashes[a].assign(1, b)
You're trying to use a string where a vector of string is needed. You need to insert b into a vector, then insert (a, your_vector) into the map.
Alternatively, use a multimap<string, string> to get the same basic effect in a way you may find easier to use. This would allow your insert(pair<...>(a, b)).
Also consider using std::make_pair instead of instantiating std::pair directly. It'll deduce the types for the arguments so you don't need to fill them out explicitly.
Just follow the definition:
vector<string> v;
v.push_back("mystring");
hashes.insert(std::make_pair("key", v));
Note how the second parameter to make_pair is a vector<string>. This will never fail then. It will fail if the second parameter is a string.