Creating a Vector of Class Objects in C++ Last Updated : 10 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisites: Object Oriented Programming in C++Vector in C++ Class is a user-defined data type that can be accessed by creating objects or instances of that class. A vector is a type of container which can store elements of a similar type. Vector of Class The vector of class objects is an example of a custom vector made where we can store multiple class instances. Example: Class: Student -> { roll , name , age , marks } If we want to data about students then use this - vector<Student> v; C++ // C++ Program to create // a vector of class objects #include <iostream> #include <string> #include <vector> using namespace std; int randomInt(int start, int range) { // A function to generate random numbers return (start + rand() % range); } string randomString(int len) { // A function to generate random strings of length --> // "len" string str; for (int i = 0; i < len; i++) { char ch = 'A' + rand() % 26; str.push_back(ch); } return str; } class Student { int roll; string name; int age; int marks; public: void getter() { roll = randomInt(100, 50); name = randomString(10); age = randomInt(10, 10); marks = randomInt(200, 300); } void disp() { cout << roll << "\t" << name << "\t" << age << "\t" << marks << "\n"; } }; int main() { // Vector of class objects vector<Student> v; Student s; for (int i = 0; i < 10; i++) { // getting the random values from // functions s.getter(); // inserting objects to vector v.push_back(s); } for (int i = 0; i < 10; i++) { // displaying object data v[i].disp(); } return 0; } Output133 WLRBBMQBHC 17 490 109 ZOWKKYHIDD 12 430 112 DXRJMOWFRX 16 411 142 BLDBEFSARC 13 426 141 ECDYGGXXPK 17 436 105 ELLNMPAPQF 14 364 143 OPKMCOQHNW 19 332 110 EWHSQMGBBU 14 378 117 JJIVSWMDKQ 11 365 139 IXMVTRRBLJ 19 327Vector of class pointer It can be used for storing the addresses of the objects rather than directly inserting objects directly in a vector. Example: Class: Land = { name }; Every Land has it's owner if we want to store owner name. vector<Land *> sites; We can use iterator which will point to address where owner lives. Explanation: vector<Land *> ; means we can now only insert address of Land object rather than object itself. So, the address of two objects can be anywhere in a memory (not continuous). Land* s1 = new Land(); this is used as s1 is storing the address of Land which is allocated in heap memory using new Land() it->name and it it is iterator used for traversing . it is address of Land and it->name is the owner of Land. C++ // C++ Program to create // a vector of class objects // code for Example 2 #include <iostream> #include <vector> using namespace std; class Land { public: string name; }; int main() { // vector of class vector<Land*> sites; // Dynamic Memory Allocated Land* s1 = new Land(); s1->name = "abc"; Land* s2 = new Land(); s2->name = "xyz"; sites.push_back(s1); sites.push_back(s2); for (auto it : sites) { cout << "Owner:" << it->name << " Address_of_Land:" << it << endl; } return 0; } OutputOwner:abc Address_of_Land:0x1a9b010 Owner:xyz Address_of_Land:0x1a9b060 Comment More infoAdvertise with us Next Article How to create an Array of Objects in the Stack memory? H harsh464565 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-vector +1 More Practice Tags : CPPSTL Similar Reads Convert Set to Vector in C++ In C++, std::vectors stores data in the contiguous memory location while std::set stores data in non-contiguous memory location but in some specified order. In this article, we will learn different methods to convert the set to vector in C++.Table of ContentUsing Range Constructor of std::vectorUsin 3 min read vector :: cbegin() and vector :: cend() in C++ STL Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterat 2 min read Passing a Vector to Constructor in C++ Just like any other data, we can also pass a vector to a constructor of the desired class. We can pass it by value or by reference. What we do with it depends on our requirement. The following are some of the common cases:Copy Data to Member VectorIf we want to store the copy of the vector in the cl 3 min read C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and 9 min read How to create an Array of Objects in the Stack memory? What is an Array of Objects? An array of objects is a data structure that stores a collection of objects of the same type. The objects in the array are stored in contiguous memory locations, and the array provides indexed access to the objects. This means that you can access an individual object in 6 min read Using class to implement Vector Quantities in C++ A Vector Quantity is a Quantity which possesses both magnitude as well as direction. Here, magnitude is simply the amount or size of the quantity and direction is where the quantity is headed. For example, consider the statement "Go 20 miles north". In the above statement 20 is the magnitude and Nor 4 min read Like