Open In App

How to Create std::vector of Custom Classes or Structs in C++?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}

Output
Class 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)





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads