Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++ 插入到地图中未正确排序_C++_Dictionary - Fatal编程技术网

C++ 插入到地图中未正确排序

C++ 插入到地图中未正确排序,c++,dictionary,C++,Dictionary,我不明白为什么这些记录没有在地图上分类 class point{ public: string s1,s2; public: point(string string1, string string2){ s1=string1;s2=string2; } }; bool operator<(const point &p1, const point &p2) { cout<<"p1.s1.c

我不明白为什么这些记录没有在地图上分类

 class point{
 public:    string s1,s2;
 public:        point(string string1, string string2){
            s1=string1;s2=string2;
    }

 };

 bool operator<(const point &p1, const point &p2)
 {
    cout<<"p1.s1.compare(p2.s1)="<<p1.s1.compare(p2.s1)<<" p1.s1="<<p1.s1<<"  p2.s1="<<p2.s1<<endl;
    return p1.s1.compare(p2.s1);
 }
 int main(int argc, char *argv[])
 {
    std::map<point,int>m1;
    point p1("hello","hi");
    m1.insert(std::make_pair(p1,1));
    point p2("abc","kbd");
    m1.insert(std::make_pair(p2,2));
    point p3("hell","hi");
    m1.insert(std::make_pair(p3,3));

    std::map<point,int>::iterator it=m1.begin();
    while(it!=m1.end())
    {
        cout<<"m1.first="<<it->first.s1<<"  m1.first.s2="<<it->first.s2<<"  m1.second="<<it->second<<endl;
        it++;
    }
    return 0;
 }
但预期的产出是

m1.first=abc  m1.first.s2=kbd  m1.second=2
m1.first=hell  m1.first.s2=hi  m1.second=3
m1.first=hello  m1.first.s2=hi  m1.second=1

任何人都可以澄清一下,这是插入在RB树中的工作方式,还是存在其他一些问题。

操作符我认为string::compare()返回一个int值。充其量,这将被隐式转换为bool。如果处理不正确,可能会弄乱less运算符

我想string::compare()会返回一个int值。充其量,这将被隐式转换为bool。如果处理不正确,可能会弄乱less运算符

为什么希望得到该输出?它应该按什么排序?为什么?比较需要发送到映射构造函数,并且它需要是一个返回true的二进制谓词。为什么希望得到该输出?它应该按什么排序?为什么?比较需要发送到映射构造函数,并且它需要是一个返回true的二进制谓词。
m1.first=abc  m1.first.s2=kbd  m1.second=2
m1.first=hell  m1.first.s2=hi  m1.second=3
m1.first=hello  m1.first.s2=hi  m1.second=1
return (p1.s1.compare(p2.s1) < 0 );
return (p1.s1 < p2.s1);