C++ Isn';t无序映射应该只有两个参数?

C++ Isn';t无序映射应该只有两个参数?,c++,boost,hash,stl,unordered-map,C++,Boost,Hash,Stl,Unordered Map,这个问题是关于从一堆点中寻找共线点 首先,我不明白slopeMap和无序映射是怎么回事?难道map不应该只有一个键和一个值(map)?在这个特殊的代码中 unordered_map<pair<int, int>, int,boost:: hash<pair<int, int> > > slopeMap; 我还想要一个基于逻辑的建议。我如何修改这段代码,使其不返回定义最大共线点数的数字,而是将共线的任何点实际存储在某种形式的数

这个问题是关于从一堆点中寻找共线点

首先,我不明白
slopeMap
和无序映射是怎么回事?难道map不应该只有一个键和一个值(
map
)?在这个特殊的代码中

unordered_map<pair<int, int>, int,boost:: 
          hash<pair<int, int> > > slopeMap;
我还想要一个基于逻辑的建议。我如何修改这段代码,使其不返回定义最大共线点数的数字,而是将共线的任何点实际存储在某种形式的数据结构中,以供以后使用

资料来源:

难道map不应该只有一个键和一个值(map)吗

实际上,它有几个额外的模板参数。第三个参数是散列

如何修改此代码,使其不返回数字 它定义了共线点的最大数量,以实际存储任何 在某种形式的数据结构中是共线的点,我可以使用它 以后


使用整数坐标比使用浮点更容易(由于精度问题)。我假设你有一个2D点数组
p
。一种方法是,对于每两对点
p[i]
p[j]
,将密钥对dX,dY简化为最低形式(其中
dX=p[j].x-p[i].x
dY=p[j].y-p[i].y
)。然后,您的值可以是一个
std::set
,其中包含匹配的索引
i
j

更正其最大值*而不是最后第二行中的人。您可以随时回答您的问题。无需评论。请不要链接到代码。问题应该是独立的。当外部链接失效时,这个问题对未来的读者将毫无用处。请把所有相关内容都放在问题中。请参见cppreference.com。无序映射不仅仅是键/值对的集合。还涉及到散列,这就是第三个模板参数for@user4581301如果你想变得超级学究:最初的标准模板库有一个
散列图
,它只是没有进入标准
using namespace std; 

// method to find maximum colinear point 
int maxPointOnSameLine(vector< pair<int, int> > points) 
{ 


    int N = points.size(); 
      if (N < 2) 
        return N; 

    int maxPoint = 0; 
    int curMax, overlapPoints, verticalPoints; 

    // here since we are using unordered_map  
    // which is based on hash function  
    //But by default we don't have hash function for pairs 
    //so we'll use hash function defined in Boost library 
    unordered_map<pair<int, int>, int,boost:: 
              hash<pair<int, int> > > slopeMap; 

    // looping for each point 
    for (int i = 0; i < N; i++) 
    { 
        curMax = overlapPoints = verticalPoints = 0; 

        // looping from i + 1 to ignore same pair again 
        for (int j = i + 1; j < N; j++) 
        { 
            // If both point are equal then just 
            // increase overlapPoint count 
            if (points[i] == points[j]) 
                overlapPoints++; 

            // If x co-ordinate is same, then both 
            // point are vertical to each other 
            else if (points[i].first == points[j].first) 
                verticalPoints++; 

            else
            { 
                int yDif = points[j].second - points[i].second; 
                int xDif = points[j].first - points[i].first; 
                int g = __gcd(xDif, yDif); 

                // reducing the difference by their gcd 
                yDif /= g; 
                xDif /= g; 

                // increasing the frequency of current slope 
                // in map 
                slopeMap[make_pair(yDif, xDif)]++; 
                curMax = max(curMax, slopeMap[make_pair(yDif, xDif)]); 
            } 

            curMax = max(curMax, verticalPoints); 
        } 

        // updating global maximum by current point's maximum 
        maxPoint = max(maxPoint, curMax + overlapPoints + 1); 

        // printf("maximum colinear point  
        // which contains current point  
        // are : %d\n", curMax + overlapPoints + 1); 
        slopeMap.clear(); 
    } 

    return maxPoint; 
} 
int main() 
{ 
    const int N = 6; 
    int arr[N][2] = {{-1, 1}, {0, 0}, {1, 1}, {2, 2}, 
                    {3, 3}, {3, 4}}; 

    vector< pair<int, int> > points; 
    for (int i = 0; i < N; i++) 
        points.push_back(make_pair(arr[i][0], arr[i][1])); 

    cout << maxPointOnSameLine(points) << endl; 

    return 0; 
} 
Input : points[] = {-1, 1}, {0, 0}, {1, 1}, 
                    {2, 2}, {3, 3}, {3, 4} 
Output : 4
Then maximum number of point which lie on same
line are 4, those point are {0, 0}, {1, 1}, {2, 2},
{3, 3}