Skip to content

Commit

Permalink
add zonejie
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Dec 12, 2021
1 parent 53c8e1c commit 5233c81
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 19 deletions.
38 changes: 19 additions & 19 deletions 02/10/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@
#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;
v1 = v2; // 拷贝赋值 O(n)

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

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;
v1 = std::move(v2); // 移动赋值 O(1)

std::cout << "after move:" << std::endl;
std::cout << "v1 length " << v1.size() << std::endl; // 200
std::cout << "v2 length " << v2.size() << std::endl; // 0
}

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

v2 = std::move(v1);
std::swap(v1, v2); // 交换两者 O(1)

std::cout << "after:" << std::endl;
std::cout << "v1.size() is " << v1.size() << std::endl;
std::cout << "v2.size() is " << v2.size() << std::endl;
std::cout << "after swap:" << std::endl;
std::cout << "v1 length " << v1.size() << std::endl; // 200
std::cout << "v2 length " << v2.size() << std::endl; // 10
}

int main() {
test_copy();
test_move();
test_swap();
return 0;
}
5 changes: 5 additions & 0 deletions 02/11/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)
65 changes: 65 additions & 0 deletions 02/11/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#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;
}

Vector(Vector &&other) {
m_size = other.m_size;
other.m_size = 0;
m_data = other.m_data;
other.m_data = nullptr;
}

Vector &operator=(Vector &&other) {
this->~Vector();
new (this) Vector(std::move(other));
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 = std::move(v1);

return 0;
}
5 changes: 5 additions & 0 deletions 02/11/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
9 changes: 9 additions & 0 deletions 02/11/zongjie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
C c1 = c2; // 拷贝构造函数
C c1 = std::move(c2); // 移动构造函数

c1 = c2; // 拷贝赋值函数
c1 = std::move(c2); // 移动赋值函数

C c1 = C(); // 移动构造函数
c1 = C(); // 移动赋值函数
return c2; // 移动赋值函数
Binary file modified 02/slides.pptx
Binary file not shown.

0 comments on commit 5233c81

Please sign in to comment.