Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created April 22, 2014 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastbaum/11194306 to your computer and use it in GitHub Desktop.
Save mastbaum/11194306 to your computer and use it in GitHub Desktop.
Creating a list of keys from an STL map.
/**
* 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