The C++ container class
The C++ standard library provides rich functionality, among which is a very important container class, used to store collections of elements, supporting bidirectional iterators.
is a sequence container in the C++ Standard Template Library (STL) that allows fast insertion and deletion of elements at any position in the container. Unlike arrays or vectors (), ), it does not require specifying a size at creation time, and can add or remove elements at any position without reallocating memory.
Syntax
The following are Some basic operations of the container:
- Include the header file:
#include - Declare a list:
std::list, wheremylist; Tis the type of element stored in the list. - Insert elements:
mylist.push_back(value); - Delete elements:
mylist.pop_back();ormylist.erase(iterator); - Access elements:
mylist.front();andmylist.back(); - Traverse the list: use an iterator
for (auto it = mylist.begin(); it != mylist.end(); ++it)
Feature
- Bidirectional iteration:
provides bidirectional iterators, allowing forward and backward traversal of elements. - Dynamic size: Unlike arrays,
's size can change dynamically, without pre-allocating a fixed-size memory block. - Fast insertion and deletion: Elements can be quickly inserted or deleted at any position in the list, without moving large numbers of elements as with a vector.
Declaration and initialization
's declaration and initialization are similar to other containers:
#include
#include
int main() {
std::list lst1; // 空的list
std::list lst2(5); // 包含5个默认初始化元素的list
std::list lst3(5, 10); // 包含5个元素,每个元素为10
std::list lst4 = {1, 2, 3, 4}; // 使用初始化列表
return 0;
}
Example
Below is an example using is a simple example, including creating a list, adding elements, traversing the list, and outputting results.
Example
#include
#include
int
main
(
)
{
// Create a list of integer type
std
::
list
<
int
numbers;
// Add elements to the list
numbers.
push_back
(
10
)
;
numbers.
push_back
(
20
)
;
numbers.
push_back
(
30
)
;
// Access and print the first element of the list
std
::
cout
<<
"First element: "
<<
numbers.
front
(
)
<<
std
::
endl
;
// Access and print the last element of the list
std
::
cout
<<
"Last element: "
<<
numbers.
back
(
)
<<
std
::
endl
;
// Traverse the list and print all elements
std
::
cout
<<
"List elements: "
;
for
(
std
::
list
<
int
::
iterator
it
=
numbers.
begin
(
)
;
it
!
=
numbers.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Delete the last element in the list
numbers.
pop_back
(
)
;
// Traverse the list again and print all elements
std
::
cout
<<
"List elements after removing the last element: "
;
for
(
std
::
list
<
int
::
iterator
it
=
numbers.
begin
(
)
;
it
!
=
numbers.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Output result:
First element: 10
Last element: 30
List elements: 10 20 30
List elements after removing the last element: 10 20
Common member functions
The following are Some commonly used member functions in:
| function | Description |
|---|---|
push_back(const T& val) |
Add an element at the end of the linked list |
push_front(const T& val) |
adds an element at the head of the linked list |
pop_back() |
Remove the element at the end of the linked list |
pop_front() |
Remove the element at the head of the linked list |
insert(iterator pos, val) |
inserts an element at a specified position |
erase(iterator pos) |
Remove the element at a specified position |
clear() |
clears all elements |
size() |
Return the number of elements in the linked list |
empty() |
checks whether the linked list is empty |
front() |
Return the first element of the linked list |
back() |
Return the last element of the linked list |
remove(const T& val) |
removes all elements equal to a specified value |
sort() |
Sort the elements in the linked list |
merge(list& other) |
Merge another sorted linked list |
reverse() |
reverses the linked list |
begin()/ end() |
Return the begin/end iterators of the linked list |
Example
1. Basic operations
Example
#include
#include
int
main
(
)
{
std
::
list
<
int
lst
=
{
10
,
20
,
30
};
// Insert and delete elements
lst.
push_front
(
5
)
;
// Insert 5 at the head
lst.
push_back
(
40
)
;
// Insert 40 at the tail
lst.
pop_front
(
)
;
// Remove the head element
lst.
pop_back
(
)
;
// Delete the tail element
// Output the linked list contents
std
::
cout
<<
"List elements: "
;
for
(
const
auto
&
elem
:
lst
)
{
std
::
cout
<<
elem
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
2. Insert and delete elements at specific positions
Example
#include
#include
int
main
(
)
{
std
::
list
<
int
lst
=
{
1
,
2
,
3
,
4
,
5
};
auto
it
=
lst.
begin
(
)
;
std
::
advance
(
it,
2
)
;
// Move the iterator to the 3rd element (value 3)
lst.
insert
(
it,
10
)
;
// Insert 10 before the 3rd element
lst.
erase
(
it
)
;
// Delete the 3rd element
// Output the linked list contents
std
::
cout
<<
"List elements: "
;
for
(
const
auto
&
elem
:
lst
)
{
std
::
cout
<<
elem
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
3. Sorting and deduplication
Example
#include
#include
int
main
(
)
{
std
::
list
<
int
lst
=
{
3
,
1
,
4
,
1
,
5
,
9
,
2
,
6
,
5
,
3
,
5
};
lst.
sort
(
)
;
// Sort
lst.
unique
(
)
;
// Remove adjacent duplicate elements
// Output the linked list contents
std
::
cout
<<
"Sorted and unique list: "
;
for
(
const
auto
&
elem
:
lst
)
{
std
::
cout
<<
elem
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
4. Merge and reverse
Example
#include
#include
int
main
(
)
{
std
::
list
<
int
lst1
=
{
1
,
3
,
5
,
7
};
std
::
list
<
int
lst2
=
{
2
,
4
,
6
,
8
};
lst1.
merge
(
lst2
)
;
// Merge two sorted linked lists
lst1.
reverse
(
)
;
// Reverse the linked list
// Output the linked list contents
std
::
cout
<<
"Merged and reversed list: "
;
for
(
const
auto
&
elem
:
lst1
)
{
std
::
cout
<<
elem
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Comparison with other containers
| Feature | std::list |
std::vector |
std::deque |
|---|---|---|---|
| Memory structure | Non-contiguous memory, doubly linked list | Contiguous memory | Segmented contiguous memory |
| Access performance | Sequential access is fast; random access is slow | Random access is fast | Access at both ends is fast |
| Insertion/deletion performance | Insertion and deletion at any position are fast | Insertion at the end is fast; in the middle is slow | Insertion and deletion at both ends are fast |
| Applicable scenarios | Frequent insertion/deletion in the middle | Requires efficient random access | Requires fast insertion/deletion at both ends |
| Iterator stability | Stable; element insertion or deletion does not invalidate iterators | Insertion and deletion may invalidate iterators | Insertion and deletion may invalidate iterators |
Notes
's elements are stored in insertion order, not sorted by element value.- Because
's elements are stored at different memory locations, it is not suitable for scenarios requiring random access. - Compared with vectors,
has lower memory efficiency, because each element needs extra space to store pointers to the previous and next elements.
Through this simple introduction and examples, beginners should be able to gain a basic understanding of C++'s container and begin using it to solve real problems.