std :: map和std :: pair的问题

Tom*_*Tom 9 c++ stl operator-overloading stdmap std-pair

我有一个小程序,我想执行测试

#include <map>
#include <iostream>
using namespace std;

struct _pos{
        float xi;
        float xf;

        bool operator<(_pos& other){

                return this->xi < other.xi;
        }
};

struct _val{

        float f;
};

int main()
{
        map<_pos,_val> m;

        struct  _pos k1 = {0,10};
        struct  _pos k2 = {10,15};

        struct  _val v1 = {5.5};
        struct  _val v2 = {12.3};                                                                   

        m.insert(std::pair<_pos,_val>(k1,v1));
        m.insert(std::pair<_pos,_val>(k2,v2));

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试编译它时,我收到以下错误

$ g++ m2.cpp -o mtest
In file included from /usr/include/c++/4.4/bits/stl_tree.h:64,
                 from /usr/include/c++/4.4/map:60,
                 from m2.cpp:1:
/usr/include/c++/4.4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = _pos]’:
/usr/include/c++/4.4/bits/stl_tree.h:1170:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = _pos, _Val = std::pair<const _pos, _val>, _KeyOfValue = std::_Select1st<std::pair<const _pos, _val> >, _Compare = std::less<_pos>, _Alloc = std::allocator<std::pair<const _pos, _val> >]’
/usr/include/c++/4.4/bits/stl_map.h:500:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = _pos, _Tp = _val, _Compare = std::less<_pos>, _Alloc = std::allocator<std::pair<const _pos, _val> >]’
m2.cpp:30:   instantiated from here
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
m2.cpp:9: note: candidates are: bool _pos::operator<(_pos&)
$ 
Run Code Online (Sandbox Code Playgroud)

我认为在密钥上声明操作符<可以解决问题,但它仍然存在.

可能有什么不对?

提前致谢.

GMa*_*ckG 25

问题是这样的:

bool operator<(_pos& other)
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

bool operator<(const _pos& other) const {
//             ^^^^               ^^^^^
Run Code Online (Sandbox Code Playgroud)

没有第一个const,比较的右侧(bin a < b)不能const,因为没有const该函数可能会修改其参数.

没有第二个const,左边的比较(ain a < b)不能const,因为没有const这个功能可能会修改this.

在内部,地图的关键始终是const.


应该注意,您应该更喜欢使用非成员函数.也就是说,更好的是自由功能:

bool operator<(const _pos& lhs, const _pos& rhs)
{
    return lhs.xi < rhs.xi;
}
Run Code Online (Sandbox Code Playgroud)

与您的类在同一名称空间中.(对于我们的例子,就在它下面.)


顺便说一句,在C++中,不需要为结构类型变量的声明添加前缀struct.这是完美的,首选:

    _pos k1 = {0,10};
    _pos k2 = {10,15};

    _val v1 = {5.5};
    _val v2 = {12.3};
Run Code Online (Sandbox Code Playgroud)

(虽然你的类型名称是以非正统的方式命名的.:P)


最后,您应该更喜欢make_pair用于制作对的效用函数:

    m.insert(std::make_pair(k1,v1));
    m.insert(std::make_pair(k2,v2));
Run Code Online (Sandbox Code Playgroud)

它使您不必写出对的类型,并且通常更容易阅读.(特别是当更长的类型名称出现时.)


Nav*_*een 5

小于运算符的签名需要是bool operator<(const _pos& other) const,否则 map 不能在 const 函数中使用这个运算符,因为这个成员函数被声明为非常量。