Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

how can i write the iterator about my struct

Question posted by: remlostime (Guest) on July 3rd, 2008 01:55 AM
struct nodeType
{
int v, index;
};
list<nodeTypenode[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?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Eric Pruneau's Avatar
Eric Pruneau
Guest
n/a Posts
July 3rd, 2008
02:25 AM
#2

Re: how can i write the iterator about my struct

"remlostime" <remlostime@gmail.coma 閏rit dans le message de news:
Join Bytes!...
Quote:
Originally Posted by
struct nodeType
{
int v, index;
};
list<nodeTypenode[1000];


an array of 1000 list<nodeType>... is it really what you want???
I guess you want a list of 1000 nodeType, Am I right???
Quote:
Originally Posted by
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<nodeTypemyList;
... // 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


------------------

Eric Pruneau



Salt_Peter's Avatar
Salt_Peter
Guest
n/a Posts
July 3rd, 2008
05:35 AM
#3

Re: how can i write the iterator about my struct
On Jul 2, 9:54 pm, remlostime <remlost...@gmail.comwrote:
Quote:
Originally Posted by
struct nodeType
{
int v, index;};
>
list<nodeTypenode[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::ostream& operator<<(std::ostream& 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::ostream_iterator< node >(std::cout, "\n") );
}

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








remlostime's Avatar
remlostime
Guest
n/a Posts
July 12th, 2008
04:15 PM
#4

Re: how can i write the iterator about my struct
On 7月3日, 下午1时30分, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
Originally Posted by
On Jul 2, 9:54 pm,remlostime<remlost...@gmail.comwrote:
>
Quote:
Originally Posted by
struct nodeType
{
int v, index;};

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

>
Quote:
Originally Posted by
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::ostream& operator<<(std::ostream& 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::ostream_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[i].begin();
iter != node[i].end(); iter++)
cout << (*iter)->v << ' ' << (*iter)->index << endl;
but it's wrong

joseph  cook's Avatar
joseph cook
Guest
n/a Posts
July 13th, 2008
05:45 AM
#5

Re: how can i write the iterator about my struct
On Jul 12, 12:13*pm, remlostime <remlost...@gmail.comwrote:
Quote:
Originally Posted by
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[i];
std::cout<<"i: "<<lis[i]<<" is a very good friend";
delete [] lis;
delete [] lis; // for good measure.
}

Joe Cook


joseph  cook's Avatar
joseph cook
Guest
n/a Posts
July 13th, 2008
06:05 AM
#6

Re: how can i write the iterator about my struct
On Jul 12, 12:13*pm, remlostime <remlost...@gmail.comwrote:
Quote:
Originally Posted by
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[i].begin();
* * iter != node[i].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


 
Not the answer you were looking for? Post your question . . .
182,266 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors