Difference between revisions of "Template:cpp/container/front"
From cppreference.com
Andreas Krug (Talk | contribs) m (Capitalized 1st letter, .) |
m (+inplace_vector() |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|inplace_vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|front}} |
+ | {{cpp/container/{{#var:cont}}/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
− | {{dcl | + | |array= |
− | | | + | {{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}| |
reference front(); | reference front(); | ||
− | |||
− | |||
}} | }} | ||
− | {{dcl | + | {{dcl|num=2|since=c++11|notes={{mark constexpr since c++14}}| |
− | | | + | |
const_reference front() const; | const_reference front() const; | ||
− | |||
− | |||
}} | }} | ||
|vector= | |vector= | ||
− | {{dcl | + | {{dcl|num=1|notes={{mark constexpr since c++20}}| |
− | | | + | |
reference front(); | reference front(); | ||
− | |||
− | |||
}} | }} | ||
− | {{dcl | + | {{dcl|num=2|notes={{mark constexpr since c++20}}| |
− | | | + | |
const_reference front() const; | const_reference front() const; | ||
− | | | + | }} |
+ | |inplace_vector= | ||
+ | {{dcl|num=1|since=c++26| | ||
+ | constexpr reference front(); | ||
+ | }} | ||
+ | {{dcl|num=2|since=c++26| | ||
constexpr const_reference front() const; | constexpr const_reference front() const; | ||
}} | }} | ||
| | | | ||
− | {{dcl|num=1|since={{cpp/std|{{ | + | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}| |
reference front(); | reference front(); | ||
}} | }} | ||
− | {{dcl|num=2|since={{cpp/std|{{ | + | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}| |
const_reference front() const; | const_reference front() const; | ||
}} | }} | ||
Line 56: | Line 53: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | |The following code uses {{tt|front}} to display the first element of a {{c|std::{{ | + | |The following code uses {{tt|front}} to display the first element of a {{c|std::{{#var:cont}}<char{{#switch:{{#var:cont}}|inplace_vector|array=, 4}}>}}: |
|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.front() == 'a'); | |
− | + | ||
− | + | ||
} | } | ||
− | |||
− | |||
}} | }} | ||
− | |||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc | + | {{#switch:{{#var:cont}} |
− | {{dsc | + | |forward_list= |
+ | {{dsc inc|cpp/container/dsc before_begin|forward_list}} | ||
+ | | | ||
+ | {{dsc inc|cpp/container/dsc back|{{#var:cont}}}} | ||
+ | {{dsc inc|cpp/container/dsc rend|{{#var:cont}}}} | ||
}} | }} | ||
+ | {{dsc inc|cpp/container/dsc begin|{{#var:cont}}}} | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |array|vector|inplace_vector= | ||
+ | {{dsc inc|cpp/container/dsc data|{{#var:cont}}}} | ||
+ | }} | ||
+ | {{dsc end}} |
Latest revision as of 02:21, 16 August 2024
constexpr reference front(); |
(1) | (since C++26) |
constexpr const_reference front() const; |
(2) | (since C++26) |
Returns a reference to the first element in the container.
Calling front
on an empty container causes undefined behavior.
Contents |
[edit] Parameters
(none)
[edit] Return value
Reference to the first element.
[edit] Complexity
Constant.
[edit] Notes
For a container c
, the expression c.front() is equivalent to *c.begin().
[edit] Example
The following code uses front
to display the first element of a std::inplace_vector<char, 4>:
Run this code
#include <cassert> #include <inplace_vector> int main() { std::inplace_vector<char, 4> letters{'a', 'b', 'c', 'd'}; assert(letters.front() == 'a'); }
[edit] See also
access the last element (public member function of std::inplace_vector<T,N> )
| |
returns a reverse iterator to the end (public member function of std::inplace_vector<T,N> )
| |
returns an iterator to the beginning (public member function of std::inplace_vector<T,N> )
| |
direct access to the underlying contiguous storage (public member function of std::inplace_vector<T,N> )
|