Difference between revisions of "Template:cpp/container/front"
From cppreference.com
m (Text replace - "{{param none}}" to "(none)") |
m (+inplace_vector() |
||
(25 intermediate revisions by 13 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}} |
+ | {{#switch:{{#var:cont}} | ||
+ | |array= | ||
+ | {{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}| | ||
reference front(); | reference front(); | ||
}} | }} | ||
− | {{ | + | {{dcl|num=2|since=c++11|notes={{mark constexpr since c++14}}| |
const_reference front() const; | const_reference front() const; | ||
}} | }} | ||
− | {{ | + | |vector= |
+ | {{dcl|num=1|notes={{mark constexpr since c++20}}| | ||
+ | reference front(); | ||
+ | }} | ||
+ | {{dcl|num=2|notes={{mark constexpr since c++20}}| | ||
+ | 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; | ||
+ | }} | ||
+ | | | ||
+ | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}| | ||
+ | reference front(); | ||
+ | }} | ||
+ | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}| | ||
+ | const_reference front() const; | ||
+ | }} | ||
+ | }} | ||
+ | {{dcl end}} | ||
− | Returns reference to the first element in the container. | + | Returns a reference to the first element in the container. |
+ | |||
+ | Calling {{tt|front}} on an empty container causes undefined behavior. | ||
===Parameters=== | ===Parameters=== | ||
Line 16: | Line 43: | ||
===Return value=== | ===Return value=== | ||
− | + | Reference to the first element. | |
− | + | ||
===Complexity=== | ===Complexity=== | ||
− | Constant | + | Constant. |
− | {{ | + | ===Notes=== |
− | + | For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}. | |
− | {{ | + | ===Example=== |
− | {{ | + | {{example |
− | {{ | + | |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= | ||
+ | #include <cassert> | ||
+ | #include <{{#var:cont}}> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::{{#var:cont}}<char{{#switch:{{#var:cont}}|inplace_vector|array=, 4}}> letters{'a', 'b', 'c', 'd'}; | ||
+ | assert(letters.front() == 'a'); | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |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> )
|