Thread: passing a vector so it will be iterated in reverse

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    passing a vector so it will be iterated in reverse

    Hi,

    this is what I'm trying to dor:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>
    
    using namespace std;
    
    void func(vector<char>::const_iterator start, vector<char>::const_iterator end)
    {
    	for_each(start, end, cout << boost::lambda::_1 << " ");
    }
    
    int main()
    {
    	vector<char> v;
    	v.push_back('A');
    	v.push_back('B');
    	v.push_back('C');
    
    	func(v.begin(), v.end());  // prints "A B C"
    	cout << endl;
    
    	func(v.rbegin(), v.rend()); // error C2664: 'func' : cannot convert
               //parameter 1 from 'std::reverse_iterator<_RanIt>' to 'std::_Vector_const_iterator<_Ty,_Alloc>'
    	cout << endl;
    }
    I've never used reverse iterating so far. How would I have to change the example so the vector gets printed out in reverse (without changing func) like "C B A"?

    Thank you in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not
    func(v.end(), v.begin());
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Because func will do start++ in the hope to get to the next element. The minimal example doesn't work then, too. An assertion is thrown

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Since forward and reverse iterators are a different type, why not use templates?

    Code:
    template <class Iterator>
    void func(Iterator start, Iterator end)
    {
    	for_each(start, end, cout << boost::lambda::_1 << " ");
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Wow, thanks, that seems to do the job.
    I've no experience with creating templates. Is there a way to restrict the compiler to generate the code for my func-template only if Iterator is of type

    • vector<char>::const_iterator
    • vector<char>::iterator
    • vector<char>::const_reverse_iterator
    • vector<char>::reverse_iterator


    ?

    Else it would be pretty insecure to let clients call that code (with arbitrary types), wouldn't it?

    Thank you!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if they don't have the operators or functions that your code uses, it won't compile, so it's still relatively safe.
    Otherwise they could send their own iterators to your function and it should work fine as long as they follow how the STL iterators were made.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thanks, that sounds reasonable.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Why not
    func(v.end(), v.begin());
    ?
    Are you nuts?!
    'end' is not dererencable, and
    'begin' is not one past the last real element, and
    those are still forward iterators, that will of course attempt to iterate forwards.

    It's posts like this where I'm reminded of how much you really know about C++, contrary to that way you've got half the beginners here thinking you're an expert.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am not knowledgeable about the standard library, iterators, etc.
    Sorry, it was a stab in the dark.
    It just made sense to pass them in reverse. I don't know how for_each works.
    But I do know how std::vector.end() works. I know that it points to one element beyond the array.
    Last edited by Elysia; 02-27-2008 at 12:39 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    Wow, thanks, that seems to do the job.
    I've no experience with creating templates. Is there a way to restrict the compiler to generate the code for my func-template only if Iterator is of type
    Look at boost::enable_if. This is what it's for.

    Else it would be pretty insecure to let clients call that code (with arbitrary types), wouldn't it?
    I wouldn't chalk it up as a security problem, just a design problem. If a type should only be an iterator, then you should enforce that. But ultimately it will be enforced at compile time if you pass a type that is not suitable. You'll get an error message either way, it's just a question of which error it is.

    EDIT: Also, I'd allow pointer types as well as iterators. Iterators are basically generalized pointers, so a pointer can be seen as a kind of iterator (and the standard algorithms accept them as iterators, so you should probably follow that).
    Last edited by brewbuck; 02-27-2008 at 12:27 PM.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by iMalc View Post
    Are you nuts?!
    'end' is not dererencable, and
    'begin' is not one past the last real element, and
    those are still forward iterators, that will of course attempt to iterate forwards.

    It's posts like this where I'm reminded of how much you really know about C++, contrary to that way you've got half the beginners here thinking you're an expert.
    Oh oh, time to decrement that "Quoted 4 times" line.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cpjust View Post
    Oh oh, time to decrement that "Quoted 4 times" line.
    Heh. No, I don't think so. It has a great effect when I help someone!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  5. Replies: 7
    Last Post: 03-18-2003, 03:32 PM