Nurlan
Nurlan

Reputation: 2960

begin iterator of map not working c++

We have map:

std::map<double, COLORREF> colorset;

Here I provide part of function which returns colorref by value

COLORREF GetColour(double value) const
{
   ...
   for(std::map<double, COLORREF>::iterator ii=colorset.begin(); ii!=colorset.end(); ++ii)
   {
    std::cout << (*ii).first << ": " << (*ii).second << std::endl;
   }
   ...
   return defaultColor;
}

But, compiler gives an error telling about nonexistance of converting from tree_const_iteratortotree_iterator in colorset.begin().

If I delete const term from function everything is OK, but I must declare function as const.

Why this error is appearing? Or can someone provide alternate way to iterate though map?

Upvotes: 0

Views: 1088

Answers (1)

user2672165
user2672165

Reputation: 3049

Use const_iterator:

   for(std::map<double, COLORREF>::const_iterator ii=colorset.begin(); ii!=colorset.end(); ++ii)

PS

I would use ii->first etc

Upvotes: 4

Related Questions