繁体   English   中英

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

[英]Problem with std::map and 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;
}

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

$ 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&)
$ 

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

可能有什么不对?

提前致谢。

问题是这样的:

bool operator<(_pos& other)

应该是这样的:

bool operator<(const _pos& other) const {
//             ^^^^               ^^^^^

没有第一个const ,比较的右边( b a < b )中a < b不能是const ,因为没有const ,函数可以修改它的参数。

没有第二const ,比较的左手侧( aa < b不能const ,因为没有const功能可以修改this

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


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

bool operator<(const _pos& lhs, const _pos& rhs)
{
    return lhs.xi < rhs.xi;
}

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


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

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

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

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


最后,您应该更喜欢make_pair实用程序函数来进行配对:

    m.insert(std::make_pair(k1,v1));
    m.insert(std::make_pair(k2,v2));

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

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

我认为你对operator <的定义是错误的 - 右边(本例中的参数)应该标记为const,它应该是一个const成员函数,例如

    bool operator<(const _pos& other) const{ 

            return this->xi < other.xi; 
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM