To create a list of lists in C++ using `std::list`, you can create a list of lists by specifying lists as elements of the outer list.
In this example,
.list1
and list2
.listOfLists
using std::list>
and initialize it with the inner lists.#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 2, 3};
std::list<char> list2 = {'a', 'b', 'c'};
std::list<std::list<int>> listOfLists = {list1, list2};
for (const auto& innerList : listOfLists) {
for (const auto& element : innerList) {
std::cout << element << ' ';
}
std::cout << '\n';
}
return 0;
}
1 2 3 a b c
In this example,
.listOfListsEmpty
using std::list>
.list3
and list4
.push_back
method to add the inner lists list3
and list4
to the outer list listOfListsEmpty
.#include <iostream>
#include <list>
int main() {
std::list<std::list<int>> listOfListsEmpty;
std::list<int> list3;
std::list<char> list4;
listOfListsEmpty.push_back(list3);
listOfListsEmpty.push_back(list4);
for (const auto& innerList : listOfListsEmpty) {
for (const auto& element : innerList) {
std::cout << element << ' ';
}
std::cout << '\n';
}
return 0;
}
In this tutorial, we learned How to create a List of Lists in C++ language with well detailed examples.