1
我已阅读this问题,它对我没有帮助。使用比较函数设置密钥类型会导致运行时错误
我的问题是:为set
使用如下的密钥类型的比较函数时,为什么会出现运行时错误?
multiset<Phone, decltype(comp)*> phones { Phone(911), Phone(112) };
//^^^^^^^^^^^^^^^
在VC2013它给了我这个上面的代码:
Unhandled exception at 0x73DECB49 in debugit.exe: 0xC0000005: Access violation executing location 0x00000000.
这里是产生误差的一个小例子:
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
struct Phone {
Phone(long long const &num) : number{num} {}
long long number;
};
// compare function:
bool comp(Phone const &n1, Phone const &n2) { return n1.number < n2.number; }
int main()
{ // The below line produces the runtime error.
multiset<Phone, decltype(comp)*> phones { Phone(911), Phone(112) };
}
我什么也看不见我在这里做错了。我用VC2013和g ++(GCC)4.9.1编译都导致相同。
[在C++初始化用定制的比较函数多重集]的可能重复(http://stackoverflow.com/questions/18718379 /初始化 - multiset与自定义比较功能在c) – NathanOliver
你需要给它一个'decltype(comp)*'的实例。例如。 '电话({...},&comp)'。 –
请勿使用整数表示电话号码。这对我的(以及我国大部分的数字)都会失败。 –