PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : C++ multimaps


Gast
2006-01-13, 21:44:51
Hi, ich habe ein einfaches key-value schema.


typedef multimap<string,string> str_str_mmap;
typedef multimap<string,str_str_mmap> str_mmap_mmap;

str_str_mmap current_map;
str_mmap_mmap big_map;
big_map.insert(make_pair(string("global"), current_map));

current_map.insert(make_pair(string("schluessel"), string("wert")));
big_map.insert(make_pair(label, current_map));


Soweit so gut, das funktoniert alles. Jetzt muss ich das nur noch in einer Schleife durchgehen.

Z.B. so irgendwie (Pseudocode):

for (i=0; i < big_map.size(); i++) {
cout << big_map[i].key << ": ";
temp_map = big_map[i].value;
cout << temp_map.key << " = ";
cout << temp_map.value << endl;
}


Aber mit den iteratoren komme ich da nicht vorwärts. Kann mir jemand sagen, wie das geht?

Trap
2006-01-13, 22:01:33
Allgemein geht es so:

typedef XYZ itertype;

for(itertype it = container.begin();it!=container.end();++it){
cout << it->key << ": ";
temp_map = it->value;
cout << temp_map.key << " = ";
cout << temp_map.value << endl;
}

Gast
2006-01-13, 22:22:04
Ich bin mal so nett, hab nur schnell ne std::map draus gemacht für das praktische insterten per opeartor [], kannst aber auch mit multimap machen.

typedef std::map<std::string,std::string> str_str_mmap;
typedef std::map<std::string,str_str_mmap> str_mmap_mmap;

int main()
{
str_mmap_mmap map;

map["Tolle Dinge"]["Rofl"] = "Rolling on floor laughing";
map["Tolle Dinge"]["LOL"] = "Laughing out loudly";
map["Tolle Dinge"]["WTF"] = "What the fuck";
map["Menschen"]["Hans"] = "dumm, bloed, bekloppt";
map["Menschen"]["Bush"] = "loves Krieg";

for(str_mmap_mmap::iterator a = map.begin(); a != map.end(); ++a) {
for(str_str_mmap::iterator b = a->second.begin(); b != a->second.end(); ++b) {
std::cout << "map[" << a->first << "]["
<< b->first << "] = '" << b->second << "'\n";
}
}

}

Gast
2006-01-13, 23:01:46
das hat mir echt geholfen, danke!!

Gast
2006-01-13, 23:26:23
also das ansprechen über den namensstring klappt einfach nicht.

Wenn ich es so mache:


cout << big_map[string("mein label")][string("meinkey")];


kommt das beim kompilieren:


test.cpp: In function `void read_config_file()':
test.cpp:66: Fehler: no match für »operator[]« in »big_map[string(((const char*)"global"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>()))))]«


[] Ansprechung scheint mit multimaps nicht zu klappen -> schade ...

Gast
2006-01-14, 12:55:40
Nein das geht natürlich nicht, drum hab ich in meinem Beispiel ja schnell ne map draus gemacht. Woher soll man auch wissen ob du jetzt ein neues Element mit dem Key einfügen willst, oder ob du eines bearbeiten willst, und wenn dann welches? :)