Found this Multimap containing pairs?, but it is not much help
How would I insert two strings into pair? Below, my two failed attempts.
multimap<string, pair<string,string> > mymm;
mymm["Alex"] = std::pair<"000","000">; //errors
mymm.insert(pair<string, pair<string, string> >
("Alex", std::pair<"000","000">); // errors out as well
I am using Visual Studio 2010, 32 bit. Thanks !
mymm.insert(make_pair("Alex",make_pair("000","000")));
A multimap doesn't allow lookup using operator [], since there may be more than one match.
make_pair is a convenient way to create a pair without having to specify the types explicitly. Without using make_pair, you would need to do something like this:
mymm.insert(pair<string,pair<string,string> >("Alex",pair<string,string>("000","000")));
std::pair<string,string>("000","000") should do it.
The code contained between < and > indicates the types of the variables you're inserting-- in this case strings
Related
I have the following nested data structure:
map<string, unordered_set<unordered_map<string, string> > > complicateMap;
Meaning, a map from string, to a set of maps from string to string.
When trying to compile, I'm getting:
error C2338: The C++ Standard doesn't provide a hash for this type.
Narrowing it down, I see that the problem is unordered_set<unordered_map<...> >
Seems like unordered_map, by itself, doesn't have a default hash.
Is there a better approach to this data structure? Or how to implement a hash for an entire map efficiently?
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)
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.
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.
I have a problem with the creation of a hash of arrays. I need a Single Key - Multi Data system:
multimap <Type, vector<type> > var;
But how I can add elements to the vector?
Example: key = 3;
Now I need to append some elements into the vector whose key is 3.
Creating a temp-vector not an answer because I don't know when I need to input element into the vector with the current key.
sorry, understand my problem. i need fast-access struct, that will be operate with ~50,000 words with length ~20 each.
and i need something like tree.
also, have question:
how quick STL-structures, like vector,map,multimap and other?
What's wrong with std::map <KeyType, std::vector<SomeType> >, or some other collection as the value type? This gives you control over how to operate on the value collection. A multimap to me seems like a low-level form of std::map <KeyType, std::list<SomeType> >, but with none of the flexibility of a list.
To find the answer to your question you can look at the slides under point 6. at this site https://ece.uwaterloo.ca/~ece250/Lectures/Slides/
Hope that helps!