Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/back"

From cppreference.com
(constexpr is for arrays only)
(p0031r0 (constexpr array))
Line 1: Line 1:
 
{{cpp/container/{{{1|}}}/title | back}}
 
{{cpp/container/{{{1|}}}/title | back}}
 
{{cpp/container/{{{1|}}}/navbar}}
 
{{cpp/container/{{{1|}}}/navbar}}
 +
{{#switch:{{{1|}}}|array=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | since={{cpp/std|{{{1|}}}}} |
+
{{dcl rev begin}}
reference back();
+
{{dcl | until=c++17|
 +
reference back( size_type pos );
 
}}
 
}}
{{#ifeq:{{{1}}}|array|
+
{{dcl | since=c++17|
 +
constexpr reference back( size_type pos );
 +
}}
 +
{{dcl rev end}}
 
{{dcl rev begin}}
 
{{dcl rev begin}}
{{dcl | since={{cpp/std|{{{1|}}}}} | until=c++14 |
+
{{dcl | until=c++14 |
const_reference back() const;
+
const_reference back( size_type pos ) const;
 
}}
 
}}
 
{{dcl | since=c++14 |
 
{{dcl | since=c++14 |
constexpr const_reference back() const;
+
constexpr const_reference back( size_type pos ) const;
 
}}
 
}}
 
{{dcl rev end}}
 
{{dcl rev end}}
|{{dcl | since={{cpp/std|{{{1|}}}}}|
+
{{dcl end}}
 +
|{{dcl begin}}
 +
{{dcl | since={{cpp/std|{{{1|}}}}} |
 +
reference back();
 +
}}
 +
{{dcl | since={{cpp/std|{{{1|}}}}}|
 
const_reference back() const;
 
const_reference back() const;
 
}}
 
}}
}}
+
{{dcl end}}}}
{{dcl end}}
+
  
 
Returns reference to the last element in the container.  
 
Returns reference to the last element in the container.  

Revision as of 20:35, 7 March 2016

reference back();
(since {std})
const_reference back() const;
(since {std})

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; }

Example

The following code uses back to display the last element of a std::{{{1}}}<char>:

#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}}}) [edit]