473,403 Members | 2,354 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,403 software developers and data experts.

STL iterator

Hi,

I am reading c++ primer, in the section about iteraor, it has an
example:

To iterate over the elements of any container type, we write
for (iter = container.begin(); iter != container.end(); ++iter)
do_soemthing_wtih_element (*iter);

why we write "++iter" instead of "iter++"?

Jan 24 '06 #1
4 1625
Allerdyce.J...@gmail.com wrote:
Hi,

I am reading c++ primer, in the section about iteraor, it has an
example:

To iterate over the elements of any container type, we write
for (iter = container.begin(); iter != container.end(); ++iter)
do_soemthing_wtih_element (*iter);

why we write "++iter" instead of "iter++"?


See this FAQ:

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

Cheers! --M

Jan 24 '06 #2
Al************@gmail.com wrote:
I am reading c++ primer, in the section about iteraor, it has an
example:

To iterate over the elements of any container type, we write
for (iter = container.begin(); iter != container.end(); ++iter)
do_soemthing_wtih_element (*iter);

why we write "++iter" instead of "iter++"?


This is covered in the FAQ, I believe. See section 13.
http://www.parashift.com/c++-faq-lite/

V
Jan 24 '06 #3
<Al************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I am reading c++ primer, in the section about iteraor, it has an
example:

To iterate over the elements of any container type, we write
for (iter = container.begin(); iter != container.end(); ++iter)
do_soemthing_wtih_element (*iter);

why we write "++iter" instead of "iter++"?


It will give a compiler a better chance of creating
efficient code. See FAQ for details:
http://www.parashift.com/c++-faq-lite/

Your question is answered in section 13 (But
be sure to read the whole FAQ, it contains
much useful information.)

-Mike
Jan 24 '06 #4
<Al************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
To iterate over the elements of any container type, we write
for (iter = container.begin(); iter != container.end(); ++iter)
do_soemthing_wtih_element (*iter);

why we write "++iter" instead of "iter++"?


The FAQ covers the efficiency aspect; but to me the reason is simpler than
that: because we want to point to the next element, nothing else...

Prefix increment, just increments; and in the case of iterators,
incrementing is pointing to the next element.

On the other hand; postfix increment stores the old state of the object,
increments the object, and returns the stored old state.

Since the for loop above only deals only with incrementing, the author of
the code uses prefix increment.

Ali

Jan 25 '06 #5

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.