473,655 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Input iterators?

In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain the
copy.

Or does iterator mean something more complicated?
Apr 24 '07 #1
8 2264
desktop wrote:
In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain the
copy.

Or does iterator mean something more complicated?
To simplify the concept, the iterators enclose the idea of pointer to a
set element. The set can be a string or a vector, and in this case it's
very similar to a classical pointer, but it can be a list, a map or any
other conceptual set, and the behaviour of the iterator is not always so
straightforward .

So, they are conceptually more abstracted than a pointer, that is a very
low level idea.

Regards,

Zeppe
Apr 24 '07 #2
On 2007-04-24 19:01, desktop wrote:
In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain the
copy.

Or does iterator mean something more complicated?
An iterator is a concept in C++, anything that fulfills the requirements
can be used as iterators. In most (all?) cases a simple pointer does
fulfill the requirements of an iterator and can thus be use where an
iterator is required.

But an iterator can be so much more, consider for example std::map,
which is usually implemented as a RB-tree, std::map has a number of
methods that returns iterators, you can as an example iterate through
all elements in the map, this you can not do with a pointer since it
would require that all elements were contiguously laid out in memory.
The same goes for std::list which is a double-linked list.

To take the example with a string (a C++ one, and not a C char array)
there is no guarantee that the second char in the string is on the
memory-location after the first, but using iterators we let them worry
about that and just increment it to get an iterator to the next character.

--
Erik Wikström
Apr 24 '07 #3
Erik Wikström wrote:
On 2007-04-24 19:01, desktop wrote:
>In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain
the copy.

Or does iterator mean something more complicated?

An iterator is a concept in C++, anything that fulfills the requirements
can be used as iterators. In most (all?) cases a simple pointer does
fulfill the requirements of an iterator and can thus be use where an
iterator is required.

But an iterator can be so much more, consider for example std::map,
which is usually implemented as a RB-tree, std::map has a number of
methods that returns iterators, you can as an example iterate through
all elements in the map, this you can not do with a pointer since it
would require that all elements were contiguously laid out in memory.
The same goes for std::list which is a double-linked list.

To take the example with a string (a C++ one, and not a C char array)
there is no guarantee that the second char in the string is on the
memory-location after the first, but using iterators we let them worry
about that and just increment it to get an iterator to the next character.
Ok so reducing iterators to pointers only works if the only structure
that are used is an integer array (under the assumption that elements in
integer arrays are placed after each other in memory).

In other cases this assumption does not necessary hold and therefore its
necessary to use an iterator which I assume it designed to work on a lot
of different structures which makes it good in generic programming.
Apr 24 '07 #4
"desktop" <ff*@sss.comwro te in message
news:f0******** **@news.net.uni-c.dk...
In accelerated C++ on page 146 there is this example:
....
They say that the function takes 3 iterators, but is that not just another
name for a pointer?
It would be more accurate to say that a pointer is a kind of iterator, but
that there are other kinds of iterators too.

Please reread the discussion that starts near the bottom of page 144 and
continues on page 145. Also note that the book doesn't actually talk about
pointers until page 170, on the basis that once you understand iterators,
pointers are easy to understand.
Apr 24 '07 #5
On 2007-04-24 20:01, desktop wrote:
Erik Wikström wrote:
>On 2007-04-24 19:01, desktop wrote:
>>In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain
the copy.

Or does iterator mean something more complicated?

An iterator is a concept in C++, anything that fulfills the requirements
can be used as iterators. In most (all?) cases a simple pointer does
fulfill the requirements of an iterator and can thus be use where an
iterator is required.

But an iterator can be so much more, consider for example std::map,
which is usually implemented as a RB-tree, std::map has a number of
methods that returns iterators, you can as an example iterate through
all elements in the map, this you can not do with a pointer since it
would require that all elements were contiguously laid out in memory.
The same goes for std::list which is a double-linked list.

To take the example with a string (a C++ one, and not a C char array)
there is no guarantee that the second char in the string is on the
memory-location after the first, but using iterators we let them worry
about that and just increment it to get an iterator to the next character.

Ok so reducing iterators to pointers only works if the only structure
that are used is an integer array (under the assumption that elements in
integer arrays are placed after each other in memory).
Any array will do, since arrays are guaranteed to be a contiguous piece
of memory.
In other cases this assumption does not necessary hold and therefore its
necessary to use an iterator which I assume it designed to work on a lot
of different structures which makes it good in generic programming.
Yes, each class, such as vector, map and list, has it's own iterator-
type. Consider the following code:

std::vector<int vec;
typedef std::vector<int >::iterator iter;

for (iter i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;

Then you can just replace std::vector<int with, for example,
std::list<std:: stringand it will work.

--
Erik Wikström
Apr 24 '07 #6
Erik Wikström wrote:
On 2007-04-24 20:01, desktop wrote:
>Erik Wikström wrote:
>>On 2007-04-24 19:01, desktop wrote:
In accelerated C++ on page 146 there is this example:

template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}

They say that the function takes 3 iterators, but is that not just
another name for a pointer?

If we are dealing with a string begin would be a pointer to the
first char, end would be a pointer to the last char and dest would
contain the copy.

Or does iterator mean something more complicated?

An iterator is a concept in C++, anything that fulfills the
requirement s can be used as iterators. In most (all?) cases a simple
pointer does fulfill the requirements of an iterator and can thus be
use where an iterator is required.

