Skip to content

Commit

Permalink
一次提交。
Browse files Browse the repository at this point in the history
  • Loading branch information
Super-LeWH committed Dec 18, 2024
1 parent 4e17ddd commit ca37f9f
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 16 deletions.
Empty file added App/AllInOne.cpp
Empty file.
Empty file added App/AllInOne.h
Empty file.
114 changes: 114 additions & 0 deletions App/BaseType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma

#include "BaseType.h"
#include <algorithm>
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <vector>

using namespace std;

static int globalVar = 0; // 仅在当前文件可见

void BaseType::ShowData() {
Person p1;
p1.age = 89;
// p1.name = "uoadhsvf";
Color colorMe = Blue;
std::cout << p1.name << std::endl;

for (int i = 0; i < 5; i++) {
std::cout << i << "";
}

int i = 0;
while (i < 5) {
std::cout << i << "";
i++;
}

int j = 0;
do {
std::cout << j << "";
j++;
} while (j < 5);

if (p1.age > 100) {
std::cout << "NIU" << std::endl;
}

if (a > 0) {

} else {
}

if (a > 0) {

} else if (a > 1) {

} else if (a > 2) {

} else {
}

switch (colorMe) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
std::cout << "No Result" << std::endl;
}
}

void BaseType::STLType() {
std::vector<int> someNumber = {1, 2, 3, 4, 5};
std::cout << someNumber[0] << std::endl;
someNumber.push_back(10);
for (auto item : someNumber) {
std::cout << item << std::endl;
}

std::deque<int> deq = {1, 2, 3};
deq.push_front(0);
deq.push_back(4);
deq.pop_back();

std::list<int> lst = {1, 2, 3, 4}; // 初始化
lst.push_front(0); // 在前面添加元素
lst.push_back(5); // 在后面添加元素
std::cout << "List elements: ";
for (auto n : lst) {
std::cout << n << " "; // 输出: 0 1 2 3 4 5
}

// std::array<int, 10> arr1 = {1, 2, 3, 4, 5}; // 初始化
std::array<int, 3> a = {1, 2, 3};

std::string str = "srgvwerg";
str += "advsrgb";

std::shared_ptr<int> sharedPtr =
std::make_shared<int>(30); // 创建一个shared_ptr
std::weak_ptr<int> weakPtr =
sharedPtr; // 创建一个 weak_ptr,指向 shared_ptr管理的对象
std::cout << "sharedPtr use_count: " << sharedPtr.use_count() << std::endl;
// 输出:sharedPtr use_count: 1
// weak_ptr 不增加引用计数
/*
为什么需要 lock()?
由于 weak_ptr
本身不拥有对象(它只是观察者,不影响对象的引用计数),不能直接访问对象;通过
lock(),可以安全地检查对象是否仍然有效,并在需要时获取对对象的临时所有权。
*/
if (auto tempPtr = weakPtr.lock()) { // weak_ptr.lock() 返回 shared_ptr
std::cout << "weakPtr is valid: " << *tempPtr
<< std::endl; // 输出:weakPtr is valid : 30
} else {
std::cout << "weakPtr is invalid." << std::endl;
}
}
93 changes: 93 additions & 0 deletions App/BaseType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma

#include <iostream>
#include <string.h>
#include <vector>

using namespace std;

class BaseType {
friend void display(BaseType &obj); // 声明友元函数
private:
/* data */

public:
BaseType(/* args */);
~BaseType();

void ShowData();
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
template <typename T> T add(T a, T b) { return a + b; }
void STLType();

private:
int a;
short b;
long c;
long long d;
unsigned e;
float e1;
double e2;
long double e3;
char c1;
wchar_t c2;
bool yesOrNo;

// 复合数据类型
int arr[5];
struct Person {
/* data */
char *name;
int age;
};
union Data {
/* data */
int i;
float f;
};
enum Color { Red, Green, Blue };

template <typename T> using Vec = std::vector<T>;
};

BaseType::BaseType(/* args */) {
cout << add<int>(3, 5) << endl; // 指定类型为 int,输出:8
cout << add<double>(2.5, 3.8) << endl; // 指定类型为 double,输出:6.3
Vec<int> number = {1, 2, 3, 4, 5};
}

BaseType::~BaseType() {}

void display(BaseType &obj) {
std::cout << obj.Blue << std::endl;

try {
if (obj.a == 0)
throw "Division by zero!";
} catch (const char *msg) {
cout << msg << endl;
}

enum class Color : int { Red = 1, Green = 2, Blue = 4 };
Color myColor = Color::Red;

// 定义 Lambda 表达式
auto sum = [](int a, int b) { return a + b; };
// 调用 Lambda 函数
int result = sum(10, 20);
// 输出结果
cout << "Sum: " << result << endl; // 输出 30

vector<int> nums = {4, 2, 8, 1, 5};
// 使用 Lambda 表达式作为排序条件
// /*std::sort
// * 的第三个参数定义了排序的逻辑,允许你根据具体需求自定义排序规则。如果不指定,则会使用默认的升序排序规则(基于
// * < 运算符)。*/
// std::sort(nums.begin(), nums.end(), [](int a, int b) { return a < b; });
// 输出排序后的结果
for (int num : nums) {
cout << num << " ";
}
// cout << endl; // 输出:1 2 4 5 8
}
6 changes: 4 additions & 2 deletions App/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 为源文件设置变量
SET(SRCS
STLModule.cpp)
AllInOne.cpp
)

