C++ front() function
Among the various operations on vector,front is a very simple function, used toget the first element.
front is a member function of the container class, used toreturns the first element of the container. It is equivalent to at(0) or operator[](0), but with clearer semantics.
front provides an intuitive way to access the container's first element.
Word meaning: front means 'front', i.e., to get the first (frontmost) element.
Basic syntax and parameters
front is a member function of the container class; calling it is straightforward and requires no arguments.
Syntax format
reference front();
const_reference front() const;
Parameter Description
- Parameters: No parameters
fronttakes no arguments.
Function description
- Return Value: Returns thereferenceto the element at the specified position. If the container is const, a const reference is returned.
- effect: Returns the first element of the container (the element at index 0).
- Note: Before calling
front, ensure the container is not empty; otherwise the behavior is undefined. It is recommended to first useempty()orsize() > 0to check.
Example
Let us thoroughly master front through a series of examples from simple to complex.
Example 1: Basic usage - getting the first element
Example
#include
#include
int
main
(
)
{
// 1. Create a vector and add some elements
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"vector size is: "
<<
numbers.
size
(
)
<<
std
::
endl
;
// 2. Use front to get the first element
std
::
cout
<<
"First element (front): "
<<
numbers.
front
(
)
<<
std
::
endl
;
std
::
cout
<<
"Using at(0): "
<<
numbers.
at
(
0
)
<<
std
::
endl
;
std
::
cout
<<
"Using [0]: "
<<
numbers
[
0
]
<<
std
::
endl
;
return
0
;
}
Expected output:
vector的大小是: 5
第一个元素 (front): 10
使用 at(0): 10
使用 [0]: 10
Code analysis:
numbers.front()returns the first element10.- It returns the same value as
numbers.at(0)andnumbers[0], but with clearer semantics.
Example 2: Modifying the first element's value
front returns a reference, so it can be used to modify the element's value.
Example
#include
#include
#include
int
main
(
)
{
std
::
vector
<
std
::
string
names
=
{
"Alice"
,
"Bob"
,
"Charlie"
}
;
std
::
cout
<<
"Before modification, first element: "
<<
names.
front
(
)
<<
std
::
endl
;
// Use front() to get a reference and modify the element
names.
front
(
)
=
"Andrew"
;
std
::
cout
<<
"After modification, first element: "
<<
names.
front
(
)
<<
std
::
endl
;
// Now the content of names is: Andrew, Bob, Charlie
return
0
;
}
Expected output:
修改前,第一个元素: Alice
修改后,第一个元素: Andrew
Code analysis:
names.front() = "Andrew";modified the value of the first element through the reference.front()returns a modifiable lvalue reference.
Example 3: Safe use of front - check whether the container is empty
When using front Before using, you should first check whether the container is empty.
Example
#include
#include
void
printFront
(
const
std
::
vector
<
int
&
v
)
{
if
(!
v.
empty
(
)
)
{
std
::
cout
<<
"The first element is: "
<<
v.
front
(
)
<<
std
::
endl
;
}
else
{
std
::
cout
<<
"Container is empty; cannot get the first element"
<<
std
::
endl
;
}
}
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
};
// Normal case
printFront
(
numbers
)
;
// Empty container case
std
::
vector
<
int
emptyVec;
printFront
(
emptyVec
)
;
return
0
;
}
Expected output:
第一个元素是: 10
容器为空,无法获取第一个元素
Code analysis:
!v.empty()Check whether the container is not empty.- When calling
frontChecking before use can avoid undefined behavior.
Example 4: Use front to implement the queue's head
front can be used to implement a queue-like data structure.
Example
#include
#include
#include
class
SimpleQueue
{
private
:
std
::
vector
<
std
::
string
data
;
public
:
// Enqueue
void
enqueue
(
const
std
::
string
&
item
)
{
data.
push_back
(
item
)
;
}
// Dequeue (remove the head)
void
dequeue
(
)
{
if
(
!
data.
empty
(
)
)
{
// Remove the first element; subsequent elements shift forward
data.
erase
(
data.
begin
(
)
)
;
}
}
// Get the head element
std
::
string
front
(
)
const
{
if
(
!
data.
empty
(
)
)
{
return
data.
front
(
)
;
}
return
""
;
}
// Check if the queue is empty
bool
empty
(
)
const
{
return
data.
empty
(
)
;
}
// Get queue size
size_t
size
(
)
const
{
return
data.
size
(
)
;
}
}
;
int
main
(
)
{
SimpleQueue q
;
q.
enqueue
(
"UserA"
)
;
q.
enqueue
(
"UserB"
)
;
q.
enqueue
(
"UserC"
)
;
std
::
cout
<<
"Queue size: "
<<
q.
size
(
)
<<
std
::
endl
;
while
(
!
q.
empty
(
)
)
{
std
::
cout
<<
"Processing: "
<<
q.
front
(
)
<<
std
::
endl
;
q.
dequeue
(
)
;
}
std
::
cout
<<
"Queue cleared"
<<
std
::
endl
;
return
0
;
}
Expected output:
队列大小: 3
处理: 用户A
处理: 用户B
处理: 用户C
队列已清空
Code analysis:
front()is used to get the head element (the element waiting to be processed).- This example shows how to use
frontto implement a first-in-first-out (FIFO) queue.
The C++ container class