문제

나는 특별한 방식으로 데이터를 구성하기 위해 일부 후프를 뛰어 넘 으려고 노력하고 있습니다. 나는 내 고통을 보여주는 단순화 된 코드를 포함하고 있습니다.

부스트를 사용할 수 없습니다. 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 개의 간단한 맵, 해당지도의 반복자 및 첫 번째 맵의 첫 번째 인수가있는 다른 맵이 있습니다.

이것으로부터 분명합니다.반복자를지도에 넣을 수없는 이유는 무엇입니까?두 번째 인수로 반복자를 가질 수 있습니다. 그러나 위에 표시된 방식은 다음을 제공합니다.

$ 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

"여기에서 Instantanted"는 아무 말도하지 않으며 웹 검색은 이것에 대한 정보를 제공하지 않습니다.

STL : MAP는 단순히 이것을 허용하지 않습니까? 이 문제를 해결하기 위해 내 앱을 리코드 할 수 있지만 매우 비효율적 이며이 작업을 수행하고 싶습니다. 내가 사용할 수있는지도 요소를 위해 만들 수있는 또 다른 종류의 포인터가 있습니까?

시간 내 줘서 고마워.

도움이 되었습니까?

해결책

당신은 이것을 할 수 없습니다 std::map 반복자는 임의의 액세스 반복자가 아니므로 비슷하지 않습니다. <.

대신 첫 번째 맵에서 value_type에 포인터를 맵 키로 사용할 수 있습니다.

다른 팁

오류 메시지를 읽는 법을 배워야합니다. 특히 긴 윈드 설명 이후에 오는 메시지를 살펴보십시오. 어디 오류가 발생했습니다.

/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<. 또한 유형이 다를 수 있기 때문에 두 가지 반복자 유형을 사용했습니다.iterator 그리고 const_iterator)

map<Key, Value>

그만큼 map iterator 다른 요소에 대한 핵심 요소로 map 가능하지 않습니다 map 기대합니다 operator < 기본적으로 키로 정의됩니다. 만약 Key (이 경우 map iterator)는 정의되지 않으면 unfunctor를 키 (MAP Ierator)의 비교를 제공하는 술어 함수로 전달해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top