# 为头文件设置变量
SET(HEADERS
STLModule.h)
AllInOne.h
)

# 创建共享库
add_library(App ${SRCS})
Expand Down
Empty file added App/ClassType.cpp
Empty file.
32 changes: 32 additions & 0 deletions App/ClassType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma

#include <iostream>
#include <string>

class ClassType {
private:
/* data */
public:
ClassType(/* args */);
~ClassType();

virtual void draw() { std::cout << "Drawing shape" << std::endl; }
};

ClassType::ClassType(/* args */) {}

ClassType::~ClassType() {}

class Circle : public ClassType {
private:
/* data */
void draw() override { std::cout << "Drawing Circle" << std::endl; }

public:
Circle(/* args */);
~Circle();
};

Circle::Circle(/* args */) {}

Circle::~Circle() {}
6 changes: 6 additions & 0 deletions App/ModulesType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <ModulesType.h>

void ModulesType::riskyFunction() {
throw std::runtime_error(
"Something went wrong!"); // 抛出 std::runtime_error类型的异常
}
17 changes: 17 additions & 0 deletions App/ModulesType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma

#include <iostream>
#include <thread>

class ModulesType {
private:
/* data */
public:
ModulesType(/* args */);
~ModulesType();
void riskyFunction();
};

ModulesType::ModulesType(/* args */) {}

ModulesType::~ModulesType() {}
99 changes: 85 additions & 14 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,90 @@
#include <future>
#include <iostream>
#include <thread>

#include "CreateError.h"
#include "STLModule.h"
#include <AllInOne.h>

// #include "BaseType.cpp"
// #include "CreateError.h"
// #include "ModulesType.h"
// #include "STLModule.h"

// void print_hello() { std::cout << "Hello from thread!" << std::endl; }
// // 线程要执行的函数,带有一个整数参数
// void print_square(int x) {
// std::cout << "Square of " << x << " is " << x * x << std::endl;
// }
// // 线程要执行的函数,返回一个整数值
// int calculate_square(int x) { return x * x; }
// // 线程要执行的函数,打印当前线程的编号
// void print_thread_id(int id) {
// std::cout << "Hello from thread " << id << std::endl;
// }
int shard = 0;

void incrment() { shard++; };
void derment() { shard--; };
int main() {
std::cout << "STLModule" << std::endl;
STLModule sTLModule;
sTLModule.InlineAdd();
std::cout << "STLModule" << std::endl;

std::cout << "CreateError" << std::endl;
CreateError createError;
createError._myDenominator = 0;
createError.funcA();
std::cout << "CreateError" << std::endl;

return 0;
std::thread t1(incrment);
std::thread t2(derment);
t1.join();
t2.join();
std::cout << "shard:" << shard << std::endl;
// std::cout << "STLModule" << std::endl;
// STLModule sTLModule;
// sTLModule.InlineAdd();
// std::cout << "STLModule" << std::endl;

// // std::cout << std::endl;
// // std::cout << "CreateError" << std::endl;
// // CreateError createError;
// // createError._myDenominator = 0;
// // createError.funcA();
// // std::cout << "CreateError" << std::endl;

// std::cout << std::endl;
// std::cout << "BaseType" << std::endl;
// BaseType baseType;
// baseType.ShowData();
// std::cout << "BaseType" << std::endl;

// std::cout << std::endl;
// std::cout << "ModulesType" << std::endl;
// ModulesType modulesType;

// try {
// modulesType.riskyFunction();
// } catch (const std::exception &e) {
// std::cout << "Exception caught: " << e.what() << std::endl; //
// 打印异常信息
// }

// // 创建一个线程,执行 print_hello 函数
// std::thread t(print_hello);
// // 等待线程执行完毕
// t.join();
// // 主线程继续执行
// std::cout << "Hello from main!" << std::endl;

// // 创建一个线程,并传递参数 5
// std::thread t12(print_square, 5);
// // 等待线程执行完毕
// t12.join();

// // 使用 std::async 启动异步任务,返回一个 std::future
// std::future<int> result =
// std::async(std::launch::async, calculate_square, 10);
// // 获取结果
// std::cout << "Square of 10 is: " << result.get() << std::endl;

// // 创建多个线程
// std::thread t1(print_thread_id, 1);
// std::thread t2(print_thread_id, 2);
// std::thread t3(print_thread_id, 3);
// // 等待所有线程执行完毕
// t1.join();
// t2.join();
// t3.join();

// std::cout << "ModulesType" << std::endl;
}

0 comments on commit ca37f9f

Please sign in to comment.