How to Copy a List in C++ STL? Last Updated : 21 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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: copiedList = {10, 20, 30, 40, 50};Copying a List to Another List in C++To copy a std::list in C++, we can use the default copy constructor which creates a new list that is a copy of an existing list with the same type. It automatically copies all elements from the original list to the new list, maintaining the order. we just have to invoke the copy constructor by passing the source list as an argument. Syntax to Copy a List list<dataType> copiedList(existingList);Here, dataType is the type of the elements stored in the list.copiedList is the new list that we are creating.sourceList is the existing list from which we want to create a copy.C++ Program to Copy One List to Another The below program demonstrates how we can copy a std::list to another list in C++. C++ // C++ program to copy a list to another list #include <iostream> #include <list> using namespace std; int main() { // Creating and initializing an source list list<int> sourceList = { 1, 2, 3, 4, 5 }; // Creating a copied list using the copy constructor list<int> copiedList(sourceList); // Output the contents of the copied list cout << "Copied List: "; for (int num : copiedList) { cout << num << " "; } cout << endl; return 0; } OutputCopied List: 1 2 3 4 5 Time Complexity: O(N), where N denotes the size of the list.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Copy a List in C++ STL? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-list cpp-list-functions CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Copy a Stack in C++? In C++, stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we are going to discuss how to copy a stack in C++. Copying a Stack to Another in C++To copy a s 2 min read How to Sort a List in C++ STL? In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: myList = {30, 10, 20, 40, 50};Out 2 min read How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Declare a List in C++? 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 Lis 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 How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read How to Copy One Array to Another in C++? In C++, arrays are a type of data structure that stores a fixed-size collection of elements of the same type in a contiguous memory location, and sometimes we need to copy the contents of one array to another. In this article, we will learn how to copy one array to another in C++. Example: Input: ar 2 min read How to Copy a One Deque to Another in C++? In C++, STL provides a container called deque (short for double-ended queue) that allows fast insertion and deletion at both its beginning and its end. In some scenarios, we may need to copy the contents of one deque to another. In this article, we will learn how to copy one deque to another in C++. 2 min read Like