Initialize Vector of Structs in C++
- Use Initializer List Constructor to Initialize a Vector of Structs in C++
- Use Range Constructor to Initialize a Vector of Structs in C++
- Use the Custom Constructor to Initialize a Vector of Structs in C++
This article will demonstrate multiple methods of how to initialize a vector of structs in C++.
Use Initializer List Constructor to Initialize a Vector of Structs in C++
An initializer list is a common way of initializing containers with constant values. This method is better suited for data structures that need to have a starting state of some sort. In the below example, since the vector
contains custom defined Person
structs, the initializer list items need to be grouped inside curly brackets and separated with colons. Note that elements of the struct
are accessed using the struct.element
notation and outputted to the console.
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl;
using std::vector; using std::string;
struct Person{
string name;
string surname;
int age;
};
int main(){
vector<Person> parr1 = {{"John", "Cooper", 32},
{"Theo", "Parrot", 23},
{"Aun", "Chao", 43},
{"Vivien", "Bardot", 67}};
for (const auto &arr : parr1) {
cout << "Name: " << arr.name << endl
<< "Surname: " << arr.surname << endl
<< "Age: " << arr.age << endl;
}
return EXIT_SUCCESS;
}
Output:
Name: John
Surname: Cooper
Age: 32
Name: Theo
Surname: Parrot
Age: 23
Name: Aun
Surname: Chao
Age: 43
Name: Vivien
Surname: Bardot
Age: 67
Use Range Constructor to Initialize a Vector of Structs in C++
Alternatively, the range constructor can be utilized to initialize thevector
of structs. This method is useful when one needs to create another copy of the existing vector
object. As shown in the following code sample, we declare a parr3
vector of struct Person
and initialize it with the contents of the parr1
vector of the same type.
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl;
using std::vector; using std::string;
struct Person{
string name;
string surname;
int age;
};
int main(){
vector<Person> parr1 = {{"John", "Cooper", 32},
{"Theo", "Parrot", 23},
{"Kim", "Colbert", 53},
{"Aun", "Chao", 43},
vector<Person> parr3(parr1.begin(), parr1.end());
for (const auto &arr : parr3) {
cout << "Name: " << arr.name << endl
<< "Surname: " << arr.surname << endl
<< "Age: " << arr.age << endl;
}
return EXIT_SUCCESS;
}
Output:
Name: John
Surname: Cooper
Age: 32
Name: Theo
Surname: Parrot
Age: 23
Name: Kim
Surname: Colbert
Age: 53
Name: Aun
Surname: Chao
Age: 43
Use the Custom Constructor to Initialize a Vector of Structs in C++
Another solution is a vector
specific constructor, which provides a feature to initialize a vector with a given number of the same values. In this case, we provide the constructor a single element of type struct Person
and an arbitrary number 3
to initialize the object.
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl;
using std::vector; using std::string;
struct Person{
string name;
string surname;
int age;
};
constexpr int NUM = 3;
int main(){
vector<Person> parr4(NUM, {"John", "Cooper", 32});
for (const auto &arr : parr4) {
cout << "Name: " << arr.name << endl
<< "Surname: " << arr.surname << endl
<< "Age: " << arr.age << endl;
}
return EXIT_SUCCESS;
}
Output:
Name: John
Surname: Cooper
Age: 32
Name: John
Surname: Cooper
Age: 32
Name: John
Surname: Cooper
Age: 32