How to Create Vectors of Unique Pointers in C++? Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the vector is defined as a dynamic array that can grow or shrink in size. Vectors of unique pointers are commonly used to manage the collections of dynamically allocated objects as they combine the dynamic resizing capability of vectors with the automatic memory management provided by unique pointers. In this article, we will learn to create vectors of unique pointers in C++. Creating Vector of Unique Pointers in C++To create vectors of unique pointers in C++, we can first define the vector of type unique_ptr of the desired type and then use the std::make_unique defined inside <memory> header file that to create a unique_ptr object, which is a smart pointer that manages the lifetime of dynamically allocated objects. Syntax to Create Vectors of Unique Pointers in C++vector<unique_ptr<Type>> myVector; Here, Type is the type of objects that the unique pointers will point to.constructor_arguments are optional arguments that are passed to the constructor of the object being created.C++ Program to Create Vectors of Unique PointersThe below program demonstrates how we can create a vector of unique pointers in C++. C++ // C++ Program to illustrate how to create vectors of unique // pointers #include <iostream> #include <memory> #include <vector> using namespace std; // Creating a class MyClass class MyClass { public: // Constructor that takes two integers as parameters MyClass(int num1, int num2): num1(num1), num2(num2){} // Function to compute the sum of num1 and num2 int sum() const { return num1 + num2; } private: // Private data members num1 and num2 int num1; int num2; }; int main() { // Declaring a vector of unique pointers to MyClass // objects vector<unique_ptr<MyClass> > myVector; // Allocate objects and store them in the vector myVector.push_back(make_unique<MyClass>(5, 10)); myVector.push_back(make_unique<MyClass>(8, 3)); // Accessing the first object in the vector and // computing its sum cout << "Sum of first object: " << myVector[0]->sum() << endl; // Iterating over the vector and computing the sum of // each object for (auto& ptr : myVector) { cout << "Sum: " << ptr->sum() << endl; } return 0; } OutputSum of first object: 15 Sum: 15 Sum: 11 Time Complexity: O(N), where N is the size of the vector.Auxilliary Space: O(N) Comment More infoAdvertise with us Next Article How to Create a Vector of Vectors of Pairs in C++? S syam1270 Follow Improve Article Tags : C++ Programs C++ STL cpp-pointer cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 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 Vectors of Pairs in C++? In C++ STL, we can nest different containers into each other at any number of levels. On such container is the Vector of Vectors of Pairs i.e. 2D vector of pairs. In this article, we will learn how to create and traverse a vector of vector of pairs in C++ STL. Vector of Vectors of Pairs in C++ The 2 3 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 Like