In this article, we will see how to initialize and use vector of structs in C++.
There are different ways to do it. This article will explain these methods clearly.
[toc]
Using Range Constructor
We can utilize the range constructor for the initialization of vector of structs in C++. It is useful when we need to make another copy of the existing vector. We will declare a different vector and copy the elements from the old vector using the range. We can access the elements of structs using ‘struct.element’ at the time of output.
Code:
#include#include #include using namespace std; //Initializing Structure: Student struct Student{ string first_name; string last_name; int roll_no; }; int main(){ //creating vector of structure(Student) vector stu1 = {{"Steve", "Smith", 59}, {"Shane", "Watson", 57}, {"Adam", "Gilchrist", 3}, {"Ricky", "Ponting", 43}}; //Passing the range of vector stu1 from first to last position //Creating another copy of vector stu1 vector new_stu(stu1.begin(), stu1.end()); //Printing the new vector for (const auto &arr : new_stu) { cout << "First Name: " << arr.first_name << endl << "Last Name: " << arr.last_name << endl << "Roll Number: " << arr.roll_no << endl; } return 0; }
Output
Last Name: Smith
Roll Number: 59
First Name: Shane
Last Name: Watson
Roll Number: 57
First Name: Adam
Last Name: Gilchrist
Roll Number: 3
First Name: Ricky
Last Name: Ponting
Roll Number: 43
Further reading:
Using Initializer list
It is a simple method to create a vector of structs in C++. Here we initialize the container with the constant values. This method is better when it is important to have an initial state of some type for data structure.
Code:
#include#include #include using namespace std; //Initializing Structure: Student struct Student{ string first_name; string last_name; int roll_no; }; int main(){ //creating vector of structure(Student) vector stu1 = {{"Steve", "Smith", 59}, {"Shane", "Watson", 57}, {"Adam", "Gilchrist", 3}, {"Ricky", "Ponting", 43}}; //Printing the vector for (const auto &arr : stu1) { cout << "First Name: " << arr.first_name << endl << "Last Name: " << arr.last_name << endl << "Roll Number: " << arr.roll_no << endl; } return 0; }
Output
Last Name: Smith
Roll Number: 59
First Name: Shane
Last Name: Watson
Roll Number: 57
First Name: Adam
Last Name: Gilchrist
Roll Number: 3
First Name: Ricky
Last Name: Ponting
Roll Number: 43
Using custom constructor
In this method, we create a vector specific constructor which will initialize the vector with a given number filled with different values. Here we will take input from the user and we create a custom vector of structs with different values.
Code:
#include#include #include using namespace std; struct Student{ string first_name; string last_name; int age; }; constexpr int count1 = 2; int main(){ //Custom Constructor to print the same values number of times vector stu1(count1, {"Donald", "Bradman", 32}); //Custom constructor to take complete input from the user vector stu2; Student p; int count; cout<<"Enter the number of values you want to add:"; cin>>count; for(int i = 0; i < count; ++i) { cout << "Please enter the first name, last name and roll number of a student:" << endl; string first_name, last_name; int age1; cin >> first_name; cin >> last_name; cin >> age1; p.first_name=first_name; p.last_name=last_name; p.age=age1; stu2.push_back(p);//Adding elements in the vector } //Printing the vector stu1 cout<<"\nFirst Vector\n"; for (const auto &arr1 : stu1) { cout << "First Name: " << arr1.first_name << endl << "Last Name: " << arr1.last_name << endl << "Age: " << arr1.age << endl; } //Printing the vector stu2 cout<<"\nSecond Vector:\n"; for (const auto &arr1 : stu2) { cout << "First Name: " << arr1.first_name << endl << "Last Name: " << arr1.last_name << endl << "Age: " << arr1.age << endl; } return 0; }
Input
Please enter the first name, last name and roll number of a student:
Glenn
Maxwell
30
Please enter the first name, last name and roll number of a student:
David
Warner
21
Output
First Name: Donald
Last Name: Bradman
Age: 32
First Name: Donald
Last Name: Bradman
Age: 32
Second Vector:
First Name: Glenn
Last Name: Maxwell
Age: 30
First Name: David
Last Name: Warner
Age: 21
Bonus Program
Program to sort a vector of structs in C++ according to the roll numbers of the students.
Code
#include#include #include using namespace std; //Intializing struct struct Student { int roll_no; string student_name; }; //Printing the vector void Print(vector & vect1) { for (int i = 0; i < vect1.size(); ++i) { cout << vect1[i].roll_no << ' ' << vect1[i].student_name << endl; } cout << endl; } int main() { //Filling values in the vector of student(struct) type vector vect; vect.push_back({ 72, "Louis" }); vect.push_back({ 78, "Mike" }); vect.push_back({ 26, "Harvey" }); vect.push_back({ 15, "David" }); cout << "Original Vector Before Sorting:\n" << endl; Print(vect); //Sorting ascendigly according to the roll number sort(vect.begin(), vect.end(), [](Student a, Student b) { return a.roll_no < b.roll_no; }); cout << "Vector After Sorting:\n" << endl; Print(vect); return 0; }
Output
72 Louis
78 Mike
26 Harvey
15 David
Vector After Sorting:
15 David
26 Harvey
72 Louis
78 Mike
Conclusion
In this article we demonstrated different methods that can create a vector of structs in C++. Custom Constructor method can be used in other ways as well as per the needs of the program.
Happy Learning!!