STL Maps Iterator Usage Question

I have a map,

map<int, map<int, map<int, int> > > color_map;

that I am putting color counts in. The structure is as follows:

color_map[blue][green][red]

Which gives me the amount of pixels seen that have that color. This works great but is not what I really want to do. I don't want to expand my map if that color doesn't exist in the image, but I don't know how it set up an iterator for the map. So how would I?
Last edited on
map<int, map<int, map<int, int> > >::const_iterator it_blue;
map<int, map<int, int> >::const_iterator it_green;
map<int, int>::const_iterator it_red;
Or
1
2
3
4
5
6
struct rgb{
  unsigned char color[3]; //or r,g,b if you prefer.
  bool operator<(const rgb &B) const; //or an extern compare class
};

map<rgb, int> color_map;
@EricDu

Ok I see so:

1
2
3
4
5
6
7
map<int, map<int, map<int, int> > >::iterator it1;
map<int, map<int, int> >::iterator it2;
map<int, int>::iterator it3;

it1 = color_map.begin();
it2 = it1->second.begin();
it3 = it2->second.begin();


would do it for me. Oh and why are you using const_iterator instead?

@ne555

I would really like to know how that second part if that struct works, It would make things so much simpler if I knew what was going on there. The first part I actually have in my code as:

1
2
3
4
struct pixel
{
    unsigned char values[3];
};


Oh and btw I am working with bitmaps so they are read from the file as bgr, not that it matters much.
Last edited on
¿what do you mean with 'second part'?
To access one element color_map[ pixel(b,g,r) ];
To traverse it
1
2
3
4
for( map<pixel,int>::const_iterator it = color_map.begin(); it!=color_map.end(); ++it){
  // it->first is a pixel
  // it->second is the count
}


const_iterator if you would not modify the map (the value field, you can't touch the key)
I guess I should have explained a little more. By "second part" I was referring to:

bool operator<(const rgb &B) const;

I do understand what is supposed to do but I have no ideal how it does it.
Actually the function exists in order to use the map.
An easy way will be comparing component by component, like you were doing in the original post (first blue, then green and last red)
Topic archived. No new replies allowed.