how can i write the iterator about my struct

R

remlostime

struct nodeType
{
int v, index;
};
list<nodeType> node[1000];
for(list<nodeType>::iter = node[q[qIndex]].begin();
iter != node[q[qIndex]].end(); iter++)

What's wrong with it? how can i fix it?
 
E

Eric Pruneau

remlostime said:
struct nodeType
{
int v, index;
};
list<nodeType> node[1000];

for(list<nodeType>::iter = node[q[qIndex]].begin();
iter != node[q[qIndex]].end(); iter++)

What's wrong with it? how can i fix it?

If you want an iterator on a list you should do

list<nodeType> myList;
... // put some node in the list
list<nodeType>::iterator myIt = myList.begin();

Now myIt is an iterator for the list myList

BTW, there is also a const_iterator, a reverse_iterator and a
const_reverse_iterator
 
S

Salt_Peter

struct nodeType
{
int v, index;};

list<nodeType> node[1000];
for(list<nodeType>::iter = node[q[qIndex]].begin();
iter != node[q[qIndex]].end(); iter++)

What's wrong with it? how can i fix it?


As already pointed out, node[1000] is one thousand (empty) elements of
the type: list<nodeType>
I doubt that was the intention.

Like most containers, a construct is available to create a loaded
container.
std::list< node > nodes(1000);
Thats one list with 1000 node elements
The question is: in what state are those elements?

If you are planning to use a zero indexed sequenced container, why not
just:
std::vector< int > v(1000, 0); // poof - instant index, all nodes
intialized

Your list has a begin() and end() member function that return the
first element and one past the last element as passed to the std::copy
algo below...

#include <iostream>
#include <ostream>
#include <list>
#include <algorithm>
#include <iterator>

struct node
{
int index;
int value;
node() : index(0), value(0) { }
node(int i, int v) : index(i), value(v) { }
};

std::eek:stream& operator<<(std::eek:stream& os, const node& n)
{
os << "index: " << n.index;
os << "\tvalue: " << n.value;
return os;
}

int main()
{
std::list< node > container(10);
std::copy( container.begin(),
container.end(),
std::eek:stream_iterator< node >(std::cout, "\n") );
}

/*
index: 0 value: 0 // 10 times
*/
 
R

remlostime

struct nodeType
{
int v, index;};
list<nodeType> node[1000];
for(list<nodeType>::iter = node[q[qIndex]].begin();
iter != node[q[qIndex]].end(); iter++)
What's wrong with it? how can i fix it?

As already pointed out, node[1000] is one thousand (empty) elements of
the type: list<nodeType>
I doubt that was the intention.

Like most containers, a construct is available to create a loaded
container.
std::list< node > nodes(1000);
Thats one list with 1000 node elements
The question is: in what state are those elements?

If you are planning to use a zero indexed sequenced container, why not
just:
std::vector< int > v(1000, 0); // poof - instant index, all nodes
intialized

Your list has a begin() and end() member function that return the
first element and one past the last element as passed to the std::copy
algo below...

#include <iostream>
#include <ostream>
#include <list>
#include <algorithm>
#include <iterator>

struct node
{
int index;
int value;
node() : index(0), value(0) { }
node(int i, int v) : index(i), value(v) { }

};

std::eek:stream& operator<<(std::eek:stream& os, const node& n)
{
os << "index: " << n.index;
os << "\tvalue: " << n.value;
return os;

}

int main()
{
std::list< node > container(10);
std::copy( container.begin(),
container.end(),
std::eek:stream_iterator< node >(std::cout, "\n") );

}

/*
index: 0 value: 0 // 10 times
*/

i wanna create an array of list, and the number of list is 1000, and
now i wanna to go through the element in every list, how can i do?
like this?
for(int i = 0; i < 1000; i++)
for(list<nodeType>::iterator iter = node.begin();
iter != node.end(); iter++)
cout << (*iter)->v << ' ' << (*iter)->index << endl;
but it's wrong
 
J

joseph cook

i wanna create an array of list, and the number of list is 1000, and
now i wanna to go through the element in every list, how can i do?
like this?

You have 1000 lists, all of which have 0 elements. (you haven't put
anything on them). So you can go through all of them by:

for(int i=0; i !=0; ++i)
{
double* lis = new double;
std::cout<<"i: "<<lis<<" is a very good friend";
delete [] lis;
delete [] lis; // for good measure.
}

Joe Cook
 
J

joseph cook

i wanna create an array of list, and the number of list is 1000, and
now i wanna to go through the element in every list, how can i do?
like this?
for(int i = 0; i < 1000; i++)
 for(list<nodeType>::iterator iter = node.begin();
    iter != node.end(); iter++)
    cout << (*iter)->v << ' ' << (*iter)->index << endl;
but it's wrong


Seriously though, if you really want to do this, you are close...you
are just over complicating it.

the last line should be:
cout << iter->value << ' ' << iter->index << endl;

Joe Cook
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top