Cpp Libs List
The C++ standard library provides rich functionality, among which `` is a very important container class used to store element collections and supports 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 (``), `` does not need to specify a size when created, and elements can be added or removed at any position without needing to reallocate memory.
### Syntax
The following are some basic operations of the `` container:
* Include header file: `#include `
* Declare a list: `std::list mylist;`, where `T` is the type of elements stored in the list.
* Insert element: `mylist.push_back(value);`
* Delete element: `mylist.pop_back();` or `mylist.erase(iterator);`
* Access elements: `mylist.front();` and `mylist.back();`
* Traverse the list: Use iterator `for (auto it = mylist.begin(); it != mylist.end(); ++it)`
### Features
* **Bidirectional Iteration**: `` provides bidirectional iterators that can traverse elements forward and backward.
* **Dynamic Size**: Unlike arrays, the size of `` can change dynamically without needing to pre-allocate fixed-size memory.
* **Fast Insertion and Deletion**: Elements can be quickly inserted or deleted at any position in the list without needing to move a large number of elements as vectors do.
### Declaration and Initialization
The declaration and initialization of `` is similar to other containers:
#include #include int main() { std::list lst1; // empty list std::list lst2(5); // list with 5 default-initialized elements std::list lst3(5, 10); // list with 5 elements, each element is 10 std::list lst4 = {1, 2, 3, 4}; // using initializer list return 0;}
## Example
The following is a simple example of using ``, including creating a list, adding elements, traversing the list, and outputting the results.
## Example
#include
#include
int main(){
// Create a list of integers
std::list 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::iterator it = numbers.begin(); it != numbers.end();++it){
std::cout<<*it <<" ";
}
std::cout<< std::endl;
// Delete the last element from 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::iterator it = numbers.begin(); it != numbers.end();++it){
std::cout<<*it <<" ";
}
std::cout<< std::endl;
return 0;
}
Output:
First element: 10Last element: 30List elements: 10 20 30 List elements after removing the last element: 10 20
## Common Member Functions
Here are some commonly used member functions in ``:
| Function | Description |
| --- | --- |
| `push_back(const T& val)` | Add element at the end of the list |
| `push_front(const T& val)` | Add element at the beginning of the list |
| `pop_back()` | Delete element at the end of the list |
| `pop_front()` | Delete element at the beginning of the list |
| `insert(iterator pos, val)` | Insert element at specified position |
| `erase(iterator pos)` | Delete element at specified position |
| `clear()` | Clear all elements |
| `size()` | Return the number of elements in the list |
| `empty()` | Check if the list is empty |
| `front()` | Return the first element of the list |
| `back()` | Return the last element of the list |
| `remove(const T& val)` | Delete all elements equal to the specified value |
| `sort()` | Sort elements in the list |
| `merge(list& other)` | Merge another sorted list |
| `reverse()` | Reverse the list |
| `begin()`[/ `end()`](#) | Return the beginning/end iterator of the list |
### Example
**1. Basic Operations**
## Example
#include
#include
int main(){
std::list lst ={10, 20, 30};
// Insert and delete elements
lst.push_front(5);// Insert 5 at the beginning
lst.push_back(40);// Insert 40 at the end
lst.pop_front();// Delete the first element
lst.pop_back();// Delete the last element
// Output 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 lst ={1, 2, 3, 4, 5};
auto it = lst.begin();
std::advance(it, 2);// Move iterator to the 3rd element (value is 3)
lst.insert(it, 10);// Insert 10 before the 3rd element
lst.erase(it);// Delete the 3rd element
// Output list contents
std::cout<<"List elements: ";
for(const auto& elem : lst){
std::cout<< elem <<" ";
}
std::cout<< std::endl;
return 0;
}
**3. Sort and Remove Duplicates**
## Example
#include
#include
int main(){
std::list lst ={3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
lst.sort();// Sort
lst.unique();// Remove adjacent duplicate elements
// Output list contents
std::cout<<"Sorted and
YouTip