Connecting Tech Pros Worldwide Help | Site Map

how can i write the iterator about my struct

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 3rd, 2008, 01:55 AM
remlostime
Guest
 
Posts: n/a
Default how can i write the iterator about my struct

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?

  #2  
Old July 3rd, 2008, 02:25 AM
Eric Pruneau
Guest
 
Posts: n/a
Default Re: how can i write the iterator about my struct


"remlostime" <remlostime@gmail.coma 閏rit dans le message de news:
d7062a5f-2955-4467-94b8-81e618c73055...oglegroups.com...
Quote:
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:
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


  #3  
Old July 3rd, 2008, 05:35 AM
Salt_Peter
Guest
 
Posts: n/a
Default Re: how can i write the iterator about my struct

On Jul 2, 9:54 pm, remlostime <remlost...@gmail.comwrote:
Quote:
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
*/







  #4  
Old July 12th, 2008, 04:15 PM
remlostime
Guest
 
Posts: n/a
Default Re: how can i write the iterator about my struct

On 7月3日, 下午1时30分, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Jul 2, 9:54 pm,remlostime<remlost...@gmail.comwrote:
>
Quote:
struct nodeType
{
int v, index;};
>
Quote:
list<nodeTypenode[1000];
for(list<nodeType>::iter = node[q[qIndex]].begin();
iter != node[q[qIndex]].end(); iter++)
>
Quote:
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
  #5  
Old July 13th, 2008, 05:45 AM
joseph cook
Guest
 
Posts: n/a
Default Re: how can i write the iterator about my struct

On Jul 12, 12:13*pm, remlostime <remlost...@gmail.comwrote:
Quote:
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

  #6  
Old July 13th, 2008, 06:05 AM
joseph cook
Guest
 
Posts: n/a
Default Re: how can i write the iterator about my struct

On Jul 12, 12:13*pm, remlostime <remlost...@gmail.comwrote:
Quote:
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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.