完善下面代码使得能输出显示哈希表中所有的键和对应的值#include pclpoint_typesh#include unordered_map#include stringstruct Slice pclPointIndices index; pclPointXYZRGB centroid;;void haxipclPointCloudpclPointXYZRGBPtr cloud
要输出哈希表中所有的键和对应的值,可以使用迭代器来遍历哈希表。以下是完善后的代码:
#include <pcl/point_types.h>
#include <unordered_map>
#include <string>
#include <iostream>
struct Slice {
pcl::PointIndices index;
pcl::PointXYZRGB centroid;
};
void haxi(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud4, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud5)
{
std::unordered_map<std::string, Slice> sliceMap;
std::vector<Slice> SALL;
for (int i = 0; i < cluster_centroids->size(); ++i)
{
Slice slices;
SALL.push_back(slices);
Slice& slice0 = SALL[i];
slice0.index = cluster_indices[i];
slice0.centroid.x = cluster_centroids->points[i].x;
slice0.centroid.y = cluster_centroids->points[i].y;
slice0.centroid.z = cluster_centroids->points[i].z;
std::string key = std::to_string(slice0.centroid.x) + "_" + std::to_string(slice0.centroid.y) + "_" + std::to_string(slice0.centroid.z);
sliceMap[key] = slice0;
}
// 输出哈希表中所有的键和对应的值
for (auto it = sliceMap.begin(); it != sliceMap.end(); ++it) {
std::cout << "Key: " << it->first << std::endl;
std::cout << "Value: " << std::endl;
std::cout << " Index: " << it->second.index << std::endl;
std::cout << " Centroid: (" << it->second.centroid.x << ", " << it->second.centroid.y << ", " << it->second.centroid.z << ")" << std::endl;
}
}
这样,当调用haxi函数时,会输出哈希表sliceMap中所有的键和对应的值。
原文地址: https://www.cveoy.top/t/topic/h1xB 著作权归作者所有。请勿转载和采集!