| | |
Associate words with the STL map
Please support our C++ advertiser: Intel Parallel Studio Home
Another experiment with the Standard Template Library (STL). This time we are taking a look at map. Map is a Sorted Pair Associative Container (a mouthful). The pair is -- const Key, Data --, where Key has to be unique. It is Key that is sorted. In this code sample a simple word association is shown. The map is loaded, displayed, and searched.
// experimenting with the STL map<const Key, Data> // const Key is unique, no two elements can have the same key // building a simple associated word search // a Dev-C++ tested console application by vegaseat 29nov2004 #include <iostream> #include <map> // map, pair using namespace std; // std::cout, std::cin int main() { char thing[30]; // key word char farbe[30]; // associated color map<string,string> sM; // map<const Key, Data> map<string,string>::iterator iter; // points to elements // load the map<string,string> with things and color // could be loaded from a data file too sM.insert(pair<string,string>("rose","red")); sM.insert(pair<string,string>("sky","blue")); sM.insert(pair<string,string>("coal","black")); sM.insert(pair<string,string>("blood","red")); sM.insert(pair<string,string>("snow","white")); sM.insert(pair<string,string>("fire","red")); // list the things (key words) cout << "things to choose from:\n"; for(iter = sM.begin(); iter != sM.end(); iter++) { cout<< iter->first << endl; } cout << "Enter a thing: "; cin >> thing; // find the specified thing (unique key word) // point to the map's end if find fails iter = sM.find(thing); // point to it if (iter != sM.end()) { cout << "the " << iter->first << " is " << iter->second << endl; } else { cout << "thing " << thing << " not found!" << endl; } // you can look for the associated word (not unique) cout << "all the red things:\n"; for(iter = sM.begin(); iter != sM.end(); iter++) { if (iter->second == "red") cout << iter->first << " is " << iter->second << endl; } cin.get(); // trap return cin.get(); // wait return 0; }
Similar Threads
- hash map in STL (C++)
- map STl (C++)
- STL map? (C++)
- (was) STL <map> question (C++)
- STL set vs. map (C++)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets



