Thread: reverse member function

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    reverse member function

    Gaddis (p. 451) has a reverse function for vectors that MS doesn't seem to recognize.

    Just experimenting around with his various vector member functions, I have the vector NumVect1 with 5 integer members (and I have the #include <vector> header), but when I put in the line of code:

    NumVect1.reverse();

    Visual Studio returns an error. Is it in a different library? Or does MS Visual Studio Express just not have it? Or am I misunderstanding Gaddis and using incorrect syntax? What it's supposed to do is just reverse the order of the elements of the vector.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    reverse is in the <algorithm> header.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aisthesis
    Gaddis (p. 451) has a reverse function for vectors that MS doesn't seem to recognize.

    Just experimenting around with his various vector member functions, I have the vector NumVect1 with 5 integer members (and I have the #include <vector> header), but when I put in the line of code:
    The reverse function that tabstop mentioned is actually a non-member function template. The author that you mentioned probably did not demonstrate the use of a reverse member function for std::vector since there is no such member function.

    If the author did demonstrate the use of such a member function, then it is time to put that book aside and get one that actually teaches you standard C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    242
    He just lists it in his "summary of vector member functions" on pp. 450-451 (6th ed.) but doesn't actually demonstrate it except to give as example:

    vect.reverse();

    Will that work if I try the <algorithm> header?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Aisthesis View Post
    He just lists it in his "summary of vector member functions" on pp. 450-451 (6th ed.) but doesn't actually demonstrate it except to give as example:

    vect.reverse();

    Will that work if I try the <algorithm> header?
    Should do, yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    242
    Hmmm... I just tried it with <algorithm> and it didn't work either. I think I'll write an ultra-simple program, and if that still doesn't work, post it to see what's wrong (or whether I should just write this command off for the moment).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aisthesis
    He just lists it in his "summary of vector member functions" on pp. 450-451 (6th ed.) but doesn't actually demonstrate it except to give as example:

    vect.reverse();
    Just a moment. It occurred to me that perhaps you misread reserve as reverse. The former is the name of a member function of std::vector.

    Quote Originally Posted by Aisthesis
    Will that work if I try the <algorithm> header?
    No, unless your standard library implementation provided std::vector with a reverse member function as a library extension. The correct code would be:
    Code:
    std::reverse(vect.begin(), vect.end());
    EDIT:
    On second thought, the example would not be a correct call of reserve anyway, so it does seem like the author has given misinformation.
    Last edited by laserlight; 05-18-2009 at 08:54 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    242
    yes, I noticed that MS offers reserve as one of the choices once I type the vector name and a period (but not reverse). But surely reserve doesn't do what Gaddis claims.

    Anyhow, this code won't compile (but will if you exclude the line with the reverse pseudo-function):

    Code:
    // testing "reverse" syntax
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	int Place;
    	vector<int> NumVect;
    
    	for (Place = 0; Place < 10; Place++)
    		NumVect.push_back(Place + 1);
    
    	cout << "First position in our vector is now " << NumVect[0] << endl
    		<< "Tenth position in our vector is now " << NumVect[9] << endl;
    
    	NumVect.reverse();
    
    	return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aisthesis
    Anyhow, this code won't compile (but will if you exclude the line with the reverse pseudo-function):
    Try:
    Code:
    // testing "reverse" syntax
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	int Place;
    	vector<int> NumVect;
    
    	for (Place = 0; Place < 10; Place++)
    		NumVect.push_back(Place + 1);
    
    	cout << "First position in our vector is now " << NumVect[0] << endl
    		<< "Tenth position in our vector is now " << NumVect[9] << endl;
    
    	reverse(NumVect.begin(), NumVect.end());
    
    	cout << "First position in our vector is now " << NumVect[0] << endl
    		<< "Tenth position in our vector is now " << NumVect[9] << endl;
    
    	return 0;
    }
    Now, what is the full title of this book? Gaddis has at least two books with titles that start with "Starting Out with C++". You are using the 6th edition of this book? I noticed that in another thread you mentioned 5th edition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    242
    It's Starting out with C++ From Control Structures through Objects, 6th ed. (if I said 5th elsewhere, that was incorrect).

    The reverse() thing is the first Member Function mentioned on p. 451.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    242
    also, if we exclude the line with the reverse, we don't even need #include <algorithm>

    I just threw that in in case reverse was in a different library (but it presumably has different syntax within <algorithm> anyway).

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    242
    btw, he also repeats the claim that reverse() is a member function in <vector> on p. 1014, bottom of page.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, that book is not in my local library, so getting a copy of it to review it at no cost to myself will not be easy. The error may be a one off thing (but repeated twice?), but it could also be symptomatic of other factual problems.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    242
    Well, it's not good, and I appreciate your clearing up that the problem is with Gaddis rather than with my compiler.

    As to value of book, it's so widely used that it can't be too bad. That's also the only substantive mistake I've seen (and I'm the kind of person who tests almost everything), aside from some obvious typos where it was clear anyway what was meant.

    The order of topics is not necessarily the best in the book (I still haven't gotten to strings yet, for example--he handles them as character arrays for the first half of the book), but he seems to get you there eventually. I don't think I'm ready to scrap the book completely just yet--since there's always some loss of time just by opening another book which approaches things in a different sequence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM