Namespaces
Variants
Views
Actions

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

From cppreference.com
m (Text replace - "{{param none}}" to "(none)")
 
(25 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | front}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/sidebar}}
+
-->{{cpp/container/{{#var:cont}}/title|front}}
{{ddcl list begin}}
+
{{cpp/container/{{#var:cont}}/navbar}}
{{ddcl list item | notes={{cpp/container/mark c++11 feature | {{{1|}}} }} |
+
{{dcl begin}}
 +
{{#switch:{{#var:cont}}
 +
|array=
 +
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}|
 
reference front();
 
reference front();
 
}}
 
}}
{{ddcl list item | notes={{cpp/container/mark c++11 feature | {{{1|}}} }} |
+
{{dcl|num=2|since=c++11|notes={{mark constexpr since c++14}}|
 
const_reference front() const;
 
const_reference front() const;
 
}}
 
}}
{{ddcl list end}}
+
|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.
reference to the first element
+
  
 
===Complexity===
 
===Complexity===
Constant
+
Constant.
  
{{#ifeq: {{{1|}}} | forward_list ||
+
===Notes===
===See also===
+
For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}.
  
{{dcl list begin}}
+
===Example===
{{dcl list template | cpp/container/dcl list back |{{{1|}}}}}
+
{{example
{{dcl list end}}
+
|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>:

#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>) [edit]
returns a reverse iterator to the end
(public member function of std::inplace_vector<T,N>) [edit]
returns an iterator to the beginning
(public member function of std::inplace_vector<T,N>) [edit]
direct access to the underlying contiguous storage
(public member function of std::inplace_vector<T,N>) [edit]