473,386 Members | 1,830 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,386 software developers and data experts.

Iterator doubts, Decision on Iterator usage

Hello all,
I havent used STL containers much, as I am used to MFC
containers/classes always
..The differences that I could see is iterators and algorithms.
The algorithms providing some basic functinaloties which are also
present in respective MFc container classes,
I wanted to know what is the main powers in Iterators ??
I have written some test programs,stl containers, using iterators,and
some stl algorithms,
I feel the code has been simplified especially when iterating, But I
wanted to know
how to pickup or decide a right iterator, I mean a forward iter,
Backward iterator etc....

This should help me in future to think about using STL instead of MFC
?
Thanks
Greg
Jul 19 '05 #1
6 4340
"greg" <hi*********@yahoo.com> wrote...
I havent used STL containers much, as I am used to MFC
[...]
This should help me in future to think about using STL instead of MFC
?


Get two books: "The C++ Standard Library" by Josuttis and
"Effective STL" by Meyers. Those should help you.

Iterators are special types with very convenient semantics
that allow algorithms to operate on the iterator's container
without knowing what it is. You can use the same 'sort' for
any container whose iterators are of random access kind. You
can use copy with any container (even a C++ array, for whom
a pointer is an iterator). And so on...

Victor
Jul 19 '05 #2

"greg" <hi*********@yahoo.com> wrote in message
news:2b*************************@posting.google.co m...
Hello all,
I havent used STL containers much, as I am used to MFC
containers/classes always
.The differences that I could see is iterators and algorithms.
The algorithms providing some basic functinaloties which are also
present in respective MFc container classes,
I wanted to know what is the main powers in Iterators ??
I have written some test programs,stl containers, using iterators,and
some stl algorithms,
I feel the code has been simplified especially when iterating, But I
wanted to know
how to pickup or decide a right iterator, I mean a forward iter,
Backward iterator etc....

This should help me in future to think about using STL instead of MFC
? One reason to consider STL over MFC, is portability. Using the STL container
classes should make porting your code to other platforms less painful.

Scott Meyers' book "Effective STL" may answer some of your questions. As
deciding forwards, or backwords iteration, It really depends on how you want
to traverse the container. You can use either type of iterator (in most case
they are bidirectional). However if you want to look at the container in
reverse order, and use operator++ to move backwards, use the reverse
iterator. If you want to look at the container from the start to finish,
using operator++ to move forwards, use the forwards iterator.

There shouldn't be a penalty for choosing one or the other, however when I
think about iterating, I start from the first element, and work my way to
the last by incrementing the iterator. When I want to go in reverse, I start
from the last element, and work my way backwards by decrementing the
iterator. Mostly because I think to readers of my code, it'll make more
sense, than using a reverse iterator, but calling operator++ to go
backwards.

HTH,

Alan


Thanks
Greg

Jul 19 '05 #3
Thanks for the reply,
I have programmed using stl, But not too much
I could see iterator is a pointer to an element, in a container,
In that case, I can my self implement my own iterators , But only thing is
my iterator can be both incrmented and decremented, This should depend on
whether I have both ++ or -- overloaded, Bi-directional Iterator ,
Pls correct me if I am wrong,
I have see MSDN help files over iterators
Here is a brief text of the same
Msdn Text start----"
a.. OutIt -- An output iterator X can only have a value V stored indirectly
on it, after which it must be incremented before the next store, as in (*X++
= V), (*X = V, ++X), or (*X = V, X++).
a.. InIt -- An input iterator X can represent a singular value that
indicates end-of-sequence. If such an iterator does not compare equal to its
end-of-sequence value, it can have a value V accessed indirectly on it any
number of times, as in (V = *X). To progress to the next value, or
end-of-sequence, you increment it, as in ++X, X++, or (V = *X++). Once you
increment any copy of an input iterator, none of the other copies can safely
be compared, dereferenced, or incremented.
a.. FwdIt -- A forward iterator X can take the place of an output iterator
(for writing) or an input iterator (for reading). You can, however, read
(via V = *X) what you just wrote (via *X = V) through a forward iterator.
And you can make multiple copies of a forward iterator, each of which can be
dereferenced and incremented independently.
"
Msdn Text end----------
Can some body explain some kind of difference between above 3,
If I use a Bi directional Iterator, it would serve all the pourpses of
out,in, Fwd, Then why should I make a decision between iterators ????
Is there any over head using a Bi directrional Iterator,I meant to say,
performance,

My general feeling is that iterators would be most useful when coupled with
Algorithms ....
I would also try looking into books specified by above two people,,
Thanks
Vijay
Jul 19 '05 #4
"Michiel Salters" <Mi*************@cmg.nl> wrote...
hi*********@yahoo.com (greg) wrote in message news:<2b*************************@posting.google.c om>...
Hello all,
I havent used STL containers much, as I am used to MFC
containers/classes always
.The differences that I could see is iterators and algorithms.
The algorithms providing some basic functinaloties which are also
present in respective MFc container classes,
I wanted to know what is the main powers in Iterators ??
I have written some test programs,stl containers, using iterators,and
some stl algorithms,
I feel the code has been simplified especially when iterating, But I
wanted to know
how to pickup or decide a right iterator, I mean a forward iter,
Backward iterator etc....


There are a number of reasons why iterators are useful. Say you want
to sort only the first half of a vector. If sort was a member of the
class, you couldn't, but with iterators you can.


This is nonsense. If sort were a member, I could give it two
arguments: where to start and where to end. Just like we now
do with iterators. So, let's assume you didn't write that
paragraph...
Another reason is that you can write iterators for your own
collections which allows you to use the <algorithm>s in the
standard library.
That's the ticket!
A third reason is that you can write a single function taking a
pair iterators and have it work on all standard containers plus
any other container written that has iterators.
....which is another side of the same coin. Implementers of
the Standard Library write functions that can be used with
any iterators.
A fourth reason is that iterators provide a common notion of
"element not found", by returning the last iterator (the one
that doesn't point to an element). This is of course not new,
arrays worked the same but now the same notion can also be
used on linked lists.


I am not sure how this is special.

The often missed beauty of iterators is that they are very
similar to pointers in semantics. What pointers are to
arrays, iterators are to containers.

Victor
Jul 19 '05 #5
The reply by Mr. Kheul was very useful
Now I understan that InIt will not work on a container but rather in a input
sequence like from theuser,
But How will the ietrator incremented , I mean on ite incrementation which
memory location will it read as it is like a pointer ???
A code snippet will be mroe useful
Regards
Vijay
Jul 19 '05 #6
"vijay" <ge********@yahoo.com> wrote:
The reply by Mr. Kheul was very useful
Assuming you std::reverse()d the middle of my name: it was the intend to be
useful. However...
Now I understan that InIt will not work on a container but rather in a input
sequence like from theuser,
.... this is not at all what I meant! An input iterator is simply used for
read-only sequences where it is not reasonable to provide multi-pass or
multi-position semantics. Typically, a container is capable to provide both
and it is thus reasonable to also provide additional capabilities. However,
I can imagine data structures which mutate while being accessed (splay-trees
come to mind although these don't fit the problem...) making eg. multi-pass
infeasible.
But How will the ietrator incremented , I mean on ite incrementation which
memory location will it read as it is like a pointer ???
Take a look eg. at 'std::istream_iterator<T>' (abbreviated as 'IsIt' below).
Essentially, this iterator internally stores a 'T' object and a pointer to a
stream. The basic operations work like this:

- the ctor and operator++() attempt to read a 'T' object:

IsIt::IsIt(): mStream(0) {} // to create a "past the end" iterator
IsIt::IsIt(std::istream& s): mStream(&s) { ++*this; }
IsIt::operator++() { *s >> mVal; return *this; }

- operator*() merely returns the internally stored value:

T const& IsIt::operator*() { return mVal; }

- operator==() checks whether both streams are OK or both are not OK:

bool IsIt::operator==(IsIt const& it) const {
return (mStream && *mStream) == (it.mStream && *it.mStream);
}

This assumes that 'IsIt' has two members:
- 'mStream' which is a pointer to an 'std::istream'
- 'mVal' which is a 'T' object

Actually, a setting like this is quite common for input iterators. Output
iterators are similar but don't have the comparision operator. Instead, they
typically implement some form of assignment operator to capture the mutation.
Of course, the above is hacked for this discussion. A real implementation
will probably deviate from the above excerpts.
A code snippet will be mroe useful


Will it? Why? Iterators are specified in much detail. Just fulfill their
contract and you're done.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #7

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...
4
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
3
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through...
2
by: Christoph Heindl | last post by:
Hi, I'm currently struggling with a design decision. One of my classes (named Attributable) has a protected member of type std::list<StrongReference<Attribute> > Basically the list has...
3
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am...
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...
15
by: jayesah | last post by:
Hi All, List and its iterator work as following way : list<intmylist; list<int>::iterator itr; itr = mylist.begin(); cout << (*itr); But I want something like this:
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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?
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
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,...
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...

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.