C++ 列表中的列表

C++ list in list

我有两个列表

list<int> s;
list<std::list<int>> q;

我做了以下作业

q.push_front(s);

如何显示 q 的内容,因为

for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
    for (s_iterator = q_iterator.begin(); s_iterator != q_iterator.end(); ++s_iterator)
        cout << *s_iterator;

给我一个错误?

你要这样写

for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
    for (s_iterator = q_iterator->begin(); s_iterator != q_iterator->end(); ++s_iterator)
        cout << *s_iterator;

或以下方式

for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
    for (s_iterator = ( *q_iterator ).begin(); s_iterator != ( *q_iterator ).end(); ++s_iterator)
        cout << *s_iterator;

前提是 q_iterators_iterator 已经声明。否则你可以写成

for ( auto q_iterator = q.begin; /*...*/ )

你也可以使用基于范围的for语句。例如

for ( const auto &s : q )
{
    for ( int x : s ) std::cout << x << ' ';
    std::cout << std::endl;
}

你可能知道,但我只是想确保你记得尖括号之间的 space。

std::list<std::list<int> >