Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/list/back"

From cppreference.com
< cpp‎ | container‎ | list
m (Add link to edit the included template)
Line 3: Line 3:
 
[[de:cpp/container/list/back]]
 
[[de:cpp/container/list/back]]
 
[[es:cpp/container/list/back]]
 
[[es:cpp/container/list/back]]
 +
[[fr:cpp/container/list/back]]
 +
[[it:cpp/container/list/back]]
 
[[ja:cpp/container/list/back]]
 
[[ja:cpp/container/list/back]]
 
[[pt:cpp/container/list/back]]
 
[[pt:cpp/container/list/back]]
 
[[ru:cpp/container/list/back]]
 
[[ru:cpp/container/list/back]]
 +
[[zh:cpp/container/list/back]]

Revision as of 19:43, 2 November 2012

 
 
 
 
reference back();
(1)
const_reference back() const;
(2)

Returns a reference to the last element in the container.

Calling back on an empty container causes undefined behavior.

Contents

Parameters

(none)

Return value

Reference to the last element.

Complexity

Constant.

Notes

For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()).

Example

#include <cassert>
#include <list>
 
int main()
{
    std::list<char> letters{'a', 'b', 'c', 'd'};
    assert(letters.back() == 'd');
}

See also

access the first element
(public member function) [edit]
returns a reverse iterator to the beginning
(public member function) [edit]
(C++11)
returns an iterator to the end
(public member function) [edit]