S
S
Sergo Zar2021-12-15 01:07:09
C++ / C#
Sergo Zar, 2021-12-15 01:07:09

Why doesn't my vector work with strings?

I'm trying to implement my version of vectors (not necessarily the correct one, etc. I'm doing this for laba).
And it works as I wanted, but for some reason it does not want to work with the string type.

my code
#include <iostream>
#include <cassert>
#include "colors.h"
using namespace std;

template <class T>
class vector{
  private:
    int length;
    T * arr, * temp;
    void clon(bool a_to_t = true){
        if (a_to_t)
            for(int i = 0; i < length; i++)
                    temp[i] = arr[i];
        else
            for(int i = 0; i < length; i++)
                    arr[i] = temp[i];
    }
    void log(){
        cout << "length :" << length << endl;
        (arr != nullptr) ? cout << "arr[0]:" << arr[0] << endl : cout<<endl;
        (temp != nullptr) ? cout << "temp[0]:" << arr[0] << endl : cout<<endl;
    }
  public: 
    vector(){
        length = 0; 
        arr = nullptr;
        temp = nullptr;
    }
    vector(int l){
        length = l;
        arr = new T[length];
        temp = nullptr;
    }
    ~vector(){
        delete[] arr, temp;
    }
    void pushBack(T value){
        length++;
        if(arr == nullptr){
            arr = new T[length];
            arr[0] = value;
            cout << 1 << endl;
            log(); // 1
        }
        else{
            cout << 2 << endl;
            log(); // 2
            temp = new T[length];
            cout << 3 << endl;
            log(); // 3
            clon();
            cout << 4 << endl;
            log(); // 4
            temp[length-1] = value;
            clon(false);
            cout << 5 << endl;
            log(); // 5
            delete[] temp;
            temp = nullptr;
        }
    }
    T& operator[](int i){
        try{
            if(i < 0)
                throw 1;
            if(i > length)
                throw 2;
            if(i == 0 && length == 0)
                throw 3;
            return arr[i];
        }
        catch(int a){
            bool nullSize = false;
            namespace c = colors;
            switch(a){
                case 1:
                    c::red("!Індекс не може бути меншим за 0");
                    cout<<endl;
                    break;
                case 2:
                    c::red("!Індекс більший за довжину масиву (");
                    c::yellow(length);
                    c::red(")");
                    cout<<endl;
                    break;
                case 3:
                    c::red("!У масиві нічого немає (");
                    cout<<endl;
                    nullSize = true;
                    break;
            }
            if(!nullSize){
                c::green("Я тобі дам перший елемент масиву:) "); 
                c::yellow(arr[0]);
                cout<<endl;
                return arr[0];
            }
            else{
                c::red("Я не знаю що тобі дати крім виходу з проги");
                cout<<endl;
                assert(!nullSize);
            }
        }
    }
};

int main(){
    system("chcp 65001");
    vector <string> obj;
    obj.pushBack("прикольно");
    obj.pushBack("правда?");
    obj.pushBack("правда?");
    obj.pushBack("правда?");
    cout << obj[0] << " " << obj[1] << endl;
    return 0;
}

The log function works only up to the comment // 3, and then for some reason it does not work.
1
length :1
arr[0]:прикольно

2
length :2
arr[0]:прикольно

3
length :2
arr[0]:прикольно
temp[0]:прикольно

Is there something wrong with the clone function?
And in general: what am I doing wrong?
ps the code was in German and I changed it to normal. And also made changes at the prompt res2001

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-12-15
@Sergomen

With clone, everything is basically fine. Of course, you can add std:: move, but that's not the problem.
The problem is that you have an array arr of size length - 1, and temp - length. And in clon() you are trying to copy length elements from arr, so the array is out of bounds.
In PushBack, increment length only after you call clon the first time, but before the second call.
It's a pity he removed the German, it was funny :-) But it's much more familiar and understandable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question