yuval
yuval

Reputation: 3088

how to access the list inside a list in c++?

Okay, so I first started learning C# and now I am learning C++ so I'm kind of new to it.
In C#, when I wanted to access the list inside a list, I would simply use nested for-each loops.

Now since in C++ I don't know how to use a for-each loop, I tried to access the nested list with for loops.

Here is my code:

int main

{      
    list<list<char *> > moves; 

    list<char *> pointers;

    list<list<char> > undoValues;
    list<char> undoValue;

    for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++)
    {
        for(list<char *>::iterator inner=outer.begin();inner!=outer.end();inner++)
        {

        }
    }
}

I get 2 errors:

error 'struct std::_List_iterator<std::list<char*>,std::allocator<char *> > >' has no member named begin

error 'struct std::_List_iterator<std::list<char*>,std::allocator<char *> > >' has no member named end

How can I access the nested list?

Upvotes: 0

Views: 2204

Answers (4)

Mike Seymour
Mike Seymour

Reputation: 254461

To access members of the object an iterator refers to, you need indirection; so the begin() member of the outer list is outer->begin(), not outer.begin(). Likewise for end().

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227400

You need to dereference the iterators to get to the element. You can either use * or ->:

for(list<char *>::iterator inner=outer->begin();inner!=outer->end();inner++)

or

for(list<char *>::iterator inner=(*outer).begin();inner!=(*outer).end();inner++)

Upvotes: 2

Jarosław Gomułka
Jarosław Gomułka

Reputation: 4995

for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++)
    for(list<char *>::iterator inner=outer->begin();inner!=outer->end();inner++)

Upvotes: 1

Vaughn Cato
Vaughn Cato

Reputation: 64308

You need to use (*outer) to get what the iterator is pointing to:

list<char *> pointers;

list<list<char> > undoValues;
list<char> undoValue;
for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++)
{
    for(list<char *>::iterator inner=(*outer).begin();inner!=(*outer).end();inner++)
    {

    }
}

Upvotes: 1

Related Questions