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);
}
C++一些例子的更多相关文章
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- REGEX例子
作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...
- CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子
CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...
- 简单例子了解View的事件分发
什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
- kqueue例子
网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...
- 今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子
通过这个案例就知道为什么不要把原生的html放数据库了 常见的几种转码 常用的几种显示方法 只有原生html和最下面一种弹框了,变成了持久xss 如果是Ajax的方式,请用@Ajax.JavaS ...
- ElasticSearch 5学习(5)——第一个例子(很实用)
想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...
- 以实际的WebGIS例子探讨Nginx的简单配置
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 以实际项目中的一个例子来详细讲解Nginx中的一般配置,其中涉 ...
随机推荐
- cronet 的简单学习
官方的解释 "Cronet is the networking stack of Chromium put into a library for use on mobile. This is ...
- win32 - 检查权限
检查当前句柄是否有指定的权限. #include <iostream> #include <windows.h> #include <tchar.h> //#pra ...
- 探秘C语言数组:解锁高效数据管理与多维空间编程技巧"
欢迎大家来到贝蒂大讲堂 养成好习惯,先赞后看哦~ 所属专栏:C语言学习 贝蒂的主页:Betty's blog 引言 前面贝蒂给大家介绍了选择结构与循环结构,今天,贝蒂准备给大家介绍C语言中一个非常重要 ...
- 08-Redis系列之-Redis布隆过滤器,MySQL主从,Django读写分离
Redis实现布隆过滤器 前言 布隆过滤器使用场景 比如有如下几个需求: 原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存 ...
- DataGear数据可视化分析平台介绍
DataGear 是一款开源免费的数据可视化分析平台,自由制作任何您想要的数据看板,支持接入SQL.CSV.Excel.HTTP接口.JSON等多种数据源. 系统特点: 友好的数据源接入 支持运行时接 ...
- 【Azure Redis 缓存】应用中出现连接Redis服务错误(production.ERROR: Connection refused)的排查步骤
问题描述 在PHP应用中,连接Redis的方法报错 RedisException(code: 0): Connection refused at /data/Redis/Connectors/Php ...
- evalFn 字符串转执行函数 附带JSONParse函数
const evalFn = (fn) => { var Fun = Function // 一个变量指向Function,防止前端编译工具报错 return new Fun('return ' ...
- C++实现一个简单的生产者-消费者队列
本文的代码都是ChatGPT生成,我只是做了微小的调整和整合,AI提示词如下: 设计一个C++类,支持生产者-消费者模型,可以通过size函数获取剩余数量 可能第一次生成的不一定合适,多刷新几次. 生 ...
- 适合新手练习的python开源经典源码
一 前记 python学习离不开三要素: A.基础知识要熟悉 B. 练习源码要够量 C.实战项目要跟上 二 链接 书本的基础就看几本书就可以了,这里给出源码练习的经典开源源码: 该链接集合了pytho ...
- Educational Codeforces Round 141:C. Yet Another Tournament
一.来源:Problem - C - Codeforces 二.题面 三.思路 读题: 其他人的胜场由位次决定,对于第i位,其胜场为i-1 人数为\(5·10^5\),不是5(看错了) 每个人和自己比 ...