473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple iterator question

Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?

2. How (can I?) pass the iterator to another function? What prototype would
I use?

Thanks for your help

Regards

Michael
Mar 27 '07 #1
4 1449
"michael" <sp**@begone.netwrote in message
news:46**********************@per-qv1-newsreader-01.iinet.net.au...
Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying
to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I
have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?
Operator precidence. Try
(*it).memb_name();
2. How (can I?) pass the iterator to another function? What prototype
would
I use
I think
void MyFunc( std::list<MyClass>::iterator it )
should work, but have never tried.
Mar 27 '07 #2
michael wrote:
Hi All,
[redacted]

int main(){
list<MyClass myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
Prefer the prefix form of increment: ++it
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()?
Look up the releative precedences of unary operator* and operator. Use
either (*it).memb_name(), or it->memb_name().
2. How (can I?) pass the iterator to another function? What prototype would
I use?
void f(list<MyClass>::iterator);

for (it = myList.begin(); it != myList.end(); ++it)
f(it);
Mar 27 '07 #3

"michael" <sp**@begone.netwrote in message
news:46**********************@per-qv1-newsreader-01.iinet.net.au...
Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying
to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I
have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?

2. How (can I?) pass the iterator to another function? What prototype
would
I use?

Thanks for your help

Regards

Michael


1)
Did you try it->memb_name?
Or (*it).member_name?
*it.member_name doesn't work because you are trying to dereference the whole
thing.
Same reason you had to use parens with the list of pointers, where it was
(*it)->member_name;

2)
why not?
void someFunc( list<MyClass>::iterator it)
{
}

Mar 27 '07 #4
On Mar 27, 3:59 am, red floyd <no.s...@here.dudewrote:
michael wrote:
for(it = myList.begin(); it != myList.end(); it++){

Prefer the prefix form of increment: ++it
It might be worth elaborating on the reasons why:

http://www.parashift.com/c++-faq-lit...html#faq-13.15

Mar 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neal D. Becker | last post by:
x = for i in x: i = 2 This doesn't change x. 2 questions: 1) Why not? Why doesn't assign to an iterator of a mutable type change the underlying object?
4
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one...
10
by: JustSomeGuy | last post by:
I have a few classes... class a : std::list<baseclass> { int keya; }; class b : std::list<a> { int keyb;
2
by: solartimba | last post by:
Hi, I am in another country, and I need to finish a program. But I do not have any reference books. Can you tell me how to print this to a file without ending the stream with an endl. Stated...
1
by: Steve H | last post by:
From "C++ Programming Language (3rd Edition)" I understand that there are 5 standard iterator categories defined in the "std" namespace: Output *p= p++ Input =*p -> ++ == !=...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
6
by: RishiD | last post by:
Hi, I am using an iterator on a list, I wanted to know is there a simple way to erase() the current element the iterator is pointing to and move the iterator forward one? The only way I can...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.