Thread: How to get last element of list?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    How to get last element of list?

    Hello all, so I am trying to check for the last element in the list, but I don't know how. list.begin() is the first element, but list.end() is the one past the last element. Doing list.end()-- seems to give me an error. My thought of doing this is using a loop that will keep iterating until it hits the list.end(). When it does that I --iter to get the last element and initialize it as something else. However is there a shorter way of doing this?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    What do you mean by "check for the last element"?

    list.back() returns a reference to the last element. Is that what you want?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    It looks exactly like what I want, but for some reason I am getting an error with this piece of code.

    Code:
    for(list<string>::iterator iter = phrase.begin(); iter != phrase.back(); iter++){
                             ....code...
    	}
    Code:
    error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
    Thanks for your help!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Because phrase.back() is not an iterator, it's a const reference. What are you trying to do? Why are you iterating through the list? phrase.back() gives you direct access to the last item so you can do, for example
    Code:
    cout << phrase.back() << endl;
    Or are you trying to alter it?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    rbegin() is the last element, assuming the list is not empty, otherwise it will equal rend().
    Are you wanting to loop over all items except the last one?
    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"

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Yes I am trying to loop through all the items except the last one.

    If rbegin() is what I am looking for then this should work..but its not.

    Code:
    for(list<string>::iterator iter = phrase.begin(); iter != phrase.rbegin(); iter++){
                       ...code
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are trying to compare iterators of different types
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dnguyen1022
    Yes I am trying to loop through all the items except the last one.
    I suggest something along these lines:
    Code:
    if (!phrase.empty())
    {
        list<string>::iterator end(phrase.end());
        --end;
        for (list<string>::iterator iter = phrase.begin(); iter != end; ++iter)
        {
            // ...
        }
    }
    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

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Yes, this would work perfectly, but why would my last code not work? Does rbegin() not reference to the last element of the list?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    Yes, this would work perfectly, but why would my last code not work? Does rbegin() not reference to the last element of the list?
    Because rbegin returns a reverse iterator, which is a different type than a (normal/forward) iterator.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  4. 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
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM