// std::map example // opposite words #include #include #include using namespace std; typedef map TStrStrMap; typedef pair TStrStrPair; int main(int argc, char *argv[]) { TStrStrMap tMap; tMap.insert(TStrStrPair("yes", "no")); tMap.insert(TStrStrPair("up", "down")); tMap.insert(TStrStrPair("left", "right")); tMap.insert(TStrStrPair("good", "bad")); string s; cout << "Enter word: " << endl;; cin >> s; string strValue = tMap[s]; if(strValue!="") { // Show value cout << "Opposite: " << strValue << endl; } else { TStrStrMap::iterator p; bool bFound=false; // Show key for(p = tMap.begin(); p!=tMap.end(); ++p) { string strKey; strValue = s; strKey= p->second; if( strValue == strKey) { // Return key cout << "Opposite: " << p->first << endl; bFound = true; } } // If not found opposite word if(!bFound) { cout << "Word not in map." << endl; } } return 0; }