2009-10-28 90 views
2

我试图通过一些特殊的方式来组织数据。我加入了一段简单的代码,证明了我的痛苦。C++ STL:通过迭代器将地图搜索映射到另一个地图

我不能使用提升。 我在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; 
} 

因此,大家可以看到,我有1个简单的地图,一个迭代到地图和其它具有第一个参数是一个迭代的第一张地图的地图。

由此可见: 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:地图根本不允许这个吗?我可以重新编码我的应用程序来解决这个问题,但效率很低,我希望能够实现这个功能。是否有另一种类型的指针可以用于我可以使用的地图元素?

谢谢你的时间。

回答

4

你不能这样做,因为std::map迭代器不是随机访问迭代器,所以不能与<进行比较。

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

+0

这将是一个简单的解决方案。指针作为关键的地图似乎非常快。谢谢。 – Travis 2009-10-28 08:17:25

0
map<Key, Value> 

mapiterator作为关键元件到另一个map是不可能的,因为预计mapoperator <默认到键来定义。如果Key(在这种情况下为map iterator)未定义,那么您需要传递一个函数作为提供Key(映射迭代器)比较的谓词函数。

3

你必须学会​​阅读错误信息。在长篇大论的描述其中错误发生后到来的消息特定的外观:

/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<。我还使用了两种迭代器类型,因为它们可能类型不同(iteratorconst_iterator

+0

是的,在阅读了这里留下的评论之后,错误信息给了我更多的信息。你在这里的比较功能可能是我要去的方式,但我需要做一些测试(主要是为了确保我明白我在做什么)。感谢您的好评。 – Travis 2009-10-28 08:12:10

+0

然而,有一个原因,运算符<未定义。这不是一个随机迭代器,所以迭代器本身无法进行比较......如果您只是取消引用它们进行比较,为什么不实际使用相同的密钥? – 2009-10-28 09:06:59