Skip to content

Instantly share code, notes, and snippets.

@crmne
Created October 16, 2010 22:32
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 crmne/630346 to your computer and use it in GitHub Desktop.
Save crmne/630346 to your computer and use it in GitHub Desktop.
This is how you can insert elements in a container, without repetition, and obtaining unique ids. Useful for inserting vertexes in Boost.Graph. See http://stackoverflow.com/questions/3950841
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main (int argc, char const *argv[])
{
string array[] = { "zero", "one", "one", "zero", "two", "three", "zero" };
map<string, unsigned int> numbers;
for(size_t i = 0; i < 7; ++i)
{
pair<string, unsigned int> p(array[i], numbers.size());
int id = numbers.insert(p).first->second;
cout << array[i] << "\t" << id << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment