473,587 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"(supporte d
expression *itr = value") is
defined as "inserts value"
The example given in the book is:
vector<int> coll;
back_insert_ite rator<vector<in t> > 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 1402
we*****@yahoo.c om 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.c om 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"(supporte d
expression *itr = value") is
defined as "inserts value"
The example given in the book is:
vector<int> coll;
back_insert_ite rator<vector<in t> > 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
3653
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 mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a...
8
2484
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 the '<' for the insert() and '==' operator for find() however it doesn't seem to be the case In fact it seems to only need to overload the '<'...
18
2281
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 of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
6
6997
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: http://groups-beta.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/fb75b00f73e979db/018b8d0eadb38dbf?q=%22STL+insert+with+hint%22+%22Mark+P%22&rnum=1&hl=en#018b8d0eadb38dbf I quoted the SGI STL docs describing a.insert(p, t), where p is the hint iterator and...
0
1890
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 'int', possible loss of data C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(621) : error C2440: 'initializing' : cannot
6
6525
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
4
6530
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 - and insert an entry in that place some time later. There's an "insert" member function, but it takes an iterator to designate the place to insert....
1
4224
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 wrapper around vector, but I can't help think there's a better answer already implemented... Tim
2
2393
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 n, const T& x); template <class InputIterator> void insert(iterator position, InputIterator first, InputIterator last);
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.