Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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的比较器函数中的seg故障/未定义行为_C++_C++11_Segmentation Fault_Undefined Behavior - Fatal编程技术网

C++ std::map的比较器函数中的seg故障/未定义行为

C++ std::map的比较器函数中的seg故障/未定义行为,c++,c++11,segmentation-fault,undefined-behavior,C++,C++11,Segmentation Fault,Undefined Behavior,这让我今天很困惑 我很难理解为什么seg下面的代码在最后插入测试映射时会出错。使用emplace()、insert()这两种方法都能按预期工作,但使用[]运算符失败。我已经阅读了有关[]的C++文档,但是下面观察到的行为与我读到的内容不符。 我已经在GDB中进行了介绍,并注意到它在尝试比较comparator函数中的字符串时失败了 #include <iostream> #include <map> #include <iostream> class Tes

这让我今天很困惑

我很难理解为什么seg下面的代码在最后插入测试映射时会出错。使用emplace()、insert()这两种方法都能按预期工作,但使用[]运算符失败。我已经阅读了有关[]的C++文档,但是下面观察到的行为与我读到的内容不符。 我已经在GDB中进行了介绍,并注意到它在尝试比较comparator函数中的字符串时失败了

#include <iostream>
#include <map>
#include <iostream>

class Testkey {
public:
    std::string s1;
    int64_t id;
    Testkey(const char* s1_, int64_t id_): s1(s1_), id(id_) {}

    bool operator<(const Testkey& rhs) const {
        if (s1 < rhs.s1)
            return true;
        if (id < rhs.id)
            return true;
        return false;
    }
};

int main() {
    Testkey i1("69739", 748072524);
    Testkey i2("69728", 52608624);
    Testkey i3("69725", 750212380);
    Testkey i4("68988", 55027788);

    std::map<Testkey, int> test_map;
    test_map[i1] = 1;
    test_map[i2] = 2;
    test_map[i3] = 3;
    std::cout << "hmm.." << std::endl;
    test_map[i4] = 4; // seg faults here in comparator function...
    std::cout << "done" << std::endl;
    return 0;
}
#包括
#包括
#包括
类Testkey{
公众:
std::字符串s1;
int64_t id;
Testkey(const char*s1_u,int64_t id_u):s1(s1_u),id(id_u){

bool运算符您的比较函数已损坏。您可能的意思是:

bool operator<(const Testkey& rhs) const {
    if (s1 < rhs.s1)
        return true;
    if (s1 > rhs.s1)
        return false;
    if (id < rhs.id)
        return true;
    return false;
}
bool运算符(右1)
返回false;
如果(id

用于
std::map
的比较函数必须定义一组要插入或比较的对象,而您的函数没有定义,因为您的比较函数已损坏。你可能是这个意思:

bool operator<(const Testkey& rhs) const {
    if (s1 < rhs.s1)
        return true;
    if (s1 > rhs.s1)
        return false;
    if (id < rhs.id)
        return true;
    return false;
}
bool运算符(右1)
返回false;
如果(id

用于
std::map
的比较函数必须定义一个要插入或比较的对象,而您的函数不是这样的,因为
i3和您的逻辑,都是“(1,2)<(2,1)”和“(2,1)<(1,2)”都是真的。如果我误解了,请原谅我,但比较功能损坏是否会导致seg故障/UB?最糟糕的情况是,它不会把我的记录插入到地图的错误位置吗?这取决于比较函数的破坏程度。如果它如此破碎,根本无法识别任何地方,那么任何事情都可能发生。同时尝试将
i3
放在
i2
之前和
i2
放在
i3
之前不会提供任何特定的错误放置位置。(在我的STL中,插入检查以确保它不会从结尾跑完,运行不处理结束情况的代码,但是无论如何都跑完终点,并进入界外。)@ SHIKIN -FYI,对于Visual C++编译器,一个破碎的比较运算符会导致调试运行时出现<代码> AsStest失败。所以不,更糟糕的不仅仅是把记录放错地方。如果我误解了,请原谅我,但是一个坏的比较函数会导致seg故障/UB吗?最糟糕的情况是,它不会把我的记录插入到地图的错误位置吗?这取决于比较函数的破坏程度。如果它如此破碎,根本无法识别任何地方,那么任何事情都可能发生。同时尝试将
i3
放在
i2
之前和
i2
放在
i3
之前不会提供任何特定的错误放置位置。(在我的STL中,插入检查以确保它不会从结尾跑完,运行不处理结束情况的代码,但是无论如何都跑完终点,并进入界外。)@ SHIKIN -FYI,对于Visual C++编译器,一个破碎的比较运算符会导致调试运行时出现<代码> AsStest失败。因此,不,更糟糕的不仅仅是把记录放错地方。