QList<unsigned int>::iterator k;
for (k = GridIndxe_List.begin(); k != GridIndxe_List.end(); ++k)
{
cout << *k << endl;
}
uj5u.com熱心網友回復:
代碼貼全
uj5u.com熱心網友回復:
這段代碼沒錯啊, 錯誤是定位在這段代碼? 你看報錯的是 std::vector , 不是 QListuj5u.com熱心網友回復:
#include <QtCore/QCoreApplication>#include <QList>
#include <QMultiHash>
#include <QString>
#include <QFileDialog>
#include <iostream>
#include "Octree.h"
#include <pcl/octree/octree.h>
using namespace std;
int main()
{
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
//pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
string fileName="";
float searchRadius=0.0f;
cout<<"請輸入檔案名稱:";
cin>>fileName;
if(!openLasFile(cloud,fileName))
{
cout<<"點云打開失敗!"<<endl;
system("pause");
return -1;
}//三維格網的尺寸
const float Xscale3d = 0.2f;
const float Yscale3d = 0.2f;
const float Zscale3d = 0.05f;
//格網的行、列、層數
int RowNum, ColNum, LayerNum;
//定義存盤極值的兩個點。
pcl::PointXYZI minPt, maxPt;
//獲取坐標極值。
pcl::getMinMax3D(*cloud, minPt, maxPt);
double X_Max = maxPt.x;
double X_Min = minPt.x;
double Y_Max = maxPt.y;
double Y_Min = minPt.y;
double Z_Max = maxPt.z;
double Z_Min = minPt.z;
double xscale = (X_Max - X_Min)/2;
double yscale = (Y_Max - Y_Min)/2;
double zscale = (Z_Max - Z_Min)/2;
/*cout << "坐標極值如下" << endl;
cout << X_Max << endl;
cout << X_Min << endl;
cout << Y_Max << endl;
cout << Y_Min << endl;
cout << Z_Max << endl;
cout << Z_Min << endl;*/
//計算整個格網的行、列、層數,取整數
RowNum = int ((X_Max-X_Min)/xscale+1);
ColNum = int ((Y_Max-Y_Min)/yscale+1);
LayerNum = int((Z_Max-Z_Min)/zscale+1);
/*cout << "格網行列層號如下" << endl;
cout << RowNum << endl;
cout << ColNum << endl;
cout << LayerNum << endl;
//計算每個點云所在的行列號。*/
//pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud(new pcl::PointCloud<pcl::PointXYZ>);
//每個點所在的三維格網線性索引號
QList<unsigned int> GridIndxe_List;//點云在格網中線性索引號鏈表。
QList<pcl::PointXYZI> GridPoint_List;
QList<unsigned int> Grid3dIndex_List; //非空三維格網索引號鏈表
//哈希表鍵值的線性索引
QList<unsigned int> HashKey_List;//哈希表鍵值串列
QList<unsigned int> HashKey_List_Fir;
int row, col, layer;
int d_row ,d_col, d_layer;
unsigned int TempIndex=0;
int root_node_code = 80;
//新建一個QMultiHash物件point3dHash,哈希表Point3dHash就代表整個三維格網。
QMultiHash<unsigned int,pcl::PointXYZI> Point3dHash;
/*cout << xscale << endl;
cout << yscale << endl;
cout <<zscale << endl;*/
for (int nIndex=0; nIndex < cloud->points.size(); nIndex++)
{
row = int((cloud->points[nIndex].x - X_Min)/xscale) + 1;
col = int((cloud->points[nIndex].y - minPt.y)/yscale) + 1;
layer = int((cloud->points[nIndex].z - minPt.z)/zscale) + 1;
if (col == 1 && row == 1 && layer == 1)
{
TempIndex = root_node_code + 1;
}
if(col == 2 && row == 1 && layer == 1)
{
TempIndex = root_node_code + 2;
}
if (col == 1 && row == 2 && layer == 1)
{
TempIndex = root_node_code + 3;
}
if(col == 2 && row == 2 && layer ==1)
{
TempIndex = root_node_code + 4;
}
if (col == 1 && row == 1 && layer == 2)
{
TempIndex = root_node_code + 5;
}
if(col == 2 && row == 1 && layer == 2)
{
TempIndex = root_node_code + 6;
}
if (col == 1 && row == 2 && layer == 2)
{
TempIndex = root_node_code + 7;
}
if (col == 2 && row == 2 && layer == 2)
{
TempIndex = root_node_code + 8;
}
GridIndxe_List.append(TempIndex);//每個點云在三維格網中的線性索引號,下表值與點云在cloud里面的下表值一一對應。
//把第nIndex個點云存放到線性索引為TempIndex的格網里面去。
//cout << col << "--" << row << "--" << layer << "--" << nIndex << "--" << TempIndex << endl;
Point3dHash.insert(TempIndex,cloud->points[nIndex]);
}
//const QList<pcl::PointXYZI>& my_list = Point3dHash.values(3);//把線性索引為3的格網里面的所有點云取出來,放到mylist鏈表里面去。
//存放每個三維格網中點云的強度極值
vector<float> Grid3dMaxI_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);//存放強度最大值
vector<float> Grid3dMinI_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);//存放強度最小值
vector<float> Grid3dMaxX_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
vector<float> Grid3dMinX_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
vector<float> Grid3dMaxY_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
vector<float> Grid3dMinY_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
vector<float> Grid3dMaxZ_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
vector<float> Grid3dMinZ_vec(RowNum*RowNum*ColNum*ColNum*LayerNum);
//宣告compare函式,比較陣列中兩個元素的強度值
bool CompareIntensityMin(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool CompareIntensityMax(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Min_X(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Max_X(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Min_Y(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Max_Y(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Min_Z(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
bool Compar_Max_Z(pcl::PointXYZI& P1, pcl::PointXYZI& P2);
//使用迭代器
unsigned int TempIndex_Fir;
QMultiHash<unsigned int,pcl::PointXYZI>::iterator k;
for (k = Point3dHash.begin(); k!=Point3dHash.end(); ++k)
{
TempIndex = k.key();
if (HashKey_List.contains(TempIndex))
{
continue;
}
HashKey_List.append(TempIndex);
GridPoint_List = Point3dHash.values(TempIndex);
//cout << GridPoint_List.size() << endl;
qSort(GridPoint_List.begin(),GridPoint_List.end(),CompareIntensityMin);
Grid3dMinI_vec[TempIndex] = GridPoint_List.at(0).intensity;
qSort(GridPoint_List.begin(),GridPoint_List.end(),CompareIntensityMax);
Grid3dMaxI_vec[TempIndex] = GridPoint_List.at(0).intensity;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Min_X);
Grid3dMinX_vec[TempIndex] = GridPoint_List.at(0).x;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Max_X);
Grid3dMaxX_vec[TempIndex] = GridPoint_List.at(0).x;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Min_Y);
Grid3dMinY_vec[TempIndex] = GridPoint_List.at(0).y;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Max_Y);
Grid3dMaxY_vec[TempIndex] = GridPoint_List.at(0).y;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Min_Z);
Grid3dMinZ_vec[TempIndex] = GridPoint_List.at(0).z;
qSort(GridPoint_List.begin(),GridPoint_List.end(),Compar_Max_Z);
Grid3dMaxZ_vec[TempIndex] = GridPoint_List.at(0).z;
if (GridPoint_List.size() > 2)
{
if (Grid3dMaxI_vec[TempIndex] - Grid3dMinI_vec[TempIndex] >= 10)
{
double xMind = (Grid3dMaxX_vec[TempIndex] - Grid3dMinX_vec[TempIndex])/2;
double yMind = (Grid3dMaxY_vec[TempIndex] - Grid3dMinY_vec[TempIndex])/2;
double zMind = (Grid3dMaxZ_vec[TempIndex] - Grid3dMinZ_vec[TempIndex])/2;
//cout << zMind <<endl;
//cout << TempIndex << "-" << xMind << endl;
for (int i = 0; i <= GridPoint_List.size(); ++i)
{
d_row = int ((GridPoint_List.at(i).x - Grid3dMinX_vec[TempIndex])/xMind) + 1;
d_col = int ((GridPoint_List.at(i).y - Grid3dMinY_vec[TempIndex])/yMind) + 1;
d_layer = int ((GridPoint_List.at(i).z - Grid3dMinZ_vec[TempIndex])/zMind) + 1;
if (d_col == 1 && d_row == 1 && d_layer == 1)
{
TempIndex = TempIndex * 10 + 1;
}
if(d_col == 2 && d_row == 1 && d_layer == 1)
{
TempIndex = TempIndex * 10 + 2;
}
if (d_col == 1 && d_row == 2 && d_layer == 1)
{
TempIndex = TempIndex * 10 + 3;
}
if(d_col == 2 && d_row == 2 && d_layer ==1)
{
TempIndex = TempIndex * 10 + 4;
}
if (d_col == 1 && d_row == 1 && d_layer == 2)
{
TempIndex = TempIndex * 10 + 5;
}
if(d_col == 2 && d_row == 1 && d_layer == 2)
{
TempIndex = TempIndex * 10 + 6;
}
if (d_col == 1 && d_row == 2 && d_layer == 2)
{
TempIndex = TempIndex * 10 + 7;
}
if (d_col == 2 && d_row == 2 && d_layer == 2)
{
TempIndex = TempIndex * 10 + 8;
}
cout << TempIndex << "--"<< i << endl;
GridIndxe_List.append(TempIndex);
//Point3dHash.insert(TempIndex,GridPoint_List.at(i));
}
}
else
{
//HashKey_List.removeAt(TempIndex);
}
}
else
{
TempIndex_Fir = TempIndex;
HashKey_List_Fir.append(TempIndex);
cout << TempIndex_Fir << endl;
}
}
return 0;
}
//定義compare函式,比較陣列中兩個元素的強度值
bool CompareIntensityMin(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.intensity <= P2.intensity);
}
bool CompareIntensityMax(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.intensity >= P2.intensity);
}
bool Compar_Min_X(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.x <= P2.x );
}
bool Compar_Max_X(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.x >= P2.x );
}
bool Compar_Min_Y(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.y <= P2.y );
}
bool Compar_Max_Y(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.y >= P2.y );
}
bool Compar_Min_Z(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.z <= P2.z );
}
bool Compar_Max_Z(pcl::PointXYZI& P1, pcl::PointXYZI& P2)
{
return (P1.z >= P2.z );
}
uj5u.com熱心網友回復:
這是初始的代碼uj5u.com熱心網友回復:
會報以上兩張圖片中的錯誤uj5u.com熱心網友回復:
點重試,看編譯器當在哪兒?uj5u.com熱心網友回復:

