【发布时间】:2021-01-08 09:20:39
【问题描述】:
我有一个代表图表上点的二维向量。我正在尝试从原点找到这些点之间的欧几里得距离。
这是我的代码:
我的想法是将点映射为向量作为其欧几里得距离的键作为值。我能够做到,但我无法使用键值对打印地图。如何打印地图以查看值
#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;
}
错误:C2679 二进制“
【问题讨论】:
-
请在您的问题中包含错误信息。
std::vector没有使用std::cout打印的重载。您需要遍历向量中的所有元素并一一打印它们(或编写一个为您执行此操作的函数并将向量传递给函数)。 -
另外,在代码示例中保留
#includes 是个好主意。 “保存” 2-3 行代码是一个糟糕的权衡,因为任何想要复制和测试您的代码的人都需要在它工作之前对其进行修改。 -
@super 我修改了我的问题
-
std::vectorkey IIRC 没有哈希函数
标签: c++ algorithm dictionary data-structures c++17