std::vector<std::vector<double>> points 声明之后,怎么设置行列
时间: 2023-08-07 09:05:23 浏览: 34
可以使用`resize`函数设置行列,示例代码如下:
```
std::vector<std::vector<double>> points; // 声明一个二维数组
int rows = 3, cols = 4; // 设置行列
points.resize(rows); // 设置行数
for (int i = 0; i < rows; i++) {
points[i].resize(cols); // 设置列数
}
```
该代码先声明了一个二维数组`points`,然后设置了行数为3,列数为4。接着使用`resize`函数设置了行数为3,然后在循环中对每行设置了列数为4。这样就创建了一个3行4列的二维数组。你可以根据实际需求修改行列数。
相关问题
编写一个c++程序,自动随机生成100000行vector<double>类型的经纬度二维数组,例如: std::vector<std::vector<double>> points = {{120.275, 32.023}, {-120.138, -28.256}};
可以使用如下的代码来自动生成100000行的经纬度二维数组:
```c++
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(NULL)); // 初始化随机数生成器
std::vector<std::vector<double>> points(100000, std::vector<double>(2));
for (int i = 0; i < 100000; ++i) {
points[i][0] = (double)rand() / RAND_MAX * 360 - 180; // 生成经度,范围为[-180, 180]
points[i][1] = (double)rand() / RAND_MAX * 180 - 90; // 生成纬度,范围为[-90, 90]
}
// 输出前10个点的经纬度
for (int i = 0; i < 10; ++i) {
std::cout << "(" << points[i][0] << ", " << points[i][1] << ")" << std::endl;
}
return 0;
}
```
这个程序使用rand()函数生成随机数来生成经度和纬度,然后使用std::vector嵌套std::vector的方式来创建二维数组。这个程序也输出了前10个点的经纬度,可以用来验证生成的数据是否正确。
std::vector<std::vector<int>> LidarObjectSeg::Run(const pcl::PointCloud<pcl::PointXYZI>::Ptr &inCloud) const { if (inCloud->empty()) return {}; std::vector<std::array<double, 3>> gridPoints; GridParam gridParam = this->EstimateGridParam(inCloud, gridPoints); std::multimap<int, int> hashTable = this->UpdateHashTable(gridPoints, gridParam); std::vector<int> clusterIndices(inCloud->size(), -1); int curClusterIdx = 0; START_HOST_TIMING(ExtractClusters) for (std::size_t i = 0; i < inCloud->size(); ++i) { if (clusterIndices[i] >= 0) continue; const auto &curGridPoint = gridPoints[i]; std::vector<int> neighborIndices = this->GetNeighbors(curGridPoint, gridParam, hashTable); for (int neighborIdx : neighborIndices) { if (neighborIdx == i) continue; int curPointVoxelIdx = clusterIndices[i]; int neighborVoxelIdx = clusterIndices[neighborIdx]; if (curPointVoxelIdx >= 0 && neighborVoxelIdx >= 0) { if (curPointVoxelIdx != neighborVoxelIdx) this->MergeClusters(clusterIndices, curPointVoxelIdx, neighborVoxelIdx); } else { if (curPointVoxelIdx < 0) clusterIndices[i] = neighborVoxelIdx; else clusterIndices[neighborIdx] = curPointVoxelIdx; } } if (clusterIndices[i] < 0 && neighborIndices.size() >= numMinPoints_) { for (int neighborIdx : neighborIndices) { clusterIndices[neighborIdx] = curClusterIdx; } curClusterIdx++; } } STOP_HOST_TIMING(ExtractClusters) START_HOST_TIMING(GetAllClusters) std::vector<std::vector<int>> allClusters = this->GetAllClusters(clusterIndices); STOP_HOST_TIMING(GetAllClusters) PRINT_ALL_TIMING() return allClusters; }
这是一个函数,可以接收一个指向 pcl::PointCloud<pcl::PointXYZI> 类型的指针作为参数,函数返回一个 std::vector<std::vector<int>> 类型的对象。在函数内部,首先判断输入点云是否为空,如果为空则直接返回一个空的 std::vector<std::vector<int>> 类型的对象。然后函数会估算点云的网格参数,并根据网格参数更新哈希表。之后,函数会遍历点云中的每个点,如果该点已经被聚类过,则跳过该点;否则,函数会获取该点附近的所有点,然后将这些点分配到同一个聚类中。最后,函数会返回所有聚类的点的索引。