example2_doubly_linked_list.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <list>
#include <algorithm>

int main() {
    std::list<std::string> fruits = {"apple", "banana", "cherry"};

    // Inserting elements
    fruits.push_front("kiwi");
    fruits.push_back("mango");

    // Removing elements
    fruits.pop_front();

    // Finding and erasing
    auto it = std::find(fruits.begin(), fruits.end(), "banana");
    if (it != fruits.end()) {
        fruits.erase(it);
    }

    // Iterating and printing
    for (const auto& fruit : fruits) {
        std::cout << fruit << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to containers