浅学C++(2)

创建与使用静态库

创建一个解决方案,里面包括两个项
Engine.cpp

#include "Engine.h"

#include <iostream>

namespace engine {
    void PrintMessage() 
    {
        std::cout << "112233" << std::endl;
        std::cin.get();
    };
}

Engine.h

#pragma once

namespace engine {
    void PrintMessage();
}

Application.cpp

#include "Engine.h"
int main()
{
    engine::PrintMessage();
}

配置“配置类型” ,一个lib,一个exe
配置“附加包含目录”

返回多值

采用引用方式

void callTwo(const int& a,const int& b,int& add,int& multipy)
{
    add = a + b;
    multipy = a * b;
}

int main()
{
    int a = 2, b = 3, add, multipy;
    callTwo(a, b, add, multipy);
    std::cout << add << "," << multipy << std::endl;
}

采用结构体

struct TwoEntity
{
    int add, multipy;
};

TwoEntity callByEntity(const int& a, const int& b)
{
    return { a + b,a * b };
}

int main()
{
    int a = 2, b = 3;
    TwoEntity entity = callByEntity(a, b);
    std::cout << entity.add << "," << entity.multipy << std::endl;
}

采用tuple

#include<iostream>
#include<functional>

std::tuple<int,int> callByEntity(const int& a, const int& b)
{
    return std::make_pair( a + b,a * b );
}

int main()
{
    int a = 2, b = 3;
    std::tuple<int, int> souce = callByEntity(a, b);
    std::cout << std::get<0>(souce) << "," << std::get<1>(souce) << std::endl;
}

采用pair

#include<iostream>
#include<functional>

std::pair<int,int> callByEntity(const int& a, const int& b)
{
    return std::make_pair( a + b,a * b );
}

int main()
{
    int a = 2, b = 3;
    std::pair<int, int> souce = callByEntity(a, b);
    std::cout << souce.first << "," << souce.second << std::endl;
}

template模板

#include<iostream>

template<typename T>
void Print(T value)
{
    std::cout << value << std::endl;
}

int main()
{
    Print(5);
    Print("hello");
}

template<typename T,int size>
class Array
{
public:
    int GetSize() const
    {
        return size;
    }
private:
    T array[size];
};

int main()
{
    Array<int, 5> arr1;
    std::cout << arr1.GetSize() << std::endl;
    Array<std::string, 10> arr2;
    std::cout << arr2.GetSize() << std::endl;
}


预处理

#ifdef PR_DEBUG
#define LOG(a) std::cout << a << std::endl;
#else
#define LOG(a) std::cout << "3" << std::endl;
#endif 


int main()
{
	LOG(8);
}

设置两种模式下的预处理信息 PR_DEBUG PR_RELEASE

auto

1

template<typename T>
T TestBack(T& a)
{
	return a;
}

int main()
{
	int a = 1;
	auto a1 = TestBack(a);
	std::cout << a1 << std::endl;
	std::string b = "ZhangSan";
	auto b1 = TestBack(b);
	std::cout << b1 << std::endl;
}

2

template<typename T>
auto TestBack(T& a) -> T
{
	return a;
}

int main()
{
	int a = 1;
	auto a1 = TestBack(a);
	std::cout << a1 << std::endl;
	std::string b = "ZhangSan";
	auto b1 = TestBack(b);
	std::cout << b1 << std::endl;
}

3

int main()
{
	std::vector<std::string> strings;
	strings.push_back("ZhangSan");
	strings.push_back("LiSi");
	for (std::vector<std::string>::iterator it = strings.begin(); it != strings.end(); it++)
	{
		std::cout << *it << std::endl;
	}
	for (auto it = strings.begin(); it != strings.end(); it++)
	{
		std::cout << *it << std::endl;
	}
}

4

class Device
{

};

class DeviceManager
{
private:
	std::unordered_map<std::string, std::vector<Device*>> m_Devices;
public:
	const std::unordered_map<std::string, std::vector<Device*>>& GetDevice()
	{
		return m_Devices;
	}
};



int main()
{
	DeviceManager testDevice;

	const std::unordered_map<std::string, std::vector<Device*>>& a = testDevice.GetDevice();

	using DeviceMap = std::unordered_map<std::string, std::vector<Device*>>;
	const DeviceMap& b = testDevice.GetDevice();

	typedef std::unordered_map<std::string, std::vector<Device*>> DeceviceM;
	const DeceviceM& c = testDevice.GetDevice();

	const auto& d = testDevice.GetDevice();
}

函数指针

void Print(std::string string)
{
	std::cout << string << std::endl;
}

void ForEach(std::vector<std::string>& vec,void(*PO)(std::string))
{
	for (std::string s : vec)
		PO(s);
}

int main()
{
	typedef void(*P)(std::string);
	P fun = Print;
	fun("test");
	std::vector<std::string> arr;
	arr.push_back("ZhangSan");
	arr.push_back("LiSi");
	ForEach(arr, Print);
}

