forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set -e | ||
|
||
cmake -B build | ||
cmake --build build | ||
build/cpptest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.