问题描述
这是我想要编译的代码:
#include< unordered_map>
#include< unordered_set>
#include< algorithm>
#include< iostream>
#include< sstream>
#include< vector>
#include< string>
using namespace std;
命名空间_LEET_MAX_POINTS_ON_A_LINE
{
struct Point
{
int x;
int y;
Point():x(0),y(0){}
Point(int a,int b):x(a),y(b){}
}
struct SlantedLine
{
public:
int slope_numerator;
int slope_denominator;
int intercept_numerator;
int intercept_denominator;
};
struct SlantedLineHash
{
size_t operator()(const SlantedLine& k)const
{
return k.slope_numerator ^ k.slope_denominator ^ k。 intercept_numerator ^ k.intercept_denominator;
}
};
struct SlantedLineEqual
{
size_t operator()(const SlantedLine& line1,const SlantedLine& line2)const
{
return(line1.slope_numerator = = line2.slope_numerator)&& (line1.slope_denominator == line2.slope_denominator)&& (line1.intercept_numerator == line2.intercept_numerator)&& (line1.intercept_denominator == line2.intercept_denominator);
}
};
类解决方案
{
public:
int maxPoints(vector< Point& points)
{
if ()== 0)
{
return 0;
}
unordered_map< SlantedLine,unordered_set< int>,SlantedLineHash,SlantedLineEqual> slantedLineToPointsMap;
unordered_map< int,unordered_set< int>> verticalLineToPointsMap;
for(size_t i = 0; i< points.size(); i ++)
{
for(size_t j = i + 1; j< points.size(); j ++ )
{
int x1 = points [i] .x;
int y1 = points [i] .y;
int x2 = points [j] .x;
int y2 = points [j] .y;
if(x1 == x2)
{
unordered_map< int,unordered_set< int>> :: iterator probe = verticalLineToPointsMap.find(x1);
if(probe == verticalLineToPointsMap.end())
{
unordered_set< int>点数;
points.insert(i);
points.insert(j);
verticalLineToPointsMap.insert(pair< int,unordered_set< int>>(x1,points));
}
else
{
probe-> second.insert(i);
probe-> second.insert(j);
}
}
else
{
int slope_numerator = y2 - y1;
int slope_denominator = x2 - x1;
int intercept_numerator = y1 * x2 - x1 * y2;
int intercept_denominator = x2 - x1;
simplify_fraction(& slope_numerator,& slope_denominator);
simplify_fraction(& intercept_numerator,& intercept_denominator);
倾斜线;
line.slope_numerator = slope_numerator;
line.slope_denominator = slope_denominator;
line.intercept_numerator = intercept_numerator;
line.intercept_denominator = intercept_denominator;
unordered_map< SlantedLine,unordered_set< int>,SlantedLine,SlantedLineEqual> :: iterator probe = slantedLineToPointsMap.find(line);
if(probe == slantedLineToPointsMap.end())
{
unordered_set< int>点数;
points.insert(i);
points.insert(j);
slantedLineToPointsMap.insert(pair< SlantedLine,unordered_set< int>>(line,points));
}
else
{
probe-> second.insert(i);
probe-> second.insert(j);
}
}
}
}
size_t maxPoints = 0;
for(unordered_map< int,unordered_set< int>> :: iterator i = verticalLineToPointsMap.begin(); i!= verticalLineToPointsMap.end(); i ++)
{
maxPoints = max (maxPoints,i-> second.size());
}
for(unordered_map< SlantedLine,unordered_set< int>,SlantedLine,SlantedLineEqual> :: iterator i = slantedLineToPointsMap.begin(); i!= slantedLineToPointsMap.end(); i ++)
{
maxPoints = max(maxPoints,i-> second.size());
}
return maxPoints;
}
private:
void simplify_fraction(int * numerator,int * denominator)
{
int common_factor = gcd(* numerator,* denominator)
* numerator / = common_factor;
* denominator / = common_factor;
if(* denominator {
* numerator * = -1;
* denominator * = -1;
}
}
int gcd(int a,int b)
{
if(a< 0)
{
return gcd(-a,b);
}
else if(b {
return gcd(a,-b);
}
else if(b> a)
{
return gcd(b,a);
}
else
{
if(b == 0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
}
};
};
使用命名空间_LEET_MAX_POINTS_ON_A_LINE;
int LEET_MAX_POINTS_ON_A_LINE()
{
解决方案;
return 0;
}
在Visual Studio 2015上编译得很好(有一些不相关的警告)但它在g ++上惨败(如在ideone上:)
这里是编译器输出:
在/ usr / include /来自/ usr / include / c ++ / 5 / unordered_map:47的$ c / 5 / bits / hashtable.h:35:0,
,来自prog.cpp的
:1:
/ usr /包括/ C ++ / 5 /位/ hashtable_policy.h:在实例化结构的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>':
/ usr / include目录/ C ++ / 5 / type_traits:137:12:从要求结构的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> >'
/ usr / include / c ++ / 5 / type_traits:148:38:需要从'struct std :: __ not_< std :: __ and_< std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine& __detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
/usr/include/c++/5/bits/unordered_map.h:100:66:required from'class std :: unordered_map< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,std :: unordered_set< int> _LEET_MAX_POINTS_ON_A_LINE: :SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLineEqual>'
prog.cpp:96:102:此处需要
/usr/include/c++/5/bits/hashtable_policy.h:85:34:错误:无匹配调用(常量_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine)(常量_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine&安培;)
noexcept(declval<常量_hash和放大器;>()(declval<常量_key和放大器;>()))>
^
在/usr/include/c++/5/bits/move.h:57:0包含的文件中,
来自/ usr / include / c ++ / 5 / bits / stl_pair。 h:59,
来自/ usr / include / c ++ / 5 / utility:70,
来自/ usr / include / c ++ / 5 / unordered_map:38,
来自prog.cpp:1 :
/ usr / include目录/ C ++ / 5 / type_traits:在结构的std :: __ not_<实例;的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >':
/usr/include/c++/5/bits/unordered_map.h:100:66:从要求类的std :: unordered_map< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,性病:: unordered_set< INT>中_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLineEqual>'
prog.cpp:96:102:从这里需要
/ usr / include / c ++ / 5 / type_traits:148:38:error:'value'is不是'性病成员:: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> >'
:public integral_constant< bool,!_Pp :: value>
^
在/ usr / include / c ++ / 5 / unordered_map:48:0,
从prog.cpp:1:
包含的文件/ usr / include / c ++ / 5 /位/ unordered_map.h:在类的std :: unordered_map< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,性病:: unordered_set< INT>中_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLineEqual>'的实例:
prog.cpp :96:102:required from here
/usr/include/c++/5/bits/unordered_map.h:100:66:error:'value'不是'std :: __ not_< std :: __and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef __umap_hashtable< _Key,_Tp,_Hash,_Pred,_Alloc> _Hashtable;
^
/usr/include/c++/5/bits/unordered_map.h:107:45:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: key_type key_type;
^
/usr/include/c++/5/bits/unordered_map.h:108:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: value_type value_type;
^
/usr/include/c++/5/bits/unordered_map.h:109:48:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: mapped_type mapped_type;
^
/usr/include/c++/5/bits/unordered_map.h:110:43:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: hasher hasher;
^
/usr/include/c++/5/bits/unordered_map.h:111:46:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: key_equal key_equal;
^
/usr/include/c++/5/bits/unordered_map.h:112:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: allocator_type allocator_type;
^
/usr/include/c++/5/bits/unordered_map.h:117:45:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: pointer pointer;
^
/usr/include/c++/5/bits/unordered_map.h:118:50:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: const_pointer const_pointer;
^
/usr/include/c++/5/bits/unordered_map.h:119:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: reference reference;
^
/usr/include/c++/5/bits/unordered_map.h:120:52:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: const_reference const_reference;
^
/usr/include/c++/5/bits/unordered_map.h:121:46:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: iterator iterator;
^
/usr/include/c++/5/bits/unordered_map.h:122:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash typedef typename _Hashtable :: const_iterator const_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:123:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: local_iterator local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:124:57:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: const_local_iterator const_local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:125:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: size_type size_type;
^
/usr/include/c++/5/bits/unordered_map.h:126:52:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
typedef typename _Hashtable :: difference_type difference_type;
^
/usr/include/c++/5/bits/unordered_map.h:280:7:错误:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>,std :: __ detail :: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
operator =(initializer_list< value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:379:2:error:'value'is不是'性病成员:: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
emplace(_Args& ... __args)
^
/usr/include/c++/5/bits/unordered_map.h:432:7:error:'value 不是的一员的std :: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
insert(const value_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:439:2:error:'value'不是的性病成员:: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
insert(_Pair&& __x)
^
/usr/include/c++/5/bits/unordered_map.h:499:7:错误:'value'不是的性病成员:: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
insert(initializer_list< value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:645:7:错误:'value'不是的性病成员:: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
equal_range(const key_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:649:7:错误:'value'不是的性病成员:: __ not_<的std :: __ and_<的std :: __ is_fast_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine>中的std :: __细节:: __ is_noexcept_hash< _LEET_MAX_POINTS_ON_A_LINE :: SlantedLine,_LEET_MAX_POINTS_ON_A_LINE :: SlantedLine> > >'
equal_range(const key_type& __x)const
^
prog.cpp:在成员函数int _LEET_MAX_POINTS_ON_A_LINE :: Solution :: maxPoints(std :: vector< _LEET_MAX_POINTS_ON_A_LINE :: Point> ;&)':
prog.cpp:97:29:error:'probe'未在此范围内声明
if(probe == slantedLineToPointsMap.end())
^
prog.cpp:118:142:error:'i'未在此范围中声明
for(unordered_map< SlantedLine,unordered_set< int>,SlantedLine,SlantedLineEqual> :: iterator i = slantedLineToPointsMap.begin ); i!= slantedLineToPointsMap.end(); i ++)
^
编译器错误输出看起来非常相似在此主题张贴的那个。
肯定我的哈希函数是const。不确定为什么它仍然抱怨我的哈希函数不是const。
您使用的是不正确的迭代器类型。 >
您有:
unordered_map< SlantedLine,unordered_set< int>,SlantedLineHash, SlantedLineEqual> slantedLineToPointsMap;
...
unordered_map< SlantedLine,unordered_set< int>,SlantedLine,SlantedLineEqual> :: iterator probe = slantedLineToPointsMap.find(line)
...
for(unordered_map< SlantedLine,unordered_set< int> SlantedLine,SlantedLineEqual> :: iterator i = slantedLineToPointsMap.begin(); i!= slantedLineToPointsMap.end(); i ++)
您可以看到您指定了一个错误的迭代器类型 - 将类型更改为 auto
(这将帮助逃避这样的问题)或使用 unordered_map< SlantedLine,unordered_set< int>,SlantedLineHash,SlantedLineEqual> :: iterator
p>
原始错误不是那么可怕 - 它告诉你,编译器需要 void SlantedLine :: operator()(const SlantedLine&)const
。如果您将它添加到您的代码如下:
struct SlantedLine
{
public:
int slope_numerator;
int slope_denominator;
int intercept_numerator;
int intercept_denominator;
void operator()(const SlantedLine&)const
{
}
};
它编译。它确实要求这个运算符,因为它认为 SlantedLine
是哈希类型。
Here is the code I am trying to compile:
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
namespace _LEET_MAX_POINTS_ON_A_LINE
{
struct Point
{
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
struct SlantedLine
{
public:
int slope_numerator;
int slope_denominator;
int intercept_numerator;
int intercept_denominator;
};
struct SlantedLineHash
{
size_t operator()(const SlantedLine& k) const
{
return k.slope_numerator ^ k.slope_denominator ^ k.intercept_numerator ^ k.intercept_denominator;
}
};
struct SlantedLineEqual
{
size_t operator()(const SlantedLine& line1, const SlantedLine& line2) const
{
return (line1.slope_numerator == line2.slope_numerator) && (line1.slope_denominator == line2.slope_denominator) && (line1.intercept_numerator == line2.intercept_numerator) && (line1.intercept_denominator == line2.intercept_denominator);
}
};
class Solution
{
public:
int maxPoints(vector<Point>& points)
{
if (points.size() == 0)
{
return 0;
}
unordered_map<SlantedLine, unordered_set<int>, SlantedLineHash, SlantedLineEqual> slantedLineToPointsMap;
unordered_map<int, unordered_set<int>> verticalLineToPointsMap;
for (size_t i = 0; i < points.size(); i++)
{
for (size_t j = i + 1; j < points.size(); j++)
{
int x1 = points[i].x;
int y1 = points[i].y;
int x2 = points[j].x;
int y2 = points[j].y;
if (x1 == x2)
{
unordered_map<int, unordered_set<int>>::iterator probe = verticalLineToPointsMap.find(x1);
if (probe == verticalLineToPointsMap.end())
{
unordered_set<int> points;
points.insert(i);
points.insert(j);
verticalLineToPointsMap.insert(pair<int, unordered_set<int>>(x1, points));
}
else
{
probe->second.insert(i);
probe->second.insert(j);
}
}
else
{
int slope_numerator = y2 - y1;
int slope_denominator = x2 - x1;
int intercept_numerator = y1 * x2 - x1 * y2;
int intercept_denominator = x2 - x1;
simplify_fraction(&slope_numerator, &slope_denominator);
simplify_fraction(&intercept_numerator, &intercept_denominator);
SlantedLine line;
line.slope_numerator = slope_numerator;
line.slope_denominator = slope_denominator;
line.intercept_numerator = intercept_numerator;
line.intercept_denominator = intercept_denominator;
unordered_map<SlantedLine, unordered_set<int>, SlantedLine, SlantedLineEqual>::iterator probe = slantedLineToPointsMap.find(line);
if (probe == slantedLineToPointsMap.end())
{
unordered_set<int> points;
points.insert(i);
points.insert(j);
slantedLineToPointsMap.insert(pair<SlantedLine, unordered_set<int>>(line, points));
}
else
{
probe->second.insert(i);
probe->second.insert(j);
}
}
}
}
size_t maxPoints = 0;
for (unordered_map<int, unordered_set<int>>::iterator i = verticalLineToPointsMap.begin(); i != verticalLineToPointsMap.end(); i++)
{
maxPoints = max(maxPoints, i->second.size());
}
for (unordered_map<SlantedLine, unordered_set<int>, SlantedLine, SlantedLineEqual>::iterator i = slantedLineToPointsMap.begin(); i != slantedLineToPointsMap.end(); i++)
{
maxPoints = max(maxPoints, i->second.size());
}
return maxPoints;
}
private:
void simplify_fraction(int* numerator, int* denominator)
{
int common_factor = gcd(*numerator, *denominator);
*numerator /= common_factor;
*denominator /= common_factor;
if (*denominator < 0)
{
*numerator *= -1;
*denominator *= -1;
}
}
int gcd(int a, int b)
{
if (a < 0)
{
return gcd(-a, b);
}
else if (b < 0)
{
return gcd(a, -b);
}
else if (b > a)
{
return gcd(b, a);
}
else
{
if (b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
}
};
};
using namespace _LEET_MAX_POINTS_ON_A_LINE;
int LEET_MAX_POINTS_ON_A_LINE()
{
Solution solution;
return 0;
}
The code compiled fine (with a unrelated few warnings) on Visual Studio 2015, but it failed miserably on g++ (as on ideone: https://ideone.com/d1Rs1I)
Here are the compiler output:
In file included from /usr/include/c++/5/bits/hashtable.h:35:0,
from /usr/include/c++/5/unordered_map:47,
from prog.cpp:1:
/usr/include/c++/5/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine>':
/usr/include/c++/5/type_traits:137:12: required from 'struct std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> >'
/usr/include/c++/5/type_traits:148:38: required from 'struct std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
/usr/include/c++/5/bits/unordered_map.h:100:66: required from 'class std::unordered_map<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, std::unordered_set<int>, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLineEqual>'
prog.cpp:96:102: required from here
/usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match for call to '(const _LEET_MAX_POINTS_ON_A_LINE::SlantedLine) (const _LEET_MAX_POINTS_ON_A_LINE::SlantedLine&)'
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
^
In file included from /usr/include/c++/5/bits/move.h:57:0,
from /usr/include/c++/5/bits/stl_pair.h:59,
from /usr/include/c++/5/utility:70,
from /usr/include/c++/5/unordered_map:38,
from prog.cpp:1:
/usr/include/c++/5/type_traits: In instantiation of 'struct std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >':
/usr/include/c++/5/bits/unordered_map.h:100:66: required from 'class std::unordered_map<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, std::unordered_set<int>, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLineEqual>'
prog.cpp:96:102: required from here
/usr/include/c++/5/type_traits:148:38: error: 'value' is not a member of 'std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> >'
: public integral_constant<bool, !_Pp::value>
^
In file included from /usr/include/c++/5/unordered_map:48:0,
from prog.cpp:1:
/usr/include/c++/5/bits/unordered_map.h: In instantiation of 'class std::unordered_map<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, std::unordered_set<int>, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLineEqual>':
prog.cpp:96:102: required from here
/usr/include/c++/5/bits/unordered_map.h:100:66: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
^
/usr/include/c++/5/bits/unordered_map.h:107:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::key_type key_type;
^
/usr/include/c++/5/bits/unordered_map.h:108:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::value_type value_type;
^
/usr/include/c++/5/bits/unordered_map.h:109:48: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::mapped_type mapped_type;
^
/usr/include/c++/5/bits/unordered_map.h:110:43: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::hasher hasher;
^
/usr/include/c++/5/bits/unordered_map.h:111:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::key_equal key_equal;
^
/usr/include/c++/5/bits/unordered_map.h:112:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::allocator_type allocator_type;
^
/usr/include/c++/5/bits/unordered_map.h:117:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::pointer pointer;
^
/usr/include/c++/5/bits/unordered_map.h:118:50: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::const_pointer const_pointer;
^
/usr/include/c++/5/bits/unordered_map.h:119:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::reference reference;
^
/usr/include/c++/5/bits/unordered_map.h:120:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::const_reference const_reference;
^
/usr/include/c++/5/bits/unordered_map.h:121:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::iterator iterator;
^
/usr/include/c++/5/bits/unordered_map.h:122:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::const_iterator const_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:123:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::local_iterator local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:124:57: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::const_local_iterator const_local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:125:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::size_type size_type;
^
/usr/include/c++/5/bits/unordered_map.h:126:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
typedef typename _Hashtable::difference_type difference_type;
^
/usr/include/c++/5/bits/unordered_map.h:280:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
operator=(initializer_list<value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:379:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
emplace(_Args&&... __args)
^
/usr/include/c++/5/bits/unordered_map.h:432:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
insert(const value_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:439:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
insert(_Pair&& __x)
^
/usr/include/c++/5/bits/unordered_map.h:499:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
insert(initializer_list<value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:645:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
equal_range(const key_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:649:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine>, std::__detail::__is_noexcept_hash<_LEET_MAX_POINTS_ON_A_LINE::SlantedLine, _LEET_MAX_POINTS_ON_A_LINE::SlantedLine> > >'
equal_range(const key_type& __x) const
^
prog.cpp: In member function 'int _LEET_MAX_POINTS_ON_A_LINE::Solution::maxPoints(std::vector<_LEET_MAX_POINTS_ON_A_LINE::Point>&)':
prog.cpp:97:29: error: 'probe' was not declared in this scope
if (probe == slantedLineToPointsMap.end())
^
prog.cpp:118:142: error: 'i' was not declared in this scope
for (unordered_map<SlantedLine, unordered_set<int>, SlantedLine, SlantedLineEqual>::iterator i = slantedLineToPointsMap.begin(); i != slantedLineToPointsMap.end(); i++)
^
The compiler error output does look very much alike the one posted on this thread.
Creating an std::unordered_map with an std::pair as key
But I am sure my hash function is const. Not sure why it is still complaining my hash function is not const.
You are using an incorrect iterator type.
You have:
unordered_map<SlantedLine, unordered_set<int>, SlantedLineHash, SlantedLineEqual> slantedLineToPointsMap;
...
unordered_map<SlantedLine, unordered_set<int>, SlantedLine, SlantedLineEqual>::iterator probe = slantedLineToPointsMap.find(line);
...
for (unordered_map<SlantedLine, unordered_set<int>, SlantedLine, SlantedLineEqual>::iterator i = slantedLineToPointsMap.begin(); i != slantedLineToPointsMap.end(); i++)
You can see that you have specified a wrong iterator type - either change the type to auto
(which will help escape such issues) or use unordered_map<SlantedLine, unordered_set<int>, SlantedLineHash, SlantedLineEqual>::iterator
.
The original error is not that terrible - what it tells you is that the compiler requires void SlantedLine::operator()(const SlantedLine&) const
. If you add it to your code like this:
struct SlantedLine
{
public:
int slope_numerator;
int slope_denominator;
int intercept_numerator;
int intercept_denominator;
void operator()(const SlantedLine&) const
{
}
};
It compiles. And it did ask for this operator since it perceived SlantedLine
to be the hash type.
这篇关于当用户在unordered_map中定制哈希函数时,无法解码g ++中的模糊编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!