Data structure in c ++

2

Hello, when I learned C I made several libraries, from heaps, binary trees, lists, rows ... etc.

I would like to know if in C ++ there are libraries that we can import to use more easily, as with the <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;
}

Because we imported the <string> library, it made it easier to manipulate the strings.

So I wanted to know if we can give import of such as <heap> and terms the ready-to-use data structure

    
asked by anonymous 05.08.2018 / 23:03

2 answers

1

To use existing libraries instead of creating your own libraries, ideally check the documentation which are 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 heap.h file like this:

#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 its struct s, classes, macros, and functions, but does not include implementations, declarations, and prototypes (except in some specific cases).

You implement the heap in a heap.c file:

#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 #include :

#include "heap.h"

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

Note this #ifndef , #define and #endif in heap.h . They serve to prevent any problems from occurring if the file is for some reason included twice.

    
06.08.2018 / 16:46
1

If you review this link: link It talks about stl containers, and these containers are nothing more than "use" the implementation of the most common data structures like: queues, stack, heaps, threaded lists, binary trees and so on to solve the most varied types of problems. for example: queues we have (std :: queue), heaps (std :: stack), heaps (std :: priority_queue), linked lists (std :: list), binary trees (std :: set). So you can, for example, include:

#include <queue>

and be using all the power of a std :: queue, the methods back, push, pop and so on goes I hope I have helped.

    
06.08.2018 / 14:51