How to Create std::vector of Custom Classes or Structs in C++?
Last Updated :
02 Apr, 2024
In C++, std::vectors are dynamic arrays that can resize themselves during the runtime, and classes or structs allow the users to create custom data types of their choice. There might be many situations when we want to store a custom data type into a vector. In this article, we will learn how to create a vector of custom classes or structs in C++.
Example:
Input:
// User-defined data type
struct StudentInfo {
int id;
string name;
};
Output:
Struct Vector Elements:
{ID: 101, Name: David}
{ID: 102, Name: John}
{ID: 103, Name: Frank}
Creating std::vector of Custom Classes or Structs
The process of creating a std::vector of custom classes or structs is similar to creating a vector of built-in data types. First, define a class or struct, then declare a std::vector using that class or struct as the template argument, and then use the std::vector::push_back() function to store instances of that type.
Note: The minimum requirement for the vector elements is that they should be atleast copy constructible and assignable. Custom classes generally matches both criteria.
Syntax
vector<DataType> vectorName;
Here,
DataType
is the name of the user-defined data type.vectorName
is the name of the vector.
C++ Program to Create std::vector of Custom Classes or Structs
The following program illustrates how we cancreate a std::vector of custom classes and structs in C++.
C++
// C++ Program to illustrate how to create a vector of
// custom classes or structs
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Initializing a class Student to create a custom Data Type
class Student {
public:
int id;
string name;
Student(int studentId, string studentName) :
id(studentId), name(studentName){}
};
// Initializing a struct StudentInfo to create a custom Data
// Type
struct StudentInfo {
int id;
string name;
StudentInfo(int studentId, string studentName)
: id(studentId)
, name(studentName)
{
}
};
int main()
{
// Initializing a vector of Student objects
vector<Student> studentVector;
// Adding Student objects to the vector
studentVector.push_back(Student(201, "Rahul"));
studentVector.push_back(Student(202, "Neha"));
studentVector.push_back(Student(203, "Amit"));
// Initializing a vector of StudentInfo objects
vector<StudentInfo> studentInfoVector;
// Adding StudentInfo objects to the vector
studentInfoVector.push_back(StudentInfo(101, "David"));
studentInfoVector.push_back(StudentInfo(102, "John"));
studentInfoVector.push_back(StudentInfo(103, "Frank"));
// Printing the elements from the vectors
cout << "Class Vector Elements:" << endl;
for (const auto& student : studentVector) {
cout << "{ID: " << student.id
<< ", Name: " << student.name << "}" << endl;
}
cout << endl;
cout << "Struct Vector Elements:" << endl;
for (const auto& studentInfo : studentInfoVector) {
cout << "{ID: " << studentInfo.id
<< ", Name: " << studentInfo.name << "}"
<< endl;
}
return 0;
}
OutputClass Vector Elements:
{ID: 201, Name: Rahul}
{ID: 202, Name: Neha}
{ID: 203, Name: Amit}
Struct Vector Elements:
{ID: 101, Name: David}
{ID: 102, Name: John}
{ID: 103, Name: Frank}
Time Complexity: O(N), here N denotes the size of the vector.
Auxiliary Space: O(N)
Similar Reads
Program to create Custom Vector Class in C++ The task is to implement a custom vector class similar to the STL vector with following functions: int push_back(data): adds an element(of any data_type) to the end of array and also returns the number of elements in that vectordata_type pop_back(): removes an element from the end of array, also ret
5 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 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 Sort a Vector of Custom Objects in C++? In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the s
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