2009-10-28 45 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