【问题标题】:C++ STL:map search by iterator to another mapC ++ STL:通过迭代器将地图搜索到另一个地图
【发布时间】:2009-10-28 07:33:11
【问题描述】:

我试图跳过一些障碍以一种特殊的方式组织数据。我包含了一段简化的代码来展示我的痛苦。

我无法使用增强功能。 我在 cygwin 中使用的是最新版本的 g++。

#include <iostream>
#include <map>

using namespace std;

int main () {

    map< int,int > genmap;
    map< int,int >::iterator genmapit;
    map< map<int,int>::iterator,int > itermap;

    // insert something into genmap
    genmap.insert (make_pair(1,500) );

    // find and return iterator.
    genmapit=genmap.find(1);

    // insert the iterator/int into itermap. Dies on each of the following 3 versions of this line.
    //itermap[genmapit] = 600; // crash
    //itermap.insert ( pair< map<int,int>::iterator,int >(genmapit,600) ); // crash
    itermap.insert ( make_pair(genmapit,600) ); // crash

    return 0;
}

如您所见,我有一个简单的映射,一个指向该映射的迭代器,另一个映射具有第一个参数作为第一个映射的迭代器。

这很明显: Why can't I put an iterator in map? 我可以有一个迭代器作为第二个参数。但是,上面显示的方式提供了这一点:

$ make
g++    -c -o main.o main.cpp
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h: In member fun
ction `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp =
 std::_Rb_tree_iterator<std::pair<const int, int> >]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h:871:   instantiate
d from `std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _All
oc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::i
nsert_unique(const _Val&) [with _Key = std::_Rb_tree_iterator<std::pair<const in
t, int> >, _Val = std::pair<const std::_Rb_tree_iterator<std::pair<const int, in
t> >, int>, _KeyOfValue = std::_Select1st<std::pair<const std::_Rb_tree_iterator
<std::pair<const int, int> >, int> >, _Compare = std::less<std::_Rb_tree_iterato
r<std::pair<const int, int> > >, _Alloc = std::allocator<std::pair<const std::_R
b_tree_iterator<std::pair<const int, int> >, int> >]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_map.h:360:   instantiated
 from `std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_
Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator, bool> std::
map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [wit
h _Key = std::_Rb_tree_iterator<std::pair<const int, int> >, _Tp = int, _Compare
 = std::less<std::_Rb_tree_iterator<std::pair<const int, int> > >, _Alloc = std:
:allocator<std::pair<const std::_Rb_tree_iterator<std::pair<const int, int> >, i
nt> >]'
main.cpp:23:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:227: error: no
 match for 'operator<' in '__x < __y'
make: *** [main.o] Error 1

“从这里实例化”什么也没告诉我,网络搜索也没有给我任何信息。

STL:map 是否根本不允许这样做?我可以重新编码我的应用程序来解决这个问题,但这会非常低效,我想让它工作。我可以为我可以使用的地图元素制作另一种指针吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: c++ map iterator


    【解决方案1】:

    您不能这样做,因为 std::map 迭代器不是随机访问迭代器,因此无法与 &lt; 进行比较。

    相反,您可以使用指向第一个映射中的 value_type 的指针作为映射键。

    【讨论】:

    • 这将是一个简单的解决方案。以指针为键的映射似乎非常快。谢谢。
    【解决方案2】:

    你必须学会​​阅读错误信息。尤其是在冗长的描述在哪里错误发生之后出现的消息:

    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y'

    地图迭代器无法与地图默认使用的小于运算符进行比较。

    我想你可以提供一个比较函数来比较迭代器指向的对,因为迭代器本身不能以有意义的方式轻松比较。

    struct CompareIterator
    {
         template <class FirstIter, class SecondIter>
         bool operator()(FirstIter lhv, SecondIter rhv) const
         {
             return *lhv < *rhv;
         }
    };
    
    //usage with map:
    map< map<int,int>::iterator,int, CompareIterator > itermap;
    

    std::pair 定义operator&lt;。我还使用了两种迭代器类型,因为类型可能不同(iteratorconst_iterator

    【讨论】:

    • 是的,在阅读了这里留下的 cmets 后,错误消息对我来说意义重大。你在这里的比较功能可能是我要走的路,但我需要做一些测试(主要是为了确保我理解我在做什么)。感谢您的精彩评论。
    • 但是没有定义 operator
    【解决方案3】:
    map<Key, Value>
    

    map iterator 作为另一个map 的关键元素是不可能的,因为map 期望operator &lt; 默认定义为关键。如果未定义 Key(在本例中为 map iterator),则需要将仿函数作为谓词函数传递,以提供 Key(映射迭代器)的比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 2016-06-28
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多