Namespaces
Variants
Views
Actions

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

From cppreference.com
(added note about UB for empty containers)
 
(21 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | front}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|front}}
{{ddcl list begin}}
+
{{cpp/container/{{#var:cont}}/navbar}}
{{ddcl list item | notes={{cpp/container/mark since c++11 | {{{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 since c++11 | {{{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 a reference to the first element in the container.  
+
Returns a reference to the first element in the container.
  
For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}.
+
Calling {{tt|front}} on an empty container causes undefined behavior.
  
 
===Parameters===
 
===Parameters===
Line 18: Line 43:
  
 
===Return value===
 
===Return value===
reference to the first element
+
Reference to the first element.
  
 
===Complexity===
 
===Complexity===
Constant
+
Constant.
  
 
===Notes===
 
===Notes===
Calling {{tt|front}} on an empty container is undefined.
+
For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| The following code uses {{tt|front}} to display the first element of a {{c|std::{{{1}}}<char>}}:
+
|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 <{{{1}}}>
+
#include <cassert>
#include <iostream>
+
#include <{{#var:cont}}>
 
   
 
   
 
int main()
 
int main()
 
{
 
{
     std::{{{1}}}<char> letters {'o', 'm', 'g', 'w', 't', 'f'};
+
     std::{{#var:cont}}<char{{#switch:{{#var:cont}}|inplace_vector|array=, 4}}> letters{'a', 'b', 'c', 'd'};
+
     assert(letters.front() == 'a');
     if (!letters.empty()) {
+
        std::cout << "The first character is: " << letters.front() << '\n';
+
    } 
+
 
}
 
}
| output=
 
The first character is o
 
 
}}
 
}}
  
{{#ifeq: {{{1|}}} | forward_list ||
 
 
===See also===
 
===See also===
 
+
{{dsc begin}}
{{dcl list begin}}
+
{{#switch:{{#var:cont}}
{{dcl list template | cpp/container/dcl list back |{{{1|}}}}}
+
|forward_list=
{{dcl list end}}
+
{{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]