Namespaces
Variants
Views
Actions

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

From cppreference.com
(mention UB first)
m
Line 6: Line 6:
 
}}
 
}}
 
{{ddcl list item | notes={{cpp/container/mark since c++11 | {{{1|}}} }} |
 
{{ddcl list item | notes={{cpp/container/mark since c++11 | {{{1|}}} }} |
const_reference front() const;
+
constexpr const_reference front() const;
 
}}
 
}}
 
{{ddcl list end}}
 
{{ddcl list end}}
Line 25: Line 25:
 
===Notes===
 
===Notes===
 
For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}.
 
For a container {{tt|c}}, the expression {{c|c.front()}} is equivalent to {{c|*c.begin()}}.
 +
 +
Const version is marked as constexpr since c++14.
  
 
===Example===
 
===Example===

Revision as of 10:02, 21 May 2013

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
reference front();
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
constexpr const_reference front() const;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Returns a reference to the first element in the container.

Calling front on an empty container is undefined.

Contents

Parameters

(none)

Return value

reference to the first element

Complexity

Constant

Notes

For a container c, the expression c.front() is equivalent to *c.begin().

Const version is marked as constexpr since c++14.

Example

The following code uses front to display the first element of a std::{{{1}}}<char>:

#include <{{{1}}}>
#include <iostream>
 
int main()
{
    std::{{{1}}}<char> letters {'o', 'm', 'g', 'w', 't', 'f'};
 
    if (!letters.empty()) {
        std::cout << "The first character is: " << letters.front() << '\n';
    }  
}

Output:

The first character is o

See also

Template:cpp/container/dcl list back