Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::map和std::pair存在问题_C++_Stl_Operator Overloading_Stdmap_Std Pair - Fatal编程技术网

C++ std::map和std::pair存在问题

C++ std::map和std::pair存在问题,c++,stl,operator-overloading,stdmap,std-pair,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; } }; struc

我想执行一个小程序来测试一些东西

#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;
}
#包括
#包括
使用名称空间std;
结构位置{
浮动席;
浮动xf;
布尔运算器
}
};
结构{
浮动f;
};
int main()
{
地图m;
结构_posk1={0,10};
结构_posk2={10,15};
结构_valv1={5.5};
结构_valv2={12.3};
m、 插入(标准::对(k1,v1));
m、 插入(标准::对(k2,v2));
返回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&)
$ 
$g++m2.cpp-o mtest
在/usr/include/c++/4.4/bits/stl_tree.h:64中包含的文件中,
从/usr/include/c++/4.4/map:60,
从m2.cpp:1:
/usr/include/c++/4.4/bits/stl_function.h:在成员函数“bool std::less::operator()(const _Tp&,const _Tp&)const[with _Tp=_pos]”中:
/usr/include/c++/4.4/bits/stl_tree.h:1170:从“std::pair std::_Rb_tree:::_M_insert_unique(const _Val&)[带_Key=_pos,_Val=std::pair,_KeyOfValue=std:_Select1st,_Compare=std::less,_Alloc=std::分配器]'
/usr/include/c++/4.4/bits/stl_-map.h:500:从“std::pair std::map::insert(const std::pair&)”实例化[with _Key=\u pos,_Tp=\u val,_Compare=std::less,_Alloc=std::allocator]'
m2.cpp:30:从此处实例化

/usr/include/c++/4.4/bits/stl_函数。h:230:错误:与“运算符”不匹配小于运算符的签名需要是
bool运算符问题在于:

bool operator<(_pos& other)
在与类相同的命名空间中。(对于我们的示例,就在它下面。)


顺便说一下,在C++中,不需要用<代码>结构> <代码>对Stutt类型变量的声明进行前缀。这是完美的,而且是首选:

    _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));

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

我认为您对运算符<的定义是错误的-右侧(本例中的参数)应标记为const,并且它应是一个const成员函数,例如

    bool operator<(const _pos& other) const{ 

            return this->xi < other.xi; 
    } 
<代码>操作员> }
@Tom:没问题,我已经添加了更多:在这种情况下,下标符号可能比直接使用insert更简洁:
m[k1]=v1;m[k2]=v2。我要注意,如果元素已经存在,下标操作符和插入操作具有不同的语义:下标替换它,而插入不替换它。它们一般不可互换!此外,插入与其他容器一起工作(例如,代码> SET/COME >),下标操作符对关联容器非常具体,如“代码> MAP< /COD>”。C++中所有以双下划线开头的标识符,在全局命名空间中,为实现保留一个后跟大写字母的下划线或一个后跟小写字母的下划线。标识符
\u pos
\u val
应该更改。@David Rodríguez。谢谢,我会的,记住这一点。
    m.insert(std::make_pair(k1,v1));
    m.insert(std::make_pair(k2,v2));
    bool operator<(const _pos& other) const{ 

            return this->xi < other.xi; 
    }