重試之后,除錯器就會報未經處理的例外。
uj5u.com熱心網友回復:
是不是在進行容器迭代回圈的時候,不能插入元素?uj5u.com熱心網友回復:
點“是”,就知道錯在哪兒了。uj5u.com熱心網友回復:
剛學C++,做實驗用,不太會除錯,麻煩了。

uj5u.com熱心網友回復:
++k改為k++吧。uj5u.com熱心網友回復:
除錯請打開CallStack,在你的VC的除錯選單下,有呼叫堆疊的選項。而后你能找到是哪里呼叫了vector.uj5u.com熱心網友回復:
查看堆疊
uj5u.com熱心網友回復:
都提示你在933行了,你看看933行那行代碼是什么。大概率是類似Grid3dMinI_vec[TempIndex]。。。。。你是怎么會這么自信這個不會超界的?好歹你至少先判斷一下 if(Grid3dMinI_vec.size()>TempIndex) {Grid3dMinI_vec[TempIndex]......}uj5u.com熱心網友回復:
居然是vc/include.那這個你需要去看呼叫堆疊。找到你自己的代碼在哪行uj5u.com熱心網友回復:
vector 是動態陣列, 迭代器內部是元素地址指標,迭代程序中肯定不能對陣列進行增刪( 可能空間不足導致重新分配,迭代器內的指標指向就非法了), 這種情況,就藥用下標, 性能也不差,一個乘法而已, 對vector 能用下標就用下標,沒必要用迭代。uj5u.com熱心網友回復:
在stl 上,夠你慢慢折騰的, 我以前在這吃過很多苦頭,曾經在string 上折騰很久,后來前輩告訴我是這個stl版本上的bug, 還有一次在使用occi時又碰到stl上莫名的問題(多半版本不一致,最終把部分 occi改成oci ), 也是折騰的夠嗆,打那后,10年沒碰 stl 了, 自己寫模版, 所有第三方類別庫優先使用 C 介面,盡量不使用c++。 還是自己寫的模版好用,最起碼性能要好多了。uj5u.com熱心網友回復:
try throw一下雖然不能從根本上解決但是起碼先不報錯在慢慢找。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130088.html
標籤:C++ 語言
上一篇:鍵盤上給多個指標賦值字串并輸出
