C++一些例子
作者:互联网
虚析构
#include<iostream>
class Base
{
public:
Base() { std::cout << "base 构造" << std::endl; }
virtual~Base() { std::cout << "base 析构" << std::endl; }
};
class Derived : public Base
{
public:
Derived() { std::cout << "derived 构造" << std::endl; }
~Derived() { std::cout << "derived 析构" << std::endl; }
};
int main()
{
Base* base = new Base();
delete base;
std::cout << "--------------" << std::endl;
Derived* der = new Derived();
delete der;
std::cout << "--------------" << std::endl;
Base* p = new Derived();
delete p;
std::cin.get();
}
联合体
#include<iostream>
struct Vector1
{
float x, y;
};
struct Vector2
{
union
{
struct
{
float x, y, z, w;
};
struct
{
Vector1 a, b;
};
};
};
void PrintVector(const Vector1& vector)
{
std::cout << vector.x << " " << vector.y << std::endl;
}
int main()
{
Vector2 vector = { 1.0f,2.0f,3.0f,4.0f };
PrintVector(vector.a);
PrintVector(vector.b);
// 1 2 3 4
vector.z = 100.0f;
PrintVector(vector.a);
PrintVector(vector.b);
// 1 2 100 4
std::cin.get();
}
线程
#include<iostream>
#include<thread>
static bool Finished = false;
void DoWorker()
{
using namespace std::literals::chrono_literals;
std::cout << "id: " << std::this_thread::get_id() << std::endl;
while (!Finished)
{
std::cout << "Working...." << std::endl;
std::this_thread::sleep_for(1s);
//std::cin.get()
}
}
int main()
{
std::thread worker(DoWorker);
std::cin.get();
Finished = true;
worker.join();
std::cin.get();
}
预处理
#if _DEBUG==1
#define LOG(x) cout<<x<<endl
#elif(_RELESE)
#define LOG(x)
#endif
重载操作符
class Vector1
{
public:
float x, y;
public:
Vector1(float X, float Y)
: x(X), y(Y)
{
Vector1* const &e = this;
}
Vector1 Add(const Vector1& other)const
{
return Vector1(x + other.x, y + other.y);
}
Vector1 operator+(const Vector1& other)const
{
return Add(other);
}
Vector1 Multiply(const Vector1& other)const
{
return Vector1(x * other.x, y * other.y);
}
Vector1 operator*(const Vector1& other)const
{
return Multiply(other);
}
bool operator==(const Vector1& other)const
{
return x == other.x && y == other.y;
}
bool operator!=(const Vector1& other)const
{
return !(*this == other);
}
};
std::ostream& operator<<(ostream& stream, const Vector1& other)
{
stream << other.x << "," << other.y;
return stream;
}
int main()
{
Vector1 v1(1.1f, 2.2f);
Vector1 v2(3.3f, 4.4f);
Vector1 result1 = v1 + v2;
Vector1 result2 = v1 * v2;
cout << result1 << endl;
cout << result2 << endl;
if (result1 != result2)
cout << "==" << endl;
else
cout << "!=" << endl;
cin.get();
}
智能指针
class Entity
{
};
{
shared_ptr<Entity>weakEneity;
{
unique_ptr<Entity>entity = make_unique<Entity>();
shared_ptr<Entity>sharedEntity = make_shared<Entity>();
weakEneity = sharedEntity;
entity->print();
}
}
lambda
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void Foreach(const vector<int>& v,const function<void(int)>&func)
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
auto it = find_if(v.begin(), v.end(), [](int val){ return val > 2; });
cout << *it << endl;
auto lambda = [=](int val) {cout << val << endl; };
Foreach(v, lambda);
cin.get();
}
函数指针
void Println(int val)
{
cout << val << endl;
}
void Foreach(const vector<int>& v,void(*func)(int val))
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
Foreach(v, Println);
}
void Foreach(const vector<int>& v,void(*func)(int val))
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
Foreach(v, [](int val)
{
cout << val << endl;
});
}
使用箭头运算符,来获取内存中某个值的偏移量
#include<iostream>
using namespace std;
struct Vector
{
int x, y, z;
};
int main()
{
int offset = (int)&((Vector*)nullptr)->x;
//x,y,z 执行结果分别是0,4,8
cout << offset << endl;
}
在不知道数组大小传递
template<int N>
void Array(const array<int,N>& arr)
{
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i];
}
}
int main()
{
array<int, 10>arr = { 1,1,1,1,1 };
Array(arr);
}
标签:const,cout,int,Vector1,C++,例子,一些,include,other 来源: https://www.cnblogs.com/chengmf/p/15055222.html