Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering std::list in C++: Syntax, Functions & Practical Examples

What is std::list?

In C++, std::list is a sequence container that stores elements as a doubly‑linked list. This structure allows you to insert or remove items at any position in constant time, while iterating over the list in either direction. Although it does not provide fast random access like std::vector, it excels at scenarios where frequent insertions or deletions occur.

The container automatically manages memory through an allocator, so it can grow or shrink from both ends during runtime.

Why Use std::list?

Defining a List

To use std::list, include the <list> header:

#include <list>

// Template definition
// template<class T, class Alloc = std::allocator<T>> class list;

Parameters:

Example 1: Basic Iteration

#include <iostream>
#include <list>

int main() {
    std::list<int> my_list{12, 5, 10, 9};
    for (int x : my_list) {
        std::cout << x << '\n';
    }
}

Output:

Mastering std::list in C++: Syntax, Functions & Practical Examples

Common std::list Operations

FunctionDescription
insert()Insert an element before the iterator position.
push_back()Add an element to the end.
push_front()Add an element to the front.
pop_front()Remove the first element.
size()Return the number of elements.
front()Access the first element.
back()Access the last element.
reverse()Reverse the order of elements.
merge()Merge two sorted lists into one.

Constructors

Example 2: Constructor Variants

#include <iostream>
#include <list>

int main() {
    std::list<int> l; // empty
    std::list<int> l1{10, 20, 30};
    std::list<int> l2(l1.begin(), l1.end()); // range copy
    std::list<int> l3(std::move(l1)); // move

    std::cout << "Size of l: " << l.size() << '\n';
    std::cout << "Contents of l2:\n";
    for (int x : l2) std::cout << x << '\n';
    std::cout << "Contents of l3:\n";
    for (int x : l3) std::cout << x << '\n';
}

Output:

Mastering std::list in C++: Syntax, Functions & Practical Examples

Container Properties

Inserting Elements

Use push_front, push_back, or insert to add items.

Example 3: Mixed Insertions

#include <algorithm>
#include <iostream>
#include <list>

int main() {
    std::list<int> my_list{12, 5, 10, 9};
    my_list.push_front(11); // front
    my_list.push_back(18);  // back
    auto it = std::find(my_list.begin(), my_list.end(), 10);
    if (it != my_list.end()) {
        my_list.insert(it, 21); // before 10
    }
    for (int x : my_list) std::cout << x << '\n';
}

Output:

Mastering std::list in C++: Syntax, Functions & Practical Examples

Deleting Elements

Use erase to remove single elements or ranges.

Example 4: Erase Demonstration

#include <iostream>
#include <list>

int main() {
    std::list<int> my_list{12, 5, 10, 9};
    std::cout << "Before deletion:\n";
    for (int x : my_list) std::cout << x << '\n';

    my_list.erase(my_list.begin()); // remove first

    std::cout << "After deletion:\n";
    for (int x : my_list) std::cout << x << '\n';
}

Output:

Mastering std::list in C++: Syntax, Functions & Practical Examples

Key Takeaways

C Language

  1. C++ Operators Explained: Types, Examples, and Sample Programs
  2. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  3. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  4. Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
  5. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  6. C++ Structs Explained with a Practical Example
  7. Mastering std::map in C++: Comprehensive Guide with Code Examples
  8. C++ Classes & Objects: A Practical Guide with Code Examples
  9. C++ Polymorphism Explained: Practical Examples & Key Concepts
  10. Python List index() – How to Find Element Positions with Practical Examples