map的begin迭代器无法使用c++



我们有map:

std::map<double, COLORREF> colorset;

这里我提供了value 返回colorref的部分函数

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;
}

但是,编译器给出了一个错误,说明在colorset.begin()中不存在从tree_const_iteratortree_iterator的转换。

如果我从函数中删除const项,一切都可以,但我必须将函数声明为const。

为什么会出现此错误?或者有人能提供另一种方法来遍历映射吗?

使用const_iterator:

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

PS

我会先使用ii->等

最新更新