If you are the one looking for guidance on vector of objects in C++, follow this article’s guidelines.
What is a Vector of Objects in C++
In C++, a vector of objects is a data structure that allows users to store a set of related objects or data types. It works similarly to a regular array and offers an advantage to the users of being resizable, which means it can grow or shrink according to the needs of a program. This feature is especially helpful when working with large datasets of varying sizes.
How to Create Vector of Objects in C++
A vector of class objects seems to be an example of such a custom vector in which multiple class instances can be stored.
The following C++ program will create a vector of class objects.
#include <string>
#include <vector>
using namespace std;
int rand_Integer(int x, int y)
{
return (x + rand() % y);
}
string rand_String(int len1)
{
string str1;
for (int i = 0; i < len1; i++) {
char ch = 'A' + rand() % 26;
str1.push_back(ch);
}
return str1;
}
class Employee
{
string name;
int id;
int age;
public:
void getter()
{
name = rand_String(10);
id = rand_Integer(10, 30);
age = rand_Integer(25, 40);
}
void disp()
{
cout << name << "\t" << id << "\t" << age << "\t" << "\n";
}
};
int main()
{
vector v1;
Employee s;
for (int i = 0; i < 5; i++)
{
s.getter();
v1.push_back(s);
}
for (int i = 0; i < 5; i++)
{
v1[i].disp();
}
return 0;
}
In the above code, we have randomly generated the data (name, age, and id) of five employees using a vector of class objects named as “Employee”. Where we defined the range of 10-30 for employee’s id and 25-40 for their age.
Advantages of Using Vector of Objects in C++
There are several advantages of using a vector of objects in C++, which are as follows:
- Allows users to efficiently store, manage, and access data.
- The users can add, remove, or manipulate data according to their needs without needing to create an array from scratch.
- Optimizes the memory layout of objects.
- Easily resize the array during the runtime.
- Performance is improved when it deals with large data.
A Vector Having Class Pointer
You can also use a vector of objects in C++ to store the object address instead of directly inserting objects into vectors. Here is an example for such a case:
#include <vector>
using namespace std;
class owner
{
public:
string name;
};
int main()
{
vector sites;
owner* s1 = new owner();
s1->name = "Alex";
owner* s2 = new owner();
s2->name = "Sam";
sites.push_back(s1);
sites.push_back(s2);
for (auto it : sites) {
cout << "Owner:" <name
<< " ID of Owner:" << it << endl;
}
return 0;
}
In this code, we used a vector of objects in C++ to store the address of an object named as “owner” instead of directly inserting objects into the vector. Here we used an arrow operator to access the members of the object.
Conclusion
A useful container in C++ that allows users to store multiple objects of the same type in a single place is called vector of objects. It helps make your code more efficient and flexible. Further, using vectors of objects, you can easily manipulate and access the data that helps you write more complex programs with ease.