Skip to content

Commit

Permalink
add EXP
Browse files Browse the repository at this point in the history
  • Loading branch information
DiaosWang committed Aug 28, 2024
1 parent ef124fc commit 792546d
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 2 deletions.
17 changes: 16 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"ctime": "cpp",
"cstring": "cpp"
"cstring": "cpp",
"bit": "cpp",
"compare": "cpp",
"concepts": "cpp",
"ios": "cpp",
"iterator": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
},
"cmake.configureOnOpen": false
}
17 changes: 17 additions & 0 deletions EXP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_COMPILER "g++")#设置C++编译器
set(CMAKE_CXX_FLAGS "-g -Wall")

set(CMAKE_CXX_STANDARD 17) # 使用C++17标准

PROJECT (proj CXX)
ADD_EXECUTABLE(main main.cpp)
ADD_EXECUTABLE(main2 main2.cpp)
ADD_EXECUTABLE(main3 main3.cpp)
ADD_EXECUTABLE(main4 main4.cpp)
ADD_EXECUTABLE(main5 main5.cpp)
ADD_EXECUTABLE(main6 main6.cpp)
ADD_EXECUTABLE(main8 main8.cpp)
ADD_EXECUTABLE(main9 main9.cpp)
ADD_EXECUTABLE(main10 main10.cpp)
ADD_EXECUTABLE(main11 main11.cpp)
15 changes: 15 additions & 0 deletions EXP/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<iostream>

int main(){
int a[2][3] = {{1,2,3},{4,5,6}};
int (*p)[3] = a;
std::cout << *(*(a+1)+2) << std::endl;
std::cout << *(*(p+1)+2) << std::endl;
std::cout << p[1][2] << std::endl;
std::cout << typeid(a).name() << std::endl;
std::cout << typeid(&a).name() << std::endl;
std::cout << typeid(&p).name() << std::endl;

int *pp = a[0];
std::cout <<typeid(a[0]).name();
}
23 changes: 23 additions & 0 deletions EXP/main10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>

class Base{
public:
int a = 1;
void show(int a = 111){ this-> a = a ; std::cout <<"show function of Class Base, a = " << a << std::endl;}
};

class Son : public Base{
public:
int a = 4;
void show(int a = 444 ){ this-> a = a; std::cout <<"show function of Class Son, a = " << a << std::endl; this->Base::show();}
};
/* 输出
show function of Class Son, a = 444
show function of Class Base, a = 111
*/
int main(){
Son* sn = new Son();
sn->show();
return 0;
}

25 changes: 25 additions & 0 deletions EXP/main11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>

class Base{
public:
int a = 1;
void show(){ std::cout << "non-param show, a = " << this->a << std::endl; }
};

class Son : public Base{
public:
int a = 4;
};
/* 输出
non-param show, a = 1
non-param show, a = 1
*/
int main(){
Son* sn = new Son();
sn->show();
sn->Base::show();

delete sn;
return 0;
}

33 changes: 33 additions & 0 deletions EXP/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <memory>
using namespace std;
class XData
{
public:
XData()
{
cout << "Create XData" << endl;
}
~XData()
{
cout << "Drop XData" << endl;
}
};
int main(int argc,char *argv[])
{
auto sp3 = make_shared<XData>();
cout << "sp3.use_count() = " << sp3.use_count() << endl;
cout << "before sp3.reset()" << endl;
sp3.reset();
cout << "after sp3.reset()" << endl;
cout << "sp3.reset() sp3.use_count() = " << sp3.use_count() << endl;
sp3 = nullptr;
cout << "after sp3 = nullptr" << endl;
cout << "sp3.reset() sp3.use_count() = " << sp3.use_count() << endl;

auto sp4 = make_shared<XData>();
sp3 = sp4;
cout << "after sp3 = sp4, sp3.use_count() = " << sp3.use_count() << endl;

return 0;
}
12 changes: 12 additions & 0 deletions EXP/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<iostream>

template<class _Ty, size_t Size>
void TestMemArr(_Ty(&data)[Size])
{
std::cout << "sizeof data : " << std::endl;
}

int main(){

return 0;
}
16 changes: 16 additions & 0 deletions EXP/main4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<iostream>
#include<string>

int main(){
std::string str1;
str1.resize(1024);
std::cout << str1 << std::endl;
for(char a : str1){
std::cout << static_cast<int>(a)<< " ";
}
std::cout << std::endl;

int a;
std::cout << a << std::endl;

}
26 changes: 26 additions & 0 deletions EXP/main5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>

// 模板函数,接受一个数组并遍历其元素
template<size_t Size>
void fun(int (&arr)[Size]) {
for (auto i : arr) {
std::cout << i << " "; // 输出数组中的元素
}
std::cout << std::endl; // 换行
}

// 模版函数,更加通用
template<typename Ty, size_t Size>
void fun2(Ty (&arr)[Size]) {
for (auto i : arr) {
std::cout << i << " "; // 输出数组中的元素
}
std::cout << std::endl; // 换行
}

