Essential STL List Container Operations in C++

List Container Overview

STL list is a sequence container supporting bidirectional iteration with constant time insertions and deletions at any position. Implemented as a doubly-linked list, each element resides in independent nodes connected via pointers. Unlike vector and array containers, list excels at frequent insertions and removals but lacks random access capabilities, requiring linear time for element access.

Constructors

Default Construction

#include <list>
#include <string>
using namespace std;

int main() {
    list<string> str_list;
    return 0;
}

Fill Construction

#include <list>
using namespace std;

int main() {
    list<int> nums(4, 100);
    list<float> floats(2);
    
    for (auto val : nums) cout << val << " ";
    cout << endl;
    for (auto val : floats) cout << val << " ";
    return 0;
}

Iterator Range Construction

#include <list>
#include <vector>
using namespace std;

int main() {
    vector<int> vec{10, 20, 30, 40};
    list<int> lst1(vec.begin(), vec.end());
    
    int arr[] = {5, 15, 25};
    list<int> lst2(arr, arr + 3);
    
    for (auto x : lst1) cout << x << " ";
    cout << endl;
    for (auto x : lst2) cout << x << " ";
    return 0;
}

Copy Operations

#include <list>
using namespace std;

int main() {
    list<char> original{'a', 'b', 'c'};
    list<char> copy1(original);
    list<char> copy2 = original;
    
    for (auto c : copy1) cout << c;
    cout << endl;
    for (auto c : copy2) cout << c;
    return 0;
}

Iterators

Forward Iteration

#include <list>
using namespace std;

int main() {
    list<double> values{1.1, 2.2, 3.3};
    auto it = values.begin();
    while (it != values.end()) {
        cout << *it << " ";
        ++it;
    }
    return 0;
}

Reverse Iteration

#include <list>
using namespace std;

int main() {
    list<int> numbers{7, 8, 9};
    auto rit = numbers.rbegin();
    while (rit != numbers.rend()) {
        cout << *rit << " ";
        ++rit;
    }
    return 0;
}

Capacity Operations

Size and Empty Checks

#include <list>
using namespace std;

int main() {
    list<string> items{"apple", "banana"};
    cout << "Size: " << items.size() << endl;
    cout << "Empty: " << items.empty() << endl;
    
    items.clear();
    cout << "After clear - Empty: " << items.empty() << endl;
    return 0;
}

Element Access

Front and Back Access

#include <list>
using namespace std;

int main() {
    list<int> sequence{50, 60, 70};
    cout << "First: " << sequence.front() << endl;
    cout << "Last: " << sequence.back() << endl;
    return 0;
}

Modification Operations

Insertion Operations

#include <list>
using namespace std;

int main() {
    list<int> data{2, 3, 4};
    data.push_front(1);
    data.push_back(5);
    
    auto pos = data.begin();
    advance(pos, 2);
    data.insert(pos, 99);
    
    for (auto x : data) cout << x << " ";
    return 0;
}

Removal Operations

#include <list>
using namespace std;

int main() {
    list<int> values{10, 20, 30, 40, 50};
    values.pop_front();
    values.pop_back();
    
    auto it = values.begin();
    while (it != values.end()) {
        if (*it == 30) {
            it = values.erase(it);
        } else {
            ++it;
        }
    }
    
    for (auto v : values) cout << v << " ";
    return 0;
}

Swap and Clear

#include <list>
using namespace std;

int main() {
    list<int> first{1, 2, 3};
    list<int> second{4, 5, 6};
    
    first.swap(second);
    
    cout << "First: ";
    for (auto x : first) cout << x << " ";
    
    cout << "\nSecond: ";
    for (auto x : second) cout << x << " ";
    
    first.clear();
    cout << "\nFirst size after clear: " << first.size();
    return 0;
}

Special List Operations

Splice Operations

#include <list>
using namespace std;

int main() {
    list<int> target{1, 2};
    list<int> source{3, 4, 5};
    
    target.splice(target.end(), source);
    
    cout << "Target: ";
    for (auto x : target) cout << x << " ";
    
    cout << "\nSource size: " << source.size();
    return 0;
}

Remove Operations

#include <list>
using namespace std;

int main() {
    list<int> numbers{1, 2, 1, 3, 1, 4};
    numbers.remove(1);
    
    for (auto n : numbers) cout << n << " ";
    return 0;
}

Sorting and Deduplication

#include <list>
using namespace std;

int main() {
    list<int> unsorted{3, 1, 4, 1, 5, 9, 2};
    unsorted.sort();
    unsorted.unique();
    
    for (auto x : unsorted) cout << x << " ";
    return 0;
}

Reversal

#include <list>
using namespace std;

int main() {
    list<char> letters{'a', 'b', 'c'};
    letters.reverse();
    
    for (auto c : letters) cout << c << " ";
    return 0;
}

List vs Vector Comparison

Feature Vector List
Underlying Structure Dynamic array Doubly-linked list
Random Access O(1) O(n)
Insert/Delete O(n) at arbitrary positions O(1) at known posisions
Memory Usage Contiguous, cache-friendly Fragmented, higher overhead
Iterator Type Raw pointers Encapsulated node pointers
Iterator Invalidation On reallocation Only deleted elements
Use Cases Random access, sequential processing Frequent insertions/deletions

Tags: C++ STL list containers Data Structures

Posted on Mon, 13 Jul 2026 17:25:55 +0000 by myflashstore