Created
April 22, 2014 21:06
-
-
Save mastbaum/11194306 to your computer and use it in GitHub Desktop.
Creating a list of keys from an STL map.
This file contains 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
/** | |
* Creating a list of keys from an STL map. | |
* | |
* Build: g++ -o keys keys.cpp | |
* Run: ./keys | |
*/ | |
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <vector> | |
// Fake ClassifierResult container for demonstration purposes | |
struct ClassifierResult { | |
int a; | |
}; | |
int main(int argc, char* argv[]) { | |
// Build a fake map of fake ClassifierResults | |
std::map<std::string, ClassifierResult> fClassifierResults; | |
fClassifierResults["foo"] = ClassifierResult(); | |
fClassifierResults["bar"] = ClassifierResult(); | |
// Create a vector of the keys | |
// Note that STL maps are generally sorted ascending by key | |
std::vector<std::string> keys; | |
std::map<std::string, ClassifierResult>::iterator it; | |
for (it=fClassifierResults.begin(); it!=fClassifierResults.end(); ++it) { | |
keys.push_back(it->first); | |
} | |
// Print it | |
for (size_t i=0; i<keys.size(); i++) { | |
std::cout << keys[i] << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment