C++ 打印映射的键值对,其中键是C+中的向量+;

C++ 打印映射的键值对,其中键是C+中的向量+;,c++,algorithm,dictionary,data-structures,c++17,C++,Algorithm,Dictionary,Data Structures,C++17,我有一个2d向量,它表示图形上的点。我试图找到这些点与原点之间的欧几里德距离 这是我的密码: 我的想法是将点映射为向量作为键,将其欧几里德距离作为值。我可以这样做,但我无法打印带有键值对的地图。如何打印地图以查看值 #include <iostream> #include <algorithm> #include <vector> #include <unordered_map> #include <map> #include <

我有一个2d向量,它表示图形上的点。我试图找到这些点与原点之间的欧几里德距离

这是我的密码:

我的想法是将点映射为向量作为键,将其欧几里德距离作为值。我可以这样做,但我无法打印带有键值对的地图。如何打印地图以查看值

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <map>
#include <string>
#include <numeric>
#include <set>
#include <unordered_set>

using namespace std;

int main()
{
    vector<vector<int>> points = { {3,3},{5,-1},{-2,4} };

    unordered_map<vector<int>, double> mp;

    //for loop to find euclidean distance of each data point and then insert into map
    for (int i = 0; i < points.size(); i++)
    {
        int sum = 0;
        vector<int> alpha;  //temporary vector to store the data points 
        
        for (int j = 0; j < points[i].size(); j++)
        {
            sum = sum + (points[i][j] * points[i][j]);
            alpha.push_back(points[i][j]);
        }
        double res = sum;
        res = sqrt(res); //finding sqrt for euclidean distance
        mp.insert({ alpha, res });  //inserting into the map
        alpha.clear();
    }
  
    //Trying to print the map but isn't working
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
            << '\t' << itr->second << '\n';
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量点={3,3},{5,-1},{-2,4};
无序地图mp;
//for循环找到每个数据点的欧氏距离,然后插入到地图中
对于(int i=0;i首先,
std::vector
键没有散列函数,因此您需要使用
std::unordered_map
其他方法:

std::无序地图mp;

您可以使用一个单独的函数来打印
std::vector
的元素,因为
操作符您可以定义一个
,请在问题中包含错误消息。
std::vector
没有用于打印
std::cout
的重载。您需要循环向量和pr>中的所有元素一个接一个地对它们进行整型(或者编写一个函数为您进行整型,并将向量传递给该函数)。此外,最好将
#包含在代码示例中。“保存”2-3行代码是一个错误的权衡,如果任何人想要复制和测试您的代码,需要在代码运行之前对其进行修改。@super我修改了我的问题没有用于
std::vector
key iirc的哈希函数这可以不用boost来完成吗library@R0bert是的,您只需要编写
std::size\u t哈希\u组合(std::size\u t,std::size\u t)
你自己。在紧要关头,
res^=elem\u hash(v);
并不可怕。
for (auto & [vec, sum] : mp) {
    for (auto & point : vec) {
        std::cout << point << " "
    }
    std::cout << '\t' << sum << '\n';
}
template <typename T, typename ElemHash = std::hash<T>>
struct sequence_hash {
    template <typename C>
    std::size_t operator()(const C & container) {
        std::size_t res = size_hash(container.size());
        for (auto & v : container) { 
            /* some combiner */
            boost::hash_combine(res, elem_hash(v)); 
        }
        return res;
    }

private:
    std::hash<std::size_t> size_hash;
    ElemHash elem_hash;
};