简体   繁体   English

自定义矢量/动态数组 C++ 问题

[英]Custom Vector/Dynamic Array C++ issue

I'm trying to make my own vector in C++ so I could understand more how it works: This is the code:我正在尝试在 C++ 中制作自己的向量,以便我可以更多地了解它是如何工作的:这是代码:

#pragma once

template<typename T>
class Vector {
public:
    Vector() {
        // allocate 2 elements
        ReAlloc(2);
    }

    void pushBack(const T& value) {
        if (m_Size >= m_Capacity) {
            ReAlloc(m_Capacity / 2);
        }
        m_Data[m_Size++] = value;
    }

    const T& operator[](size_t index) const {
        /*
        if (index >= m_Size) {
            // assert
        }
        */
        return m_Data[index];
    }

    T& operator[](size_t index) {
        /*
        if (index >= m_Size) {
            // assert
        }
        */
        return m_Data[index];
    }

    const size_t Size() const { return m_Size; }
    size_t Size() { return m_Size; }
private:
    T* m_Data = nullptr;
    size_t m_Size = 0;
    size_t m_Capacity = 0;
private:
    void ReAlloc(size_t newCapacity) {
        // 1. allocate a new block of memory
        // 2. copy/move old elements into the new block
        // 3. delete the old one

        T* newBlock = new T[newCapacity];
        if (newCapacity < m_Size) {
            m_Size = newCapacity;
        }
        for (size_t i = 0; i < m_Size; i++) {
            newBlock[i] = m_Data[i];
        }
        delete[] m_Data;
        m_Data = newBlock;
        m_Capacity = newCapacity;
    }
};

However, When I try to use it from main.cpp like this:但是,当我尝试像这样从 main.cpp 使用它时:

#include <iostream>
#include <string>

#include "Vector.h"

#define print(x) std::cout << x
#define println(x) std::cout << x << std::endl

template<typename T>
void PrintVector(Vector<T>& vector) {
    for (size_t i = 0; i < vector.Size(); i++) {
        println(vector[i]);
    }
    println("------------------------------------------");
}

int main() {
    Vector<std::string> vector;
    vector.pushBack("Ahmed");
    vector.pushBack("C++");
    vector.pushBack("Vector");

    PrintVector(vector);
}

The code give me this output:代码给我这个 output:

Ahmed
Vector
------------------------------------------
(process 7540) exited with code -1073740940.

it's not even printing C++ and the code acts weird, whenever I try o change anything the output became more mess, Could anyone tell me what did I do wrong please?.: Thx !它甚至没有打印 C++ 并且代码行为很奇怪,每当我尝试更改任何东西时,output 变得更加混乱,谁能告诉我我做错了什么吗?。:谢谢! :) :)

In your pushBack function, when the size is greater than or equal to capacity, you are doing:在您的pushBack function 中,当大小大于或等于容量时,您正在执行以下操作:

ReAlloc(m_Capacity / 2);

which doesn't make sense.这是没有意义的。 If you need additional space to add elements, you should increase the capacity of the underlying array, not decrease it by half.如果你需要额外的空间来添加元素,你应该增加底层数组的容量,而不是减半。

You are probably looking for:您可能正在寻找:

ReAlloc(m_Capacity * 2);

which doubles the underlying capacity.这使基础容量翻了一番。

Here's a working demo , without any segfault.这是一个工作演示,没有任何段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM