How to get last element of list?

This is a discussion on How to get last element of list? within the C++ Programming forums, part of the General Programming Boards category; Hello all, so I am trying to check for the last element in the list, but I don't know how. ...

  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
    595
    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
    595
    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,307
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you are trying to compare iterators of different types
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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)
        {
            // ...
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21