[英]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.