迭代对象列表

时间:2016-12-16 06:38:27

标签: c++ list stl

我想迭代一个Object列表列表,

list<list<Point>> gnuPoints;

这是一个静态列表,它接受来自圆和多边形等各种类的列表,所有类都推送多个对象,即Point(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&gt;&gt;)。

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

现在我想在文件中写出不同形状的所有x,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.
}

1 个答案:

答案 0 :(得分:0)

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)
    {
    }
}