How to Copy a One Deque to Another in C++?
Last Updated :
03 Apr, 2024
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}
Copying All Elements of One Deque to Another Deque in C++
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.
Syntax to Copy a Deque in C++
copy(oldDeque.begin(), oldDeque.end(), newDeque.begin());
Here,
- oldDeque.begin() is the iterator to the beginning of the original deque (oldDeque).
- oldDeque.end() is the iterator to the end of the original deque (oldDeque).
- newDeque.begin() is the iterator to the beginning of the destination deque(newDeque) where we have to copy the elements of the original deque.
C++ Program to Copy One Deque to Another
The below program demonstrates how we can copy one deque to another using copy() function in C++.
C++
// C++ Program to Copy One Deque to Another
#include <algorithm>
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Original deque
deque<int> oldDeque = { 1, 2, 3, 4, 5 };
// destination deque
deque<int> newDeque(oldDeque.size());
// Copying Deque Using copy algorithm
copy(oldDeque.begin(), oldDeque.end(),
newDeque.begin());
// Printing original deque
cout << "Original deque:";
for (const auto& elem : oldDeque) {
cout << " " << elem;
}
cout << endl;
// Printing copied deque
cout << "Copied deque:";
for (const auto& elem : newDeque) {
cout << " " << elem;
}
cout << endl;
return 0;
}
OutputOriginal 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)
Similar Reads
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 Compare Two Deques in C++? In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++. Example: Input: deque1 = {10,20,30}; deque2 = {10,20,30};
2 min read
How to Sort a Deque in C++? In C++, the STL provides a container called a double-ended queue which is popularly known as deque. This container allows fast insertions and deletions at both ends of the container. In this article, we will learn how to sort a deque in C++. Example: Input: myDeque = {30, 10, 20,50,40} Output: 10 20
2 min read
C++ Program to Copy One File into Another File To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++. To copy the file using C++, we read the contents of the source file and write it into the destination file. Steps to copy one file to another in C++: Create objects of ifstream
2 min read
How to Create a Deque of Arrays in C++? In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends whereas arrays are fixed-size collections of elements. In this article, we will learn how to create a deque of arrays in C++ STL. Example: Input: myArray1 = {1, 4, 8, 9, 11} myArray2 = {1, 2, 3,
2 min read