Dec 23, 2013 at 10:25am UTC
Do you mean something like this?
1 2 3 4 5 6 7 8 9
#include <map>
#include <vector>
#include <string>
// Create the object
std::map<std::string, std::vector<std::string> > dictionary;
// Example of setting a value
dictionary["Dog" ] = {"terrier" , "white" , "old" };
EDIT:
Here is a quick example:
http://coliru.stacked-crooked.com/a/230ae066ea57c761
Last edited on Dec 23, 2013 at 10:40am UTC
Dec 23, 2013 at 10:50am UTC
I would like to search through the dictionary with a function
1 2 3 4 5 6 7 8 9 10
string searchDictionary(string wordToSearch) {
// searching
map<string, string>::iterator it = dictionary.find(wordToSearch);
if (it != dictionary.end()) {
//cout << "Found! " << it->first << " is " << it->second << "\n";
return it->second ;
}else {
return "Not found" ;
}
}
The above works with two words or a pair, but how the function could look like for more words?
Bwt, i cannot use "auto", cause i'm using c++ 0x3
Last edited on Dec 23, 2013 at 11:04am UTC
Dec 23, 2013 at 11:02am UTC
Do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// use a typedef
typedef std::vector<std::string> dictType;
dictType searchDictionary(std::string wordToSearch) {
std::map<std::string, dictType>::iterator it;
it = dictionary.find(wordToSearch);
if (it != dictionary.end()) {
return it->second;
} else {
// Return empty set of results
return dictType();
}
}
// example usage
dictType results = searchDictionary("colour" );
if (!results.empty()) {
std::cout << "Colour: " ;
for (dictType::const_iterator it = results.begin(); it != results.end(); ++it) {
std::cout << *it << " " ;
}
} else {
std::cout << "No results found." ;
}
Last edited on Dec 23, 2013 at 11:04am UTC
Dec 23, 2013 at 11:22am UTC
Thank you: i will try.
Just a question, the previous routine can print the first and the second elements.
it is possibile to do this way also?
cout << "Found! " << it->first << " is " << it->second << "\n" << it->third << "\n" << it->fourth;
maybe something similar
Last edited on Dec 23, 2013 at 11:33am UTC
Dec 23, 2013 at 11:24am UTC
No, but you can do it this way:
1 2
std::cout << "Found! " << it->first << " is " <<
it->second[0] << " " << it->second[1] << " " << it->second[2]
Though, if you do that, you have to be sure that there ARE at least 3 words associated with the key.
(
it->second.size() can tell you)
Last edited on Dec 23, 2013 at 11:27am UTC
Dec 23, 2013 at 11:37am UTC
Thank you very much.
I've got the point, now.
Jan 2, 2014 at 1:45pm UTC
Thank you for the informations.
The example of NT3 is interesting.
Have to investigate on the Tuple too. It's a specific c++11 or is available on c++0x?
going to search about std::multimap too.
Last edited on Jan 2, 2014 at 2:21pm UTC
Jan 2, 2014 at 1:58pm UTC
Use std::map< std::string, std::vector<std::string> > dictionary;
Not std::multimap< std::string, std::string > dictionary ;
Definitely not: std::map< std::string, std::tuple<std::string,std::string,std::string> > dictionary ;
Jan 2, 2014 at 2:12pm UTC
@ JLBorges: since I was the one who brought up std::multimap , I'd be thankful if you gave an explanation as to why you're against it.
Jan 2, 2014 at 2:31pm UTC
You should use
std::multimap when you have keys that need not be unique, not when you want to associate more than one value with a single (unique) key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <map>
#include <string>
int main()
{
std::multimap<std::string, std::string> map;
map.emplace("Dog" , "terrier" );
map.emplace("Dog" , "white" );
map.emplace("Dog" , "old" );
std::cout << "Keys in map: " << map.size() << '\n' ;
for (auto & mapped : map)
std::cout << mapped.first << ": " << mapped.second << '\n' ;
}
Keys in map: 3
Dog: terrier
Dog: white
Dog: old
Last edited on Jan 2, 2014 at 2:34pm UTC
Jan 2, 2014 at 2:53pm UTC
@NT3
map<string, vector<string> > myDictionary;
myDictionary["cat"] = {"siamese", "15", "3-legged", "pussy", "George"};
This doesn't work for me:
error: 'myDictionary' does not name a type|
Jan 2, 2014 at 5:46pm UTC
Solved:
my mistake, sorry.
I put the myDictionary initializzazion in the global declaration area :P
This warning i get
warning: control reaches end of non-void function [-Wreturn-type]|
In the example you linked me, you used the "auto" keyword
I don't use c++0x11. Auto is a specific c++011 version or what?
Last edited on Jan 2, 2014 at 6:45pm UTC