Difference between revisions of "Template:cpp/container/back"
From cppreference.com
(fix versioning) |
(fix versioning) |
||
Line 6: | Line 6: | ||
}} | }} | ||
{{dcl rev begin}} | {{dcl rev begin}} | ||
− | {{dcl | until= | + | {{dcl | until=c++14 | |
const_reference back() const; | const_reference back() const; | ||
}} | }} | ||
− | {{dcl | since= | + | {{dcl | since=c++14 | |
constexpr const_reference back() const; | constexpr const_reference back() const; | ||
}} | }} |
Revision as of 03:52, 1 July 2013
reference back(); |
(since {std}) | |
const_reference back() const; |
(until C++14) | |
constexpr const_reference back() const; |
(since C++14) | |
Returns reference to the last element in the container.
Calling back
on an empty container is undefined.
Contents |
Parameters
(none)
Return value
Reference to the last element
Complexity
Constant
Notes
For a container c
, the expression return c.back(); is equivalent to { auto tmp = c.end(); --tmp; return *tmp; }
Const version is marked as constexpr since c++14
Example
The following code uses back
to display the last element of a std::{{{1}}}<char>:
Run this code
#include <{{{1}}}> #include <iostream> int main() { std::{{{1}}}<char> letters {'o', 'm', 'g', 'w', 't', 'f'}; if (!letters.empty()) { std::cout << "The last character is: " << letters.back() << '\n'; } }
Output:
The last character is f
See also
access the first element (public member function of std::{{{1}}} )
|