[英]How to deal with nested data structure that contain lists in c++
I have two data structure one containing the other as seen, I want to loop on all lineSegmentClusters, access the list of candidatePointList of each and finally access the lineSegmentId of each in that list. 我有两个数据结构,一个包含另一个,如图所示,我想在所有lineSegmentClusters上循环,访问每个的候选点列表列表,最后访问该列表中的每个的lineSegmentId。
typedef struct CandidateClusterPoint {
float orderingValue;
int lineSegmentId;
bool startPointFlag;
} CandidateClusterPoint;
typedef struct LineSegmentCluster {
int lineSegmentClusterId;
int nLineSegments;
list<CandidateClusterPoint> candidatePointList;
vector<CMDPoint> clusterPointArray;
bool enabled;
} LineSegmentCluster;
what I tried is: 我试过的是:
list<CandidateClusterPoint>::iterator iter;
for (int i = 0; i < m_currComponentId; i++)
{
if (m_lineSegmentClusters[i].enabled)
{
for(iter=m_lineSegmentClusters[i].candidatePointList.begin() ; iter!=m_lineSegmentClusters[i].candidatePointList.end() ; iter++)
{
}
but I cant access the lineSegmentID of each one what do I need to write to be able to access them one after the other? 但是我无法访问每个行的lineSegmentID,我需要写什么才能能够一个接一个地访问它们?
The iterator is not your actual struct but a class that contains a reference to it. 迭代器不是实际的结构,而是包含对其的引用的类。 You can access them by using the * operator like this:
您可以使用*运算符来访问它们,如下所示:
(*iter).lineSegmentID = <your value>;
Have you tried using pointers? 您是否尝试过使用指针? In your LineSegmentCluster class, define this as the list:
在LineSegmentCluster类中,将其定义为列表:
list<CandidateClusterPoint*> candidatePointList;
Then in your main function do this (always use consts if you are not changing the variables the iterator is pointing to): 然后在您的main函数中执行此操作(如果不更改迭代器指向的变量,请始终使用const):
list<CandidateClusterPoint*>::const_iterator iter;
for (int i = 0; i < m_currComponentId; i++)
{
if (m_lineSegmentClusters[i].enabled)
{
for(itertry=m_lineSegmentClusters[i].candidatePointList.begin() ; itertry!=m_lineSegmentClusters[i].candidatePointList.end() ; itertry++)
{
cout << (*iter)->lineSegmentId // This should be where you access it
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.