How to Initialize a Deque from a Vector in C++? Last Updated : 15 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the Standard Template Library(STL) provides a container deque also known as a double-ended queue where insertion and deletions are possible from both ends. On the other hand, vectors are dynamic containers that can resize themselves during the insertion or deletion of elements. In this article, we will learn how to initialize a deque using a vector in C++. Example Input: myVector = {1, 4, 5, 9, 7} Output: myDequeu = {1 4 5 9 7}Initializing a Deque from a Vector in C++The most efficient way to initialize a deque from a vector in C++ is using the Range Constructor provided by the std::deque class. The constructor takes two iterators which represent the beginning and the end of the range of elements to be copied in the deque and copies them easily inside the deque from the vector. C++ Program to Initialize a Deque Using a Vector C++ // C++ program to Initialize a Deque from a Vector #include <deque> #include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = { 10, 20, 30, 40, 50 }; // Print the elements of the vector cout << "Elements in Vector:"; for (int num : vec) { cout << num << " "; } cout << endl; // Initialize a deque from vector using range // constructor deque<int> Dq(vec.begin(), vec.end()); // Print elements of deque cout << "Elements in Deque:"; for (int num : Dq) { cout << num << " "; } cout << endl; return 0; } OutputElements in Vector:10 20 30 40 50 Elements in Deque:10 20 30 40 50 Time Complexity: O(N) where N is the total number of elements in the vectorAuxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Initialize a Deque from a Vector in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-deque CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 min read How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector 2 min read How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read How to Initialize a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim 2 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u 2 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Extract a Subvector from a Vector in C++? A subvector is a continuous part from the original vector. In this article, we will learn how to extract the subvector from a vector in C++.The simplest method to extract the subvector form a vector is by using vector's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h 3 min read How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa 2 min read Like