Skip to content

Commit

Permalink
100
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Dec 12, 2021
1 parent 2664df2 commit 53c8e1c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 02/09/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 17)
project(hellocpp LANGUAGES CXX)

add_executable(cpptest main.cpp)
52 changes: 52 additions & 0 deletions 02/09/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <cstdlib>
#include <iostream>
#include <cstring>

struct Vector {
size_t m_size;
int *m_data;

Vector(size_t n) {
m_size = n;
m_data = (int *)malloc(n * sizeof(int));
}

~Vector() {
free(m_data);
}

Vector(Vector const &other) {
m_size = other.m_size;
m_data = (int *)malloc(m_size * sizeof(int));
memcpy(m_data, other.m_data, m_size * sizeof(int));
}

Vector &operator=(Vector const &other) {
m_size = other.m_size;
m_data = (int *)realloc(m_data, m_size * sizeof(int));
memcpy(m_data, other.m_data, m_size * sizeof(int));
return *this;
}

size_t size() {
return m_size;
}

void resize(size_t size) {
m_size = size;
m_data = (int *)realloc(m_data, m_size);
}

int &operator[](size_t index) {
return m_data[index];
}
};

int main() {
Vector v1(32);
Vector v2(64);

v2 = v1;

return 0;
}
5 changes: 5 additions & 0 deletions 02/09/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set -e

cmake -B build
cmake --build build
build/cpptest
5 changes: 5 additions & 0 deletions 02/10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 17)
project(hellocpp LANGUAGES CXX)

add_executable(cpptest main.cpp)
42 changes: 42 additions & 0 deletions 02/10/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>

void test_copy() {
std::cout << std::endl << "test copy:" << std::endl;

std::vector<int> v1(10);
std::vector<int> v2(200);

std::cout << "before:" << std::endl;
std::cout << "v1.size() is " << v1.size() << std::endl;
std::cout << "v2.size() is " << v2.size() << std::endl;

v2 = v1;

std::cout << "after:" << std::endl;
std::cout << "v1.size() is " << v1.size() << std::endl;
std::cout << "v2.size() is " << v2.size() << std::endl;
}

void test_move() {
std::cout << std::endl << "test move:" << std::endl;

std::vector<int> v1(10);
std::vector<int> v2(200);

std::cout << "before:" << std::endl;
std::cout << "v1.size() is " << v1.size() << std::endl;
std::cout << "v2.size() is " << v2.size() << std::endl;

v2 = std::move(v1);

std::cout << "after:" << std::endl;
std::cout << "v1.size() is " << v1.size() << std::endl;
std::cout << "v2.size() is " << v2.size() << std::endl;
}

int main() {
test_copy();
test_move();
return 0;
}
5 changes: 5 additions & 0 deletions 02/10/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set -e

cmake -B build
cmake --build build
build/cpptest
Binary file modified 02/slides.pptx
Binary file not shown.

0 comments on commit 53c8e1c

Please sign in to comment.