-
September 12th, 2011, 09:27 AM
#1
Detecting a container's end
I'm used to scan through a container in a fashion like this:
Code:
vector<int> Data;
...
for (
vector<int>::iterator Val= Data.begin();
Val != Data.end();
++Val
)
{...}
I now saw in some other guy's code something similar but using operator< instead of !=. The code is working as expected but I doubt whether this is a safe alternative (because it's unlikely that all elements of the container will be in successive order).
Thanks for any comments!
-
September 12th, 2011, 09:30 AM
#2
Re: Detecting a container's end
Originally Posted by greve
I now saw in some other guy's code something similar but using operator< instead of !=. The code is working as expected but I doubt whether this is a safe alternative (because it's unlikely that all elements of the container will be in successive order).
It is a safe alternative for std::vector because the iterators of a std::vector are random access iterators, and operator< is available for random access iterators. However, it also means that the code will not work for say, forward iterators, which would be a shame if forward iterators will do.
-
September 14th, 2011, 02:13 AM
#3
Re: Detecting a container's end
!= Works for ALL flavors of iterators, no exceptions. [EDIT: Higher than output iterator]
< Works ONLY for RA iterators. At best, it < is no faster than !=, but can be slower....
Just like the i++ vs ++i debate, You are better of just using the more efficient !=, unless you have a very specific reason not to.
Last edited by monarch_dodra; September 14th, 2011 at 03:36 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 14th, 2011, 02:47 AM
#4
Re: Detecting a container's end
Originally Posted by monarch_dodra
!= Works for ALL flavors of iterators, no exceptions.
Output iterators are an exception.
-
September 14th, 2011, 03:38 AM
#5
Re: Detecting a container's end
..Forgot about those output iterators....
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 15th, 2011, 12:11 AM
#6
Re: Detecting a container's end
Originally Posted by greve
I'm used to scan through a container in a fashion like this:
A simple scan over just about anything can be done in a highlevel way using "foreach". It's coming in the new C++ standard but in the meantime one can use BOOST_FOREACH,
http://www.boost.org/doc/libs/1_47_0...l/foreach.html
-
September 15th, 2011, 01:31 AM
#7
Re: Detecting a container's end
Originally Posted by nuzzle
I personally avoid boost's for each: It doesn't actually bring any functionality, apart from prettier syntax, via some very hackish macro/template. Have you ever looked at the output of the processor after a FOREACH? It is a legit 30 solid lines of templates. Sure, the compiler inlines it all away, but still...
I've found that the good old fashion std::for_each works just as well, and can actually improve your program by more than just pretty syntax.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 15th, 2011, 02:37 AM
#8
Re: Detecting a container's end
Originally Posted by monarch_dodra
I personally avoid boost's for each: It doesn't actually bring any functionality, apart from prettier syntax, via some very hackish macro/template. Have you ever looked at the output of the processor after a FOREACH? It is a legit 30 solid lines of templates. Sure, the compiler inlines it all away, but still...
I've found that the good old fashion std::for_each works just as well, and can actually improve your program by more than just pretty syntax.
Most modern languages have a built-in foreach statement and so does C++11.
http://en.wikipedia.org/wiki/Foreach_loop
This means the Boost foreach will be somewhat sidestepped in the future. In think the Boost foreach has been a good substitute for a native foreach though. Template code has a tendency to be ugly and especially when automatically generated but who cares really. You don't have to torture yourself by looking at it. Boost foreach is efficient and offers a higher abstraction level and that's what counts to me. I leave the peeking into implementations behind the curtain to pervert masochists.
Should you use the C++11 foreach? I certainly will. In my view one should seek the highest abstraction level that's readily available. It's a sure way to improve a program. For the same reason I'm also going to use for example C++11 lambdas.
Last edited by nuzzle; September 15th, 2011 at 06:52 AM.
-
September 15th, 2011, 02:44 AM
#9
Re: Detecting a container's end
Originally Posted by monarch_dodra
I've found that the good old fashion std::for_each works just as well, and can actually improve your program by more than just pretty syntax.
I prefer to use more specific algorithms where applicable. I think std::for_each would be more useful with lambda functions. The thing is, although lambda functions have been introduced with C++11, the range based for loop makes this std::for_each + lambda function combination rather redundant for typical usage.
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
|