473,395 Members | 1,568 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.

confusion with insert iterator

Hi, all:
I am reading the book "The C++ Stadnard Library" by Josuttis. I have
trouble to understand
operations for the insert iterator(page 272).
++itr, *itr and itr++ are defined as no-op, and "itr = value"(supported
expression *itr = value") is
defined as "inserts value"
The example given in the book is:
vector<int> coll;
back_insert_iterator<vector<int> > itr(coll);
....
*itr = 1;
itr++;
*itr = 2;
*itr++;
....
Here is my question, if "itr++" defined as no-op what is the purpose of
using itr++ in the above
example? why could we use just:
*itr = 1;
*itr = 2;
instead?
Thanks.

Jul 23 '05 #1
2 1390
we*****@yahoo.com wrote:
Here is my question, if "itr++" defined as no-op what is the purpose of
using itr++ in the above
example? why could we use just:
*itr = 1;
*itr = 2;
instead?


You could, but that shortened code won't work right with other kinds of
iterators. If you're writing a general-purpose algorithm you need to
work with all writable iterators, and that means you have to increment
the iterator.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
we*****@yahoo.com wrote:
Hi, all:
I am reading the book "The C++ Stadnard Library" by Josuttis. I have
trouble to understand
operations for the insert iterator(page 272).
++itr, *itr and itr++ are defined as no-op, and "itr = value"(supported
expression *itr = value") is
defined as "inserts value"
The example given in the book is:
vector<int> coll;
back_insert_iterator<vector<int> > itr(coll);
...
*itr = 1;
itr++;
*itr = 2;
*itr++;
...
Here is my question, if "itr++" defined as no-op what is the purpose of
using itr++ in the above example? why could we use just:
*itr = 1;
*itr = 2;
instead?


That would only work with an insert iterator. If you include the operator++
call, you can later exchange the iterator with a forward iterator, and the
code will still work.
In the case of templates, the above might be part of one that doesn't even
know which type of iterator to expect. Think of std::copy. It will do
something similar to:

while (itr1 != end)
{
*itr2 = *itr1;
++itr1;
++itr2;
}

And when you use std::copy with an insert iterator for itr2, it will still
do the increment, since it doesn't care what type of iterator you give it.

Jul 23 '05 #3

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...
8
by: Rom | last post by:
I'm a bit confused as to how the STL set works with the <setname>.insert() and <setname>.find() functions Compiler used : CodeWarrior 5.0 My intial interpretation was that I needed to overload...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
6
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
0
by: A Taylor | last post by:
Hello, I am getting the following error using .NET 2003 and I wonder if anyone can help me understand what is going on. godcDoc.cpp(228) : warning C4267: '=' : conversion from 'size_t' to...
6
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
4
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it -...
1
by: Tim H | last post by:
Is there an STL or similar container that behaves like a map (string keys or tempate<Tkeykeys with template<Tvalvalues) but can be iterated in insert order? I whipped up a keyed-vector as a...
2
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.