. Advertisement .
..3..
. Advertisement .
..4..
There is no shortage of ways to create array of structs in C++. Check out the common solutions below.
Create Array Of Structs In C++
With C-Style Arrays
The most obvious solution for this application is the built-in array data structure. In C++, it retains most of the popular characteristics you may be already familiar with in C.
These C-style arrays are sequences of C++ objects of the same type, including structs. Each array like that occupies a contiguous memory area.
The following example uses a regular C-style array to hold several struct instances in C++:
#include <iostream>
int main() {
struct Site{
std::string name;
std::string domain;
std::string type;
int rank;};
Site site_arr[3] = {
{"ITTutoria", "ittutoria.net", "Q&A", 1},
{"Stack Overflow", "stackoverflow.com", "Q&A", 2},
{"Reddit", "reddit.com", "Forum", 3}};
for (const auto &arr : site_arr) {
std::cout << "Website: " << arr.name << std::endl
<< "Domain: " << arr.domain << std::endl
<< "Type: " << arr.type << std::endl
<< "Rank: " << arr.rank << std::endl << std::endl;
}
return 0;
}
Output:
Website: ITTutoria
Domain: ittutoria.net
Type: Q&A
Rank: 1
Website: Stack Overflow
Domain: stackoverflow.com
Type: Q&A
Rank: 2
Website: Reddit
Domain: reddit.com
Type: Forum
Rank: 3
First, you will need to define a structure type with the struct keyword. The above code creates a structure that consists of four members, including their names and data types.
The braces below it declare an array that holds 3 structs we have defined. To specify the size of the array, you will need to declare it right after the array name, not its type name as required in other languages. The structure type name is used to specify the type of the array (i.e., Site).
The example above uses a stack declaration, which is faster to access and allocate than the heap-based method. This approach doesn’t work well if you have a big array, however. If your array has so many elements that they eat up too much memory, check out heap declarations.
The final section of our program uses a for loop to go through each member of a struct in the array and print it to the output.
With std::array and std::vector
While traditional C-style arrays are still supported by C++ standards, they are a common source of bugs. We don’t recommend using them in a modern code base.
To store a sequence of related C++ objects, you should use containers like std::array and std::vector. These class templates are safer to use and can also save your elements in a contiguous memory block. Compared to C-style arrays, they have better support for iterators and type safety.
This is a program that uses std::array (from the array header file) to carry out the same function of the previous example:
#include <iostream>
#include <array>
int main() {
struct Site{
std::string name;
std::string domain;
std::string type;
int rank;};
std::array<Site, 3> site_arr = {{
{"ITTutoria", "ittutoria.net", "Q&A", 1},
{"Stack Overflow", "stackoverflow.com", "Q&A", 2},
{"Reddit", "reddit.com", "Forum", 3}}};
for (const auto &arr : site_arr) {
std::cout << "Website: " << arr.name << std::endl
<< "Domain: " << arr.domain << std::endl
<< "Type: " << arr.type << std::endl
<< "Rank: " << arr.rank << std::endl << std::endl;
}
return 0;
}
The vector class in the standard library can also hold elements of the same type. It should be the preferred choice when you need fast random-access performance. You can rewrite your program with std::vector like this:
#include <iostream>
#include <vector>
int main() {
struct Site{
std::string name;
std::string domain;
std::string type;
int rank;};
std::vector<Site> site_arr = {
{"ITTutoria", "ittutoria.net", "Q&A", 1},
{"Stack Overflow", "stackoverflow.com", "Q&A", 2},
{"Reddit", "reddit.com", "Forum", 3}};
for (const auto &arr : site_arr) {
std::cout << "Website: " << arr.name << std::endl
<< "Domain: " << arr.domain << std::endl
<< "Type: " << arr.type << std::endl
<< "Rank: " << arr.rank << std::endl << std::endl;
}
return 0;
}
To learn more about printing vectors in C++, check out this guide.
Summary
You can create array of structs in C++ with a traditional C-style array or a container like std::array or std::vector. They can all work with your code, but the vector and array classes come with greater type safety and make your program less prone to bugs.
Leave a comment