How to Declare a Vector in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 in C++, we first need to include the <vector> header. Then, we can declare a vector variable or object or instance using the below syntax Syntax to Declare Vector in C++vector<dataType> vector_Name;Here, vector_name: name of the instance.dataType: type of each element of the vector.The above vector will be constructed using the default constructor. We can also create a vector with different constructors as shown here. C++ Program to Declare a VectorThe following program illustrates how we can declare a vector in C++: C++ // C++ Program to illustrates how we can declare a vector #include <iostream> #include <vector> using namespace std; int main() { // Declare a vector of integers vector<int> vec = { 1, 2, 3, 4, 5 }; // Print the elemetns of the vector cout << "Vector Elements: "; for (int number : vec) { cout << number << " "; } cout << endl; return 0; } OutputVector Elements: 1 2 3 4 5 Time Complexity: O(N) where N is the size of the vector.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Declare a Vector in C++? G ghuleyogesh Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 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 Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read How to Create a Vector of Tuples in C++? In C++, a tuple is an object that allows the users to store elements of various data types together while a vector is used to store elements of the same data types. In this article, we will learn how we can create a vector of tuples in C++. Example: Input: Tuple1 ={10,A,5.3} Tuple2= {20,B,6.5}Output 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Create a Deque of Vectors in C++? In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++ 2 min read How to convert a Vector to Set in C++ This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea 4 min read Like