lundi 29 octobre 2018

Create a new reverse list (own class) using vector in C++

I have the next code:

#include <vector>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <tchar.h>

class TList {
private:
     std::vector<const char*> elementos;
     int position;
     TList(std::vector<const char*> elementos);
public:
    TList(const char** e, int s);
    TList *GetReverseList();
    int Size();
    const char *Next();
};

TList::TList(const char** e, int s) {
    std::vector<const char*> res (&e[0], &e[s]);
    elementos = res;
    position = 0;
}

TList::TList(std::vector<const char *> elementos) {
    std::vector<const char*> res = std::vector<const char*>();
    int size = elementos.size();
    for (int i = 0; i < size; i++) {
        res.push_back(elementos.at(i));
    }
    elementos = res;
    position = 0;
}

//Create a new TList with the reverse list of elements
TList *TList::GetReverseList() {
    TList *res = new TList(elementos);
    std::reverse(res->elementos.begin(), res->elementos.end());
    return res;
}

int TList::Size() {
    return elementos.size();
}

//Use the position to get the next char *
const char * TList::Next() {
    const char * res;
    if (elementos.empty()) {
        res = NULL;
    }
    else {
        int pos = position;
        int size = elementos.size();
        res = pos == size ? elementos.at(position - 1) : elementos.at(position);
        if (pos < size) {
            position++;
        }
    }
    return res;
}

int main()
{
    int size = 2;
    const char *arr[2] = {"Hola", "AAAA"};
    TList *list = new TList(arr, size);
    TList *listReverse = list->GetReverseList();
    printf("Size: %u \n", listReverse->Size());
    printf("First value: %s \n", listReverse->Next());
    printf("Second value: %s \n", listReverse->Next());
    delete list;
    delete listReverse;
    return 0;
}

When I run it in Visual Studio it says in the console

Size: 0
First Value: (null)
Second Value: (null)

and it throw an exception "wntdll.pdb not loaded" "wntdll.pdb contains the debug information required to find the source for the module ntdll.dll" and also open this window:

enter image description here

I want to create a new TList as optimized as possible but with its elements reversed, I don't want to do a copy constructor because I need it as a function "GetReverseList" so, what can I do?

Aucun commentaire:

Enregistrer un commentaire