C++/Map Multimap/map indexer — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Текущая версия на 13:25, 25 мая 2010
Map Index Operator
<source lang="cpp">
- include <map>
- include <iostream>
using namespace std; class Data { public:
Data(int val = 0) { mVal = val; } int getVal() const { return mVal; } void setVal(int val) {mVal = val; }
protected:
int mVal;
}; int main(int argc, char** argv) {
map<int, Data> dataMap; dataMap[1] = Data(4); dataMap[1] = Data(6); return (0);
}
</source>
use map as an array
<source lang="cpp">
- include <algorithm>
- include <iostream>
- include <map>
- include <utility>
using namespace std; int main( ) {
const char* word[] = { "A", "B", "C", "D","E" }; const char* clue[] = { "a", "b","c", "d","e" }; map<string,string> dictionary1; transform( word, word+sizeof(word)/sizeof(word[0]), clue,inserter( dictionary1, dictionary1.end() ),make_pair<string,string> ); cout << "There are " << dictionary1.size() << " words in the dictionary\n\n"; cout << "The clue for POPS is \"" << dictionary1["POPS"] << "\"\n" << "The clue for TAT is \"" << dictionary1["TAT"] << "\"\n" << "The clue for ESPO0 is \"" << dictionary1["ESPO0"] << "\"\n";
}
</source>