Difference between revisions of "Template:cpp/container/back"
From cppreference.com
m (+inplace_vector() |
|||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|back}} |
+ | {{cpp/container/{{#var:cont}}/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
+ | |array= | ||
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}| | {{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}| | ||
reference back(); | reference back(); | ||
Line 15: | Line 17: | ||
{{dcl|num=2|notes={{mark constexpr since c++20}}| | {{dcl|num=2|notes={{mark constexpr since c++20}}| | ||
const_reference back() const; | const_reference back() const; | ||
+ | }} | ||
+ | |inplace_vector= | ||
+ | {{dcl|num=1|since=c++26| | ||
+ | constexpr reference back(); | ||
+ | }} | ||
+ | {{dcl|num=2|since=c++26| | ||
+ | constexpr const_reference back() const; | ||
}} | }} | ||
| | | | ||
− | {{dcl|num=1|since={{cpp/std|{{ | + | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}| |
reference back(); | reference back(); | ||
}} | }} | ||
− | {{dcl|num=2|since={{cpp/std|{{ | + | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}| |
const_reference back() const; | const_reference back() const; | ||
}} | }} | ||
Line 44: | Line 53: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | |||
|code= | |code= | ||
− | #include < | + | #include <cassert> |
− | #include < | + | #include <{{#var:cont}}> |
− | + | ||
int main() | int main() | ||
{ | { | ||
− | std::{{ | + | std::{{#var:cont}}<char{{#switch:{{#var:cont}}|inplace_vector|array=, 4}}> letters{'a', 'b', 'c', 'd'}; |
− | + | assert(letters.back() == 'd'); | |
− | + | ||
− | + | ||
} | } | ||
− | |||
− | |||
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc front|{{{ | + | {{dsc inc|cpp/container/dsc front|{{#var:cont}}}} |
+ | {{dsc inc|cpp/container/dsc rbegin|{{#var:cont}}}} | ||
+ | {{dsc inc|cpp/container/dsc end|{{#var:cont}}}} | ||
{{dsc end}} | {{dsc end}} |
Latest revision as of 15:30, 15 August 2024
reference back(); |
(1) | (constexpr since C++20) |
const_reference back() const; |
(2) | (constexpr since C++20) |
Returns a reference to the last element in the container.
Calling back
on an empty container causes undefined behavior.
Contents |
[edit] Parameters
(none)
[edit] Return value
Reference to the last element.
[edit] Complexity
Constant.
[edit] Notes
For a non-empty container c
, the expression c.back() is equivalent to *std::prev(c.end()).
[edit] Example
Run this code
#include <cassert> #include <vector> int main() { std::vector<char> letters{'a', 'b', 'c', 'd'}; assert(letters.back() == 'd'); }
[edit] See also
access the first element (public member function of std::vector<T,Allocator> )
| |
(C++11) |
returns a reverse iterator to the beginning (public member function of std::vector<T,Allocator> )
|
(C++11) |
returns an iterator to the end (public member function of std::vector<T,Allocator> )
|