Estructura de datos (Data Structure Struct)

Las estructuras de datos se emplean con el objetivo de organizar los datos dentro de la memoria del ordenador. Una vez los datos organizados en el ordenador se puede realizar diferentes procesos estas estructuras. Un ejemplo básico de estructura de dato son los arreglos, pero en este caso no hablaremos de los arreglos si no mas bien de la estructuras en C++.

Las estructuras a diferencia de los array, nos permite agrupar varios datos, que mantengan algún tipo de relación entre ellos aunque sean de distintos tipos permitiendo manipularlos todos juntos, usando un mismo identificador o cada uno separado.

Para definir una estructura utilizamos la palabra reservada struct seguido al nombre de la estructura, luego llaves con las propiedades o elementos de las estructura.

#include <iostream>

using namespace std;

struct Book {
    long isbn;
    string title;
    string author;
    bool available;
};

int main()
{
    struct Book book1, book2;

    book1.isbn = 97852670063;
    book1.title = "1984";
    book1.author = "George Orwell";
    book1.available = 12;

    book2.isbn = 97152972264;
    book2.title = "Make It Time";
    book2.author = "Jake Knapp";
    book2.available = 6;

    cout << book1.title << endl;

    cout << book2.author << endl;

    return 0;
}
//-> 1984
//-> Jake Knapp

Acceder a propiedades de estructura

Para acceder a propiedades de las estructura utilizamos el operador . .

book1.author = "George Orwell";

Estructuras como parámetros de argumentos en una función

Las estructuras similar a las variables regulares pueden ser pasada como parámetros a funciones.

#include <iostream>

using namespace std;

struct Book {
    long isbn;
    string title;
    string author;
    bool available;
};

void printBook(struct Book book) {
    cout << "Book title is " << book.title; 
    cout << " and author is " << book.author << endl;
}

int main()
{
    struct Book book1, book2;

    book1.isbn = 97852670063;
    book1.title = "1984";
    book1.author = "George Orwell";
    book1.available = 12;

    book2.isbn = 97152972264;
    book2.title = "Make It Time";
    book2.author = "Jake Knapp";
    book2.available = 6;

    printBook(book1);
    printBook(book2);

    return 0;
}
//-> Book title is 1984 and author is George Orwell
//-> Book title is Make It Time and author is Jake Knapp

Uso de punteros para estructura

También podemos declarar punteros con estructura de la misma forma que lo implementamos con variables comunes. La única diferencia que para acceder a sus propiedades utilizamos el operador -> flecha en estos casos o el siguiente sintaxis (*ptr).propiedad .

#include <iostream>

using namespace std;

struct Book {
    long isbn;
    string title;
    string author;
    bool available;
};

void printBook(struct Book *book) {
    cout << "Book title is " << book->title; 
    cout << " and author is " << book->author << endl;
}

int main()
{
    struct Book book1, book2;

    book1.isbn = 97852670063;
    book1.title = "1984";
    book1.author = "George Orwell";
    book1.available = 12;

    book2.isbn = 97152972264;
    book2.title = "Make It Time";
    book2.author = "Jake Knapp";
    book2.available = 6;

    printBook(&book1);
    printBook(&book2);

    return 0;
}
//-> Book title is 1984 and author is George Orwell
//-> Book title is Make It Time and author is Jake Knapp

results matching ""

    No results matching ""