【问题标题】:No match for operator with map.find()与 map.find() 的运算符不匹配
【发布时间】:2014-02-05 18:39:42
【问题描述】:

我有一张这样设置的地图。

vector < map <RGB, int> > count;

使用这样的迭代器:

vector < map <RGB, int> >::iterator it;

这行得通:

count[min_distance_index].find(img[i]);

这不是(它说不匹配运算符 =):

it = count[min_distance_index].find(img[i]);

有什么想法吗? RGB 只是一个结构体,我自己定义了

【问题讨论】:

  • std::map::find 不返回 std::vector::iterator。事实上,我从 Clang 中得到了这个:error: no可行的转换从 'iterator' (aka '__map_iterator') 到 'std::vector<:map int> >::iterator'(又名 '__wrap_iter') 使用 std::map&lt;int, int&gt; 而不是 &lt;RGB, int&gt; 进行测试时。
  • 它返回一个std::map&lt;RGB, int&gt;::iterator
  • 好的。那么我怎么才能找到这种意义上的东西呢?
  • 使用auto it = count[min_distance_index].find(img[i]); (C++11)
  • 那不是“地图”。那是地图的向量。

标签: c++ vector map iterator


【解决方案1】:

这段代码: count[min_distance_index].find(img[i]); 你在这里做的是:

vector &lt; map &lt;RGB, int&gt; &gt;[] 会给你一个map&lt;RGB, int&gt; 然后你打电话给map&lt;RGB, int&gt;.find()。 根据本站:http://www.cplusplus.com/reference/map/map/find/

元素的迭代器,如果找到具有指定键的元素, 或 map::end 否则。

如果地图对象是 const 限定的,则函数返回一个 常量迭代器。否则,它返回一个迭代器。

成员类型 iterator 和 const_iterator 是双向迭代器 指向元素的类型(value_type 类型)。请注意 map 容器中的 value_type 是 pair 的别名。

map&lt;RGB, int&gt;.find() 会给你一个map&lt;RGB, int&gt;::iterator 而不是vector&lt;map&lt;RGB,int&gt; &gt;::iterator

【讨论】:

    【解决方案2】:

    试试

    map<R G B, int> ::const_iterator it;
    

    vector[] 将返回对 map 的引用

    【讨论】:

    • 看看他把.find应用到什么地方了。
    • count 是一个向量,但 count[min_distance_index] 是对 map 的引用。对吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多