题
我正试图通过一些箍来以特殊的方式组织数据。我包含了一段简化的代码,证明了我的痛苦。
我不能使用提升。 我在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;
}
正如你所看到的,我有一个简单的映射,一个映射到该映射的迭代器和另一个映射,它有第一个参数作为第一个映射的迭代器。
从中可以清楚地看出: 为什么我不能在地图中放置迭代器? 我可以将迭代器作为第二个参数。但是,上面显示的方式提供了这个:
$ 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只是不允许这样吗?我可以重新编写我的应用程序来解决这个问题,但效率非常低,我希望能够解决这个问题。我可以使用另一种指针来制作我可以使用的地图元素吗?
感谢您的时间。
解决方案
你不能这样做,因为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
作为另一个operator <
的关键元素是不可能的,因为Key
期望map iterator
默认定义为键。如果未定义<=>(在本例中为<=>),则需要将仿函数作为谓词函数传递,该函数提供Key的比较(map iterator)。