Namespaces
Variants
Views
Actions

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

From cppreference.com
m (Capitalized 1st letter, .)
 
(2 intermediate revisions by 2 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}}
 +
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{#switch:{{{1|}}}|array=
+
{{#switch:{{#var:cont}}
{{dcl rev multi|num=1
+
|array=
|dcl1=
+
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}|
 
reference front();
 
reference front();
|since2=c++17|dcl2=
 
constexpr reference front();
 
 
}}
 
}}
{{dcl rev multi|num=2
+
{{dcl|num=2|since=c++11|notes={{mark constexpr since c++14}}|
|dcl1=
+
 
const_reference front() const;
 
const_reference front() const;
|since2=c++14|dcl2=
 
constexpr const_reference front() const;
 
 
}}
 
}}
 
|vector=
 
|vector=
{{dcl rev multi|num=1
+
{{dcl|num=1|notes={{mark constexpr since c++20}}|
|dcl1=
+
 
reference front();
 
reference front();
|since2=c++20|dcl2=
 
constexpr reference front();
 
 
}}
 
}}
{{dcl rev multi|num=2
+
{{dcl|num=2|notes={{mark constexpr since c++20}}|
|dcl1=
+
 
const_reference front() const;
 
const_reference front() const;
|since2=c++20|dcl2=
+
}}
 +
|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|{{{1|}}}}}|
+
{{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|
 
reference front();
 
reference front();
 
}}
 
}}
{{dcl|num=2|since={{cpp/std|{{{1|}}}}}|
+
{{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::{{{1}}}<char{{#ifeq:{{{1}}}|array|, 6}}>}}:
+
|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{{#ifeq:{{{1}}}|array|, 6}}> letters{'a', 'b', 'c', 'd', 'e', '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 'a'.
 
 
}}
 
}}
  
{{#ifeq: {{{1|}}}|forward_list||
 
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/container/dsc back|{{{1|}}}}}
+
{{#switch:{{#var:cont}}
{{dsc end}}
+
|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]