How to Declare a List in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a List in C++To declare a list in C++, we use the std::list template from the STL library, specifying the type of elements the list will store, such as integers. We can then add elements using the list::push_back method and iterate through the list to access and print its elements. Syntax to Declare a List in C++ list<dataType> list_name;here, dataType: is the type of data that a list will store.list_name: is the name of the list container.C++ Program to Declare a ListThe following program illustrates how we can declare a list in C++: C++ // C++ Program to illustrates how we can declare a list #include <iostream> #include <list> using namespace std; int main() { // Declaring an empty list of integers list<int> listData; // Adding elements to the list listData.push_back(10); listData.push_back(20); listData.push_back(30); listData.push_back(40); // Printing the elements of the list cout << "List elements:"; for (int num : listData) { cout << " " << num; } cout << endl; return 0; } OutputList elements: 10 20 30 40 Time Complexity: O(N) , where N is the number of list elementsAuxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Declare a List in C++? G gpancomputer Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Declare a Map in C++? In C++, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to declare a map in C++. How to declare a map in C++?In C++, you can declare a map u 2 min read How to Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector 2 min read How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of 2 min read How to Copy a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to copy one list to another in C++. Input: sourceList = {10, 20, 30, 40, 50};Output: 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read Like