-
October 14th, 2003, 10:27 AM
#1
stl std::iterator
If you've got an iterator iter, how do you get an iterator pointing to the previous thing EASILY?
Like eg
Code:
while (iter!= (iter0 - 1))
No answers of the form
That's easy, you just declare another iterator:
Code:
Iterator iter1;
iter1 = iter;
--iter1;
0 points.
-
October 14th, 2003, 10:45 AM
#2
What is the significance of using a temporary variable? It will not affect the over performance.
Kuphryn
-
October 14th, 2003, 10:47 AM
#3
What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 14th, 2003, 01:10 PM
#4
Naughty, Yves - that's a trick question, dropping forward iterators into it, when he's asked for the previous.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 15th, 2003, 03:00 AM
#5
Originally posted by Yves M
What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?
-
October 15th, 2003, 03:24 AM
#6
It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 15th, 2003, 07:35 AM
#7
Originally posted by Graham
It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.
--iter is, like iter = iter-1;
-
October 15th, 2003, 07:56 AM
#8
Originally posted by c94wjpn
Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?
Well, because making a map's iterator random access would make you incurr big performance penalties. The ++ and -- operators for map's iterators are already amortized constant time, so it's a little slower than constant time. Getting a random access iterator over a map would be amortized linear time (so something like O(N log(log(N))) ).
It just doesn't make sense. Anyways, I don't see the problem in having a temporary iterator which points at the previous element.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 15th, 2003, 08:05 AM
#9
Originally posted by c94wjpn
No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.
--iter is, like iter = iter-1;
Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 15th, 2003, 08:10 AM
#10
Originally posted by Graham
Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
ok thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|