我有两个清单

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;
}

关于c++ - list 中的C++ list ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30275037/

10-11 18:43