But an iterator can be so much more, consider for example std::map,
which is usually implemented as a RB-tree, std::map has a number of
methods that returns iterators, you can as an example iterate through
all elements in the map, this you can not do with a pointer since it
would require that all elements were contiguously laid out in memory.
The same goes for std::list which is a double-linked list.

To take the example with a string (a C++ one, and not a C char array)
there is no guarantee that the second char in the string is on the
memory-location after the first, but using iterators we let them
worry about that and just increment it to get an iterator to the next
character.

Ok so reducing iterators to pointers only works if the only structure
that are used is an integer array (under the assumption that elements
in integer arrays are placed after each other in memory).

Any array will do, since arrays are guaranteed to be a contiguous piece
of memory.
>In other cases this assumption does not necessary hold and therefore
its necessary to use an iterator which I assume it designed to work on
a lot of different structures which makes it good in generic programming.

Yes, each class, such as vector, map and list, has it's own iterator-
type. Consider the following code:

std::vector<int vec;
typedef std::vector<int >::iterator iter;

for (iter i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;

Then you can just replace std::vector<int with, for example,
std::list<std:: stringand it will work.
Ok I thought that a pointer or an iterator was just a number, but it
seems that the compiler treats them as different types:

int myints[] = {1,2,3,4,5,1,2, 3,4,5};
std::vector<int myvector (myints,myints+ 10);

int match1[] = {1,2,3};

int ff = match1; // gives an error indicating that match1 is an int*
int gg = myvector.begin( ); // gives an error indicating that
myvector.begin( ) is an iterator.
BTW: Why is this legal:
std::vector<int myvector (myints,myints+ 10);

on this page:
http://www.cppreference.com/cppvecto...structors.html

it should match:

vector( input_iterator start, input_iterator end );

but as just described 'myints' like 'match1' are int* and not iterators.

Apr 24 '07 #7
desktop wrote:
Ok I thought that a pointer or an iterator was just a number, but it
seems that the compiler treats them as different types:
The iterator is a class in the most general situation. Actually, there
are not specific constraints on the type of the iterators, but rather on
the operation they support, as in http://www.cppreference.com/iterators.html
BTW: Why is this legal:
std::vector<int myvector (myints,myints+ 10);

on this page:
http://www.cppreference.com/cppvecto...structors.html

it should match:

vector( input_iterator start, input_iterator end );

but as just described 'myints' like 'match1' are int* and not iterators.

an input iterator is defined as an object that can be compared (operator
==), incremented (operator++) and dereferenced (operator&) to read the
associated container values. That's it. Everything that supports these
three operations is an iterator... so, there is nothing that prevents
the int* to be used as an iterator. Of course, you have to take care
that the pointer actually iterates on something that is allocated
properly ^^

Regards,

Zeppe
Apr 25 '07 #8
On Apr 24, 10:32 pm, desktop <f...@sss.comwr ote:
Erik Wikström wrote:
[...]
Ok I thought that a pointer or an iterator was just a number, but it
seems that the compiler treats them as different types:
At a certain level, everything is just a number (or a collection
of numbers). At that level, you're generally programming in
assembler. C++ is a typed language, and pointers are definitly
not numbers.
int myints[] = {1,2,3,4,5,1,2, 3,4,5};
std::vector<int myvector (myints,myints+ 10);
int match1[] = {1,2,3};
int ff = match1; // gives an error indicating that match1 is an int*
int gg = myvector.begin( ); // gives an error indicating that
myvector.begin( ) is an iterator.
BTW: Why is this legal:
std::vector<int myvector (myints,myints+ 10);
on this page:http://www.cppreference.com/cppvecto...structors.html
it should match:
vector( input_iterator start, input_iterator end );
but as just described 'myints' like 'match1' are int* and not
iterators.
Well, the template mechanism doesn't care. All it requires is
that the two parameters have the same type. The actual
implementation of vector supposes, however, that these two types
meet the constraints of an iterator, i.e. you can increment
them, dereference them, etc. And iterators (and their
constraints) were designed so that pointers meet these
constraints: a pointer acts like an iterator.

(Note that there is a very special case here, and something
like:

std::vector< int v( 20, 42 ) ;

also invokes this constructor... which due to some tricky
template programming, doesn't treat the integers as iterators.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 26 '07 #9

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

Similar topics

10
2203
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*) where I want to receive tuples of (item1, item2, item3) from the iterables. But it doesn't work well for a situation like: zip(*tuple_iter)
2
1251
by: Azumanga | last post by:
Hello. A small question with regards input iterators. Table 72 of the standard says with regards ++r on an input iterator r: "any copies of the previous value of r are no longer required to be dereferencable or in the domain of ==" Consider therefore the following code segment, where r is a currently
6
1712
by: Fraser Ross | last post by:
Algorithms cannot be used with input stream iterators? Is copying the range to a temporary container before using the algorithm the usual thing to do? Fraser.
1
1892
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range // (i.e. as rbegin, rend was passed) } How can I call lexicographical_compare inside the function so that it traverses the range backwards?
8
1988
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a function where I with the help of iterators want to search through the container and remove a certain object in the container. Here is part of the code in that function:
24
3941
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = s2 = s3 = for value in ???(s1, s2, s3):
8
2439
by: Mateusz Åoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
90
3408
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
33
2798
by: cesco | last post by:
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution...
0
8296
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
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
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.