Data structure in c++

Asked

Viewed 765 times

2

Hello, when I learned C I did several libraries, since Heaps, binary trees, lists, queues...etc

I wonder if in C++ there are libraries that we can import to use more easily, as with libraries <string> or <vector>

Ex:

#include <iostream>
#include <string>

using namespace std;

int main () {

   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // total length of str3 after concatenation
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
}

How we import the library <string> allowed it to be easier to manipulate strings.

So I was wondering if we could import how for example <heap> and have the data structure ready to use

  • This type of question would result in an objective and short answer

  • 2

    I wonder if there are possible examples of how to apply...

2 answers

2

If you scan this link: http://www.cplusplus.com/reference/stl/ La talks about the containers of stl, and these containers are nothing more than the "use" the implementation of the most common data structures such as: queues, stack, Heaps, chained lists, binary trees and so on to solve the most varied types of problems. for example: queues we have (Std:::Queue), stacks (Std::stack), Heaps (Std::priority_queue), chained lists (Std:::list), binary trees (Std:::set). Then Voce can yes include for example:

#include <queue>

and be using all the power of a Std::What’s up, the back, push, pop methods and so on hope I’ve helped.

  • 2

    http://en.cppreference.com/ is a more reliable source.

1


To use existing libraries instead of creating your own, the ideal is to consult on documentation what are the available.

On the other hand, if you want to create your own library, you can create files that contain your data structure and header files that import them. For example, you can create a file heap.h thus:

#ifndef HEADER_HEAP
#define HEADER_HEAP

typedef struct ALGUM_STRUCT {
    // Os campos do seu struct aqui.
} ALGUM_STRUCT;

class Heap {
    // Os campos, protótipos dos métodos, construtores e destrutores aqui.

    public:
        Heap();
        ~Heap();
        void inserir_valor(int valor);
};

ALGUM_STRUCT *criar_algum_struct();

void fazer_alguma_coisa(ALGUM_STRUCT *exemplo, char a, char b);

// Outros protótipos de funções aqui.

#endif

The header file defines your structs, classes, macros and functions, but does not include implementations, only statements and prototypes (except in some specific cases).

You implement heap in a file heap.c:

#include "heap.h"

ALGUM_STRUCT *criar_algum_struct() {
    // blablabla
}

void fazer_alguma_coisa(ALGUM_STRUCT *exemplo, char a, char b) {
    // blablabla
}

Heap::Heap() {
    // blablabla
}

Heap::~Heap() {
    // blablabla
}

void Heap::inserir_valor(int valor) {
    // blablabla
}

And you can use the heap somewhere else by putting the #include:

#include "heap.h"

void outra_funcao_que_usa_heap() {
    Heap h = Heap();
    h.inserir_heap(123);
    ALGUM_STRUCT *x = criar_algum_struct();
    // blablabla
}

Watch this #ifndef, #define and #endif in the heap.h. Serve to prevent any problem if the file is for some reason included twice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.