lambde

void Print(std::string string)
{
	std::cout << string << std::endl;
}

void ForEach(std::vector<std::string>& vec,const std::function<void(std::string)>& PO)
{
	for (std::string s : vec)
		PO(s);
}

int main()
{
	std::vector<std::string> arr = { "ZhangSan","LiSi" };

	int a = 1;
	auto lambde = [&](std::string s) mutable {a = 6; std::cout << s << a << std::endl; };
	ForEach(arr, lambde);

	auto index = std::find_if(arr.begin(), arr.end(), [](std::string s) { return s == "ZhangSan"; });
	std::cout << *index << std::endl;
}

线程

#include<thread>
static bool lock = false;
void Print()
{
	using namespace std::literals::chrono_literals;
	while (!lock)
	{
		std::cout << "hello" << std::endl;
		std::this_thread::sleep_for(1s);
	}
}

int main()
{
	std::thread work(Print);
	std::cin.get();
	lock = true;
	work.join();
	std::cin.get();
	return 0;
}

计算时间

#include<chrono>
struct Time
{
	Time()
	{
		start = std::chrono::high_resolution_clock::now();
	};
	~Time()
	{
		end = std::chrono::high_resolution_clock::now();
		duration = end - start;
		std::cout << duration.count() << "s" << std::endl;
	};

	std::chrono::time_point <std::chrono::steady_clock> start, end;
	std::chrono::duration<float> duration;
};


void Print()
{
	Time time;
	for (int i = 0; i < 100; i++) 
	{
		std::cout << "2" << std::endl;
	}
}

int main()
{
	Print();
	std::cin.get();
	return 0;
}

多维数组


int main()
{
	int** arr = new int*[5];
	for (int i = 0; i < 5; i++)
	{
		arr[i] = new int[5];
	}
	int start = 1;
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			arr[i][j] = start++;
		}
	}
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			std::cout << arr[i][j] << "\t";
		}
		std::cout << std::endl;
	}
	std::cin.get();
	return 0;
}

排序

#include<vector>
#include<algorithm>
int main()
{
	std::vector<int> arr = { 5,2,4,1,3 };
	std::sort(arr.begin(), arr.end(), [](int a, int b)
		{
			if (a==1)
			{
				return false;
			}
			if (b == 1)
			{
				return true;
			}
			return a < b;
		});
	for (int i = 0; i < arr.size(); i++)
	{
		std::cout << arr[i] << std::endl;
	}
	
	std::cin.get();
	return 0;
}

类型双关

struct Entity
{
	int x, y;
};

int main()
{
	Entity e = { 2,3 };
	int* b = (int*)&e;
	int y = *(int*)((char*)&e + 4);
	std::cout << b[0] << std::endl;
	std::cout << y << std::endl;
	
	std::cin.get();
	return 0;
}

union

与类型双关类似

struct VectorIner
{
	float x, y;
};

struct VectorUnion
{
	union 
	{
		struct {
			float x, y, w, h;
		};
		struct 
		{
			VectorIner a, b;
		};
	};
};

void Print(const VectorIner& v)
{
	std::cout << v.x << "\t" << v.y << std::endl;
}

int main()
{
	VectorUnion e = { 1.0f,2.1f,3.2f,4.3f };
	Print(e.a);
	Print(e.b);
	e.w = 100.f;
	std::cout << "---------" << std::endl;
	Print(e.a);
	Print(e.b);
	std::cin.get();
	return 0;
}

纯析构函数

class Base
{
public:
	Base()
	{
		std::cout << "base creating" << std::endl;
	};
	virtual ~Base()
	{
		std::cout << "base destorying" << std::endl;
	};

};


class Derived : public Base
{
public:
	Derived()
	{
		arr = new int[2];
		std::cout << "derived creating" << std::endl;
	};
	~Derived()
	{
		delete[] arr;
		std::cout << "derived destorying" << std::endl;
	};

private:
	int* arr;
};

int main()
{
	Base* base = new Base;
	delete base;
	std::cout << "------------" << std::endl;

	Derived* derived = new Derived;
	delete derived;
	std::cout << "------------" << std::endl;

	Base* a = new Derived;
	delete a;
	std::cout << "------------" << std::endl;

	std::cin.get();
	return 0;
}

类型转换

class Base
{
public:
	Base(){};
	virtual ~Base(){};

};

class Derived : public Base
{
public:
	Derived(){};
	~Derived(){};

};

class AnotherDerived : public Base
{
public:
	AnotherDerived() {};
	~AnotherDerived() {};

};

int main()
{
	Derived* derived = new Derived;

	Base* base = derived;

	AnotherDerived* ac = dynamic_cast<AnotherDerived*>(base);
	Derived* bc = dynamic_cast<Derived*>(base);

	std::cin.get();
	return 0;
}
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hi_heibao

谢谢您的鼓励与支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值