int main() {
int a[4] = {1, 2}; // 数组 a,部分初始化,未初始化的元素将被默认初始化为 0
fun<4>(a); // 调用模板函数 fun,并传递数组 a (注意: 之所以显示调用模版,是为何防止编译器将其视为模版变量)
fun2(a);
return 0;
}
42 changes: 42 additions & 0 deletions EXP/main6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <cstdlib>
#include <cstring>

// 全局变量来跟踪分配的总内�?
size_t allocated_memory = 0;

void* operator new[](std::size_t size) {
// 分配内存并记录大�?
allocated_memory = size;
void* ptr = std::malloc(size);
std::cout << "operator new[]: 分配 " << size << " 字节内存,地址: " << ptr << std::endl;
return ptr;
}

void operator delete[](void* ptr) noexcept {
// 输出即将释放的内存信�?
std::cout << "operator delete[]: 释放地址: " << ptr << std::endl;

// 模拟修改内存的内�?
if (ptr != nullptr) {
std::memset(ptr, 0xAA, allocated_memory); // �?0xAA填充内存
}

// 释放内存
std::free(ptr);
}

int main() {
// 分配一个int数组
int* arr = new int[1024];
std::cout << "数组地址: " << arr << std::endl;

// 删除数组
delete[] arr;

// 尝试访问数组的元祖
std::cout << "尝试访问已释放的内存: " << arr[0] << std::endl; // 访问释放后的内存(未定义行为)
std::cout << "你好"<<std::endl;

return 0;
}
29 changes: 29 additions & 0 deletions EXP/main7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <algorithm> // For std::begin and std::end
// #include <cstring> // For memcpy

class XData {
int a;
float b;
double c;
// Your class definition
};

int main(int argc, char* argv[]) {
XData datas[3];
// unsigned char buf[1024] = { 0 };

// std::cout << "==================memcpy==================" << std::endl;
// memcpy(buf, &datas, sizeof(datas));

// Printing the addresses
std::cout << "Address of datas: " << static_cast<void*>(&datas) << std::endl;
std::cout << "Address from std::begin(datas): " << static_cast<void*>(std::begin(datas)) << std::endl;

std::cout << typeid(datas).name() << std::endl;
std::cout << typeid(datas).name() << std::endl;
// std::copy(std::begin(datas), std::end(datas), buf);

// std::cout << "cppds.com" << std::endl;
return 0;
}
25 changes: 25 additions & 0 deletions EXP/main8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>

class TestMem{
public:
TestMem(){
std::cout << "this is TestMem constructor" << std::endl;
}
TestMem(int a ){
std::cout << "construct the TestMem by "<< a << std::endl;
}
TestMem(int a, int b ){
std::cout << "construct the TestMem by "<< a << ", " << b << std::endl;
}
~TestMem(){
std::cout << "thi is TestMem destroy" << std::endl;
}
};

int main(){
TestMem* tm = new TestMem[3]{{2,3},2,3};
TestMem ta[2] = {};
TestMem tb[2]{};

return 0;
}
31 changes: 31 additions & 0 deletions EXP/main9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>

class Base{
public:
int a = 1;
int b = 2;
Base(int a, int b): a(a), b(b){}
Base(){
std::cout << "this is no params of Base()" <<std::endl;
}

};

class Son : public Base{
public:
int a = 44;
int k = 12;
Son(int a, int k): a(a), k(k){
std::cout << "bagin Son(int a, int k)" << std::endl;
}
};

int main(){
Base* ba = new Base(111,222);
std::cout << ba->a << " " << ba->b << std::endl;
Son* sn = new Son(333,444);
std::cout << sn->a << " " << sn->k << " " << sn->b << std::endl;
Base* ba1 = new Base();
std::cout << ba1->a << " " << ba1->b << std::endl;
return 0;
}
1 change: 1 addition & 0 deletions Link/Link4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ project(CheckLinkage)
set(CMAKE_CXX_STANDARD 17)

add_executable(main main.cpp)
add_executable(main6 main6.cpp)
2 changes: 1 addition & 1 deletion Link/Link4/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template class TypeClass<int>; // 实例化声明,会实例化模板
TypeClass<int> tc;

template<typename T>
static T TypeClass<T>::staticKK; // 泛型定义
T TypeClass<T>::staticKK; // 泛型定义

template<> int TypeClass<int>::staticKK = 10;

Expand Down
31 changes: 31 additions & 0 deletions Link/Link4/main6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Tool::fun()::a 和fun2::a 不冲突,都是局部变量
#include <iostream>

class Tool{
public:
void fun(){
static int a = 12;
a += 1;
std::cout << a << std::endl;
}
static void fun3(){
static int a = 22;
a += 1;
}
};

void fun2(){
static int a = 24;
a+=1;
}

int main(){
Tool tool, tool2;
tool.fun();
tool.fun();
tool.fun();
tool2.fun();
tool2.fun();
tool.fun3();
return 0;
}
8 changes: 8 additions & 0 deletions STL/functor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_COMPILER "g++")#设置C++编译器
set(CMAKE_CXX_FLAGS "-g -Wall")

set(CMAKE_CXX_STANDARD 17) # 使用C++17标准

PROJECT (proj CXX)
ADD_EXECUTABLE(main main.cpp)
Loading

0 comments on commit 792546d

Please sign in to comment.