迭代对象列表

Iterating over list of list of object

本文关键字:列表 对象 迭代      更新时间:2023-10-16

我想迭代对象列表

list<list<Point>> gnuPoints;

这是一个静态列表,它接受来自Circle和Polygon等各个类的列表,所有类别将多个对象(X,Y)推到列表

 while (angle != 360)
{
 double x = m_radius*cos(angle);
 double y = m_radius*sin(angle);
 angle += 30;
 circlePoint.push_back(Point(x, y));
}
similarly goes with Polygon and line shapes
polygonPoint.push_back(Point(x,y));
linePoint.Push_back(point(x,y));

然后将这些列表推入gnupoint(list&lt; list&lt; point>>)。

gnuPoints.push_back(circlePoints);
gnuPoints.push_back(polygonPoints);
gnuPoints.push_back(linePoints);

现在我想在文件中写下所有这些不同形状的y值,要迭代它,我在此代码之后找不到任何特定的解决方案。

for (list<list<Point>>::iterator it = Point::gnuPoints.begin();
    it != Point::gnuPoints.end();
    it++)
{
//My assumption is that another For loop would come but could not apply 
  as I don't know what is available at the first index of gnuPoints list.
}
for (list<list<Point>>::iterator it = Point::gnuPoints.begin();
    it != Point::gnuPoints.end();
    it++)
{
     for (list<Point>::iterator innerIt = it->begin(); innerIt != it->end(); ++innerIt)
}

如果您已经在支持C 11的编译器上,则可以简单地写为:

for (const auto& container : Point::gnuPoints)
{
    for (const auto& item : container)
    {
    }
}