Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Arrays of Structs | Struct Usage
Quizzes & Challenges
Quizzes
Challenges
/
C++ Structures and Enumerations

bookArrays of Structs

When you need to represent multiple related items with the same structure, such as a group of people, you can use arrays or vectors of structs. Suppose you have already defined a Person struct with members like name, age, and height. Declaring an array of Person structs allows you to store and manage a fixed-size collection of people. For example, you can create an array to represent a classroom roster or a team.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> #include <string> struct Person { std::string name; int age; double height; }; int main() { Person people[3] = { {"Alice", 30, 5.5}, {"Bob", 25, 5.9}, {"Charlie", 28, 5.7} }; for (int i = 0; i < 3; ++i) std::cout << "Name: " << people[i].name << std::endl; }

Vectors offer several advantages over arrays when working with dynamic collections of structs:

  • Allow you to add or remove elements at runtime;
  • Automatically manage memory and resizing;
  • Provide useful member functions such as push_back, pop_back, and size;
  • Help prevent common errors like buffer overflows;
  • Make code easier to read and maintain.
Note
Note

Use a std::vector when the size of your collection may need to change during program execution.

question mark

What is a main advantage of using an array of structs to represent multiple related items?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me how to declare an array of Person structs?

What is the syntax for creating a vector of Person structs?

Can you give an example of adding a new Person to a vector?

Awesome!

Completion rate improved to 8.33

bookArrays of Structs

Desliza para mostrar el menú

When you need to represent multiple related items with the same structure, such as a group of people, you can use arrays or vectors of structs. Suppose you have already defined a Person struct with members like name, age, and height. Declaring an array of Person structs allows you to store and manage a fixed-size collection of people. For example, you can create an array to represent a classroom roster or a team.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> #include <string> struct Person { std::string name; int age; double height; }; int main() { Person people[3] = { {"Alice", 30, 5.5}, {"Bob", 25, 5.9}, {"Charlie", 28, 5.7} }; for (int i = 0; i < 3; ++i) std::cout << "Name: " << people[i].name << std::endl; }

Vectors offer several advantages over arrays when working with dynamic collections of structs:

  • Allow you to add or remove elements at runtime;
  • Automatically manage memory and resizing;
  • Provide useful member functions such as push_back, pop_back, and size;
  • Help prevent common errors like buffer overflows;
  • Make code easier to read and maintain.
Note
Note

Use a std::vector when the size of your collection may need to change during program execution.

question mark

What is a main advantage of using an array of structs to represent multiple related items?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
some-alt