![]() |
VOOZH | about |
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++.
Example:
Input:
deque1 = {10, 20, 30, 40, 50, 60}
Output:
deque2: {10, 20, 30, 40, 50, 60}
For copying all the elements stored in one std::deque to another in C++, we can use the std::copy() function provided in the STL <algorithm> library that copies a range of elements from one container to another.
copy(oldDeque.begin(), oldDeque.end(), newDeque.begin());Here,
The below program demonstrates how we can copy one deque to another using copy() function in C++.
Original deque: 1 2 3 4 5 Copied deque: 1 2 3 4 5
Time Complexity: O(N), where n is the number of elements in each deque.
Auxiliary Space: O(N)