创建与使用静态库
创建一个解决方案,里面包括两个项
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;
}