CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Detecting a container's end

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Detecting a container's end

    Quote Originally Posted by monarch_dodra
    != Works for ALL flavors of iterators, no exceptions.
    Output iterators are an exception.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Detecting a container's end

    Quote Originally Posted by greve View Post
    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

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Detecting a container's end

    Quote Originally Posted by nuzzle View Post
    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
    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.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Detecting a container's end

    Quote Originally Posted by monarch_dodra View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Detecting a container's end

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured