Created
September 20, 2012 21:10
-
-
Save fpersson/3758371 to your computer and use it in GitHub Desktop.
Simple with std::map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <map> | |
enum TMemoryValue{YES=1, NO, MAYBE, UNKNOWN}; | |
int main () { | |
std::map<std::string, TMemoryValue> memorymap; | |
std::map<std::string, TMemoryValue>::iterator it; | |
memorymap.insert(std::pair<std::string, TMemoryValue>("Peasant", UNKNOWN)); | |
memorymap.insert(std::pair<std::string, TMemoryValue>("Woodcutter", UNKNOWN)); | |
memorymap.insert(std::pair<std::string, TMemoryValue>("Stonemasson", UNKNOWN)); | |
memorymap.insert(std::pair<std::string, TMemoryValue>("Blacksmith", UNKNOWN)); | |
memorymap.insert(std::pair<std::string, TMemoryValue>("WoodWalls", UNKNOWN)); | |
//memorymap["Peasant"]=YES; | |
(*memorymap.find("Peasant")).second = YES; //don't create Peasant if it dont exist... | |
if((memorymap.find("Peasant") != memorymap.end()) && ((*memorymap.find("Peasant")).second == YES)){ | |
(*memorymap.find("Woodcutter")).second = MAYBE; | |
(*memorymap.find("Stonemasson")).second = MAYBE; | |
} | |
for(it=memorymap.begin(); it != memorymap.end(); it++){ | |
std::cout << "Object: " << (*it).first << " (" << (*it).second << ")" << std::endl; | |
} | |
return 0; | |
} |
memorymap.insert(std::make_pair("Peasant", UNKNOWN));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Du kan använda std::make_pair för att korta ner koden.