简体   繁体   English

std :: map使用。 这是怎么了 核心转储? 我没有正确做些什么?

[英]std::map use. What is wrong here ? core dump ? What I do not do correctly?

What is wrong here ? 这是怎么了 core dump ? 核心转储? What I do not do correctly? 我没有正确做些什么?

#include <cstdio>
#include <map>
#include <cmath>
#include <cstring>
#include <iostream>

bool compFloats (const float f1, const float f2){
  return floor (f1) < floor (f2);
};

int main (int argc, char **argv){

  std::map < float, char, decltype (&compFloats) > m;


  m.emplace (3.9, 'a');
  m.emplace (3.1, 'b');
  m.emplace (4.1, 'c');
  m.emplace (4.5, 'd');
  m.emplace (5.2, 'e');

  for (auto i = m.begin (); i != m.end (); i++)
    printf ("%c ", i->second);
  printf ("\n");


  printf ("hello world\n");

  return 0;
}

Starting with the assumption that your custom-built comparison operator is necessary for this code to function, the issue you're facing is that the argument being passed to the map template, decltype (&compFloats) , doesn't describe the specific function you want to use as a comparator, but rather the typename of that function. 从假设您需要使用自定义的比较运算符来使此代码正常运行开始,您面临的问题是传递给地图模板decltype (&compFloats)未描述您想要的特定函数用作比较器,而是该函数的类型名。 As a result, when the map attempts to perform comparisons, it doesn't have any concrete function to use, and probably dereferences a null pointer somewhere, causing the core dump. 结果,当映射尝试执行比较时,它没有任何具体功能要使用,并且可能在某处取消引用空指针,从而导致核心转储。

If you actually need this kind of comparison (where the floating point values are being rounded down to the nearest integer before being saved), you'll need to define the map like this instead, using a local struct that can compare float s together: 如果您确实需要这种比较(在保存之前将浮点值四舍五入到最接近的整数),则需要使用可以将float一起比较的本地struct来定义这样的映射:

struct CompFloat {
    bool operator()(float a, float b) const {
        return floor(a) < floor(b);
    }
};

int main() {
    std::map<float, char, CompFloat> m;
    //...
}

Alternatively, if what you need is just to use a std::map with float s as the key, and the truncation is immaterial (or unnecessary/unhelpful), just write this instead. 另外,如果您只需要使用以float为键的std::map ,并且截断是无关紧要的(或不必要/无益的),则只需编写此代码即可。

int main() {
    std::map<float, char> m;
    //...
}

This will do what you probably expect of it. 这将完成您可能期望的操作。

Based on the preliminary indications of @Xirema I tried an "expected syntax" 根据@Xirema的初步迹象,我尝试了“预期语法”

instead of: 代替:

std::map < float, char, decltype (&compFloats) > m;

I initialized it as: 我将其初始化为:

std::map<float,char,decltype(&floatComp)> m(&floatComp);

I don't know where in the documents it is written but now works correctly as expected !!! 我不知道它写在文档中的什么地方,但是现在可以正常工作了!!!

The code to understand better the difference between class and object of a map follows by defining two maps with the same type but different comparison functions. 为了更好地理解映射的类和对象之间的区别,下面的代码是定义两个类型相同但比较函数不同的映射。

#include <cstdio>
#include <map>
#include <cmath>
#include <cstring>
#include <iostream>

bool comp1(const float f1,const float f2)   {return floor(f1)<floor(f2);}
bool comp2(const float f1,const float f2) {return f1-floor(f1)<f2-floor(f2);}

typedef bool (*comp_t)(const float,const float);
typedef std::map<float,char,comp_t> M;

int main(int argc, char **argv)
{   
        M m1(&comp1);
        M m2(&comp2);

        m1.emplace(3.9,'a');m2.emplace(3.9,'a');
        m1.emplace(3.1,'b');m2.emplace(3.1,'b');
        m1.emplace(4.1,'c');m2.emplace(4.1,'c');
        m1.emplace(4.5,'d');m2.emplace(4.5,'d');
        m1.emplace(5.2,'e');m2.emplace(5.2,'e');

    for (auto i=m1.begin();i!=m1.end();i++)
        printf("%c ",i->second);
    printf("\n");

    for (auto i=m2.begin();i!=m2.end();i++)
        printf("%c ",i->second);
    printf("\n");

    return 0;
}

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

相关问题 我必须在哪些功能中使用 std::? - In what functions do I got to use std::? 我在这里使用std :: forward或std :: move吗? - Do I use std::forward or std::move here? 在使用 AWS IoT Core 运行 C++ 脚本时,我收到了一个致命错误。 我对 C++ 和 IoT Core 比较陌生,但我不明白这里有什么问题 - Upon running a C++ script with AWS IoT Core, I get a fatal error. I am relatively new to C++ and IoT Core but I do not understand what is wrong here 使用std :: placeholder需要哪些库? - What libraries do I need to use std::placeholders? 在什么情况下我必须使用 std::function? - In what situation do I have to use a std::function? 我们在std :: map或std :: set中进行哪种排序? - What kind of sorting do we have in std::map or std::set? 我是否正确使用了move语义? 有什么好处? - Do I use the move semantic correctly? What would be the benefit? 什么是标准::map<k,v> ::map; 以及在实现/使用 stl 容器和函数时如何知道使用什么命名空间?</k,v> - What is std::map<K,V>::map; and how do you know what namespace to use when implementing/using stl containers and functions? 我是否在这里正确使用 atomic&lt;&gt; over shared memory - Do I use atomic<> over shared memory correctly here std :: map有什么用呢? - What is this use of std::map doing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM