Skip to content

Instantly share code, notes, and snippets.

@lucidfrontier45
Last active September 25, 2021 20:51
Show Gist options
  • Select an option

  • Save lucidfrontier45/ebd8ceacb17a92e0461c2deb09b3ffd6 to your computer and use it in GitHub Desktop.

Select an option

Save lucidfrontier45/ebd8ceacb17a92e0461c2deb09b3ffd6 to your computer and use it in GitHub Desktop.

Revisions

  1. lucidfrontier45 revised this gist Sep 25, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.cpp
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ struct MyVector {
    }
    }

    MyVector(MyVector&& other)
    MyVector(MyVector&& other) noexcept
    {
    std::cout << "move constructor" << std::endl;
    capacity = other.capacity;
  2. lucidfrontier45 revised this gist Sep 25, 2021. No changes.
  3. lucidfrontier45 revised this gist Sep 25, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion test.cpp
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,6 @@ struct MyVector {
    void push_back(double x)
    {
    if (capacity == 0) {
    capacity = 0;
    data = new double[capacity];
    } else if (current_size == capacity) {
    capacity = capacity * 2;
  4. lucidfrontier45 renamed this gist Sep 25, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. lucidfrontier45 created this gist Sep 25, 2021.
    86 changes: 86 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    #include <iostream>

    struct MyVector {

    size_t current_size = 0;
    size_t capacity = 0;
    double* data = nullptr;

    MyVector() : capacity { 10 }, current_size { 0 }
    {
    std::cout << "default constructor" << std::endl;
    data = new double[capacity];
    }

    MyVector(const MyVector& other)
    {
    std::cout << "copy constructor" << std::endl;
    capacity = other.capacity;
    current_size = other.current_size;
    data = new double[capacity];
    for (size_t i = 0; i < capacity; i++) {
    data[i] = other.data[i];
    }
    }

    MyVector(MyVector&& other)
    {
    std::cout << "move constructor" << std::endl;
    capacity = other.capacity;
    current_size = other.current_size;
    data = other.data;

    other.capacity = 0;
    other.current_size = 0;
    other.data = nullptr;
    }

    ~MyVector()
    {
    delete[] data;
    data = nullptr;
    }

    double operator[](size_t i)
    {
    return data[i];
    }

    void push_back(double x)
    {
    if (capacity == 0) {
    capacity = 0;
    data = new double[capacity];
    } else if (current_size == capacity) {
    capacity = capacity * 2;
    auto new_data = new double[capacity];
    for (size_t i = 0; i < current_size; i++) {
    new_data[i] = data[i];
    }
    delete[] data;
    data = new_data;
    }

    data[current_size + 1] = x;
    current_size += 1;
    }
    };

    int main()
    {
    MyVector v1;
    v1.push_back(1);
    v1.push_back(2);

    MyVector v2 { v1 };
    std::cout << "v1.current_size = " << v1.current_size << " data addr = " << v1.data
    << std::endl;
    std::cout << "v2.current_size = " << v2.current_size << " data addr = " << v2.data
    << std::endl;

    MyVector v3 { std::move(v1) };
    std::cout << "v1.current_size = " << v1.current_size << " data addr = " << v1.data
    << std::endl;
    std::cout << "v3.current_size = " << v3.current_size << " data addr = " << v3.data
    << std::endl;
    }