Skip to content

Instantly share code, notes, and snippets.

@jirihnidek
Created April 2, 2014 11:59
Show Gist options
  • Save jirihnidek/9932711 to your computer and use it in GitHub Desktop.
Save jirihnidek/9932711 to your computer and use it in GitHub Desktop.
Pointer as key for C++ map
#include <iostream>
#include <map>
// Compile: g++ map_example.cc
// Run: ./a.out
int main(void)
{
std::map<unsigned char*, int> paleta;
unsigned char rgb1[3], rgb2[3];
rgb1[0] = 100;
rgb1[1] = 200;
rgb1[2] = 0;
rgb2[0] = 0;
rgb2[1] = 0;
rgb2[2] = 255;
paleta[rgb1] = 100;
paleta[rgb2] = 55;
std::cout << paleta[rgb1] << ", " << paleta[rgb2] << std::endl;
for( std::map<unsigned char*, int>::iterator ii=paleta.begin(); ii!=paleta.end(); ++ii)
{
std::cout << "(" << (int)(*ii).first[0] << ", "
<< (int)(*ii).first[1] << ", "
<< (int)(*ii).first[2] << "): " << (*ii).second << std::endl;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment