473,695 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4366
"greg" <hi*********@ya hoo.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*********@ya hoo.com> wrote in message
news:2b******** *************** **@posting.goog le.com...
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*********@yah oo.com (greg) wrote in message news:<2b******* *************** ***@posting.goo gle.com>...
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********@yah oo.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_i terator<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.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
Jul 19 '05 #7

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

Similar topics

38
3665
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 crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
4
3065
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
2281
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 the map, which means I must iterate through all of the sets in the map. I need an iterator that points to the set where the integer was found. How can I do this? What will the type of this iterator be? Can you help me with a code snippet?
2
2688
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 strong references that point to Attribute's. In Attributable i want to provide an stl like interface for bidirectional iterators like:
3
2689
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 using , i use mutex for thread safety. Now comes the question. Please answer this question assuming that i am not using a thread safe stl version. I obtain an iterator to the map from inside the critical section(protected by mutex). If i use...
0
2676
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
21
5708
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 classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
16
2578
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 totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
15
2787
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
8555
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
9112
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...
1
8824
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,...
1
6484
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5831
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4336
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...
0
4571
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1970
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.