目录
future&std::async
https://www.cnblogs.com/chengyuanchun/p/5394843.html
https://www.cnblogs.com/haippy/p/3280643.html
对象初始化
Person p=new Person(12);
在c++ 中有两种方式
Person p1(12);
Person *p2=new Person(12);
p1在方法结束后,就会消亡
p2在方法结束后,不会消亡,得手动删除
对p2的方法调用可以 p2->run();或者 (*p2).run();
数组与指针
将数组交给指针
int a[5];
int *p = &a[0];
int *k = a;
二维数数组
int b[3][4];
int *p;
p=b[0];
指针数组与数组指针
https://www.cnblogs.com/hongcha717/archive/2010/10/24/1859780.html
字符串转化为数字 数字转化为字符串
char str[10];
int a=1234321;
sprintf(str,"%d",a);
--------------------
char str[10];
double a=123.321;
sprintf(str,"%.3lf",a);
char str[]="1234321";
int a;
sscanf(str,"%d",&a);
.............
char str[]="123.321";
double a;
sscanf(str,"%lf",&a);
//字符串转数字
// 同样, 可以使用 stol(long), stof(float), stod(double) 等.
std::string str;
int i = std::stoi(str);
...
//数字转字符串
std::string str2 = std::to_string(15);
字符数组 与string 相互转化
char ch [] = "ABCDEFG";
string str(ch);//也可string str = ch;
或者
char ch [] = "ABCDEFG";
string str;
str = ch;//在原有基础上添加可以用str += ch;
char buf[10];
string str("ABCDEFG");
length = str.copy(buf, 9);
buf[length] = '\0';
或者
char buf[10];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);
锁
cmake
cmake .
上面这个命令就是在当前文件目录下查找CMakeList.txt 进行项目构建,然后把构建后的文件就放在当前目录(这里面就包括了Mikefile)
cmake -S /home/opc/toy_c/gtest_demo -B /home/opc/toy_c/gtest_demo/myBuild
上面这个命令的语义就是在/home/opc/toy_c/gtest_demo目录下查找CMakeList.txt,然后把构建后的文件放到/home/opc/toy_c/gtest_demo/myBuild
cmake --build ./myBuild
就是在当前目录的myBuild下面,执行 Mikefile文件
那么cmake --build ./myBuild 和直接命令行敲make有什么区别?
主要是为了跨平台,使用前者后,不管你是使用的什么生成器,CMake 都能正确构建,否则如果使用的是 Ninja 或者其他生成器,那 make 就不生效了。
执行完cmake --build ./myBuild之后在myBuild文件夹里就可以看到可执行的二进制文件了。
cmake --build ./myBuild --target myPro -j 10
上面这个命令和cmake --build ./myBuild相比多个一个target,就是单独构建指定的target就好。然后目录下也就只只会生产指定的二进制文件。
更多知识请参见:
线程
#include <iostream>
#include <thread>
using namespace std;
void output(int i)
{
cout << i << endl;
}
int main()
{
for (uint8_t i = 0; i < 4; i++)
{
thread t(output, i);
t.detach();
}
getchar();
return 0;
}
线程的创建有多种方式
指定某个方法
就是上面例子里面的:thread t(output, i);
使用lambda表达式
for (int i = 0; i < 4; i++)
{
thread t([i]{
cout << i << endl;
});
t.detach();
}
重载了()操作符的类实例
class Task
{
public:
void operator()(int i)
{
cout << i << endl;
}
};
int main()
{
for (uint8_t i = 0; i < 4; i++)
{
Task task;
thread t(task, i);
t.detach();
}
}
直接传入一个类的成员函数
#include <iostream>
#include <thread>
class X
{
public:
void do_work()
{
std::cout << "Hello World!" << std::endl;
}
};
int main(int argc, char const *argv[])
{
X my_x;
std::thread t(&X::do_work, &my_x);
t.join();
return 0;
}
线程的参考资料
lambda
函数指针与指针函数
指针函数
看下面的代码
#include "iostream"
int test() {
int b = 10;
std::cout << "b address " << &b << std::endl;
return b;
}
int *test2(int x) {
int a = 17;
int c = 15;
std::cout << "c2 point address " << &c << std::endl;
return &c;
}
int main() {
int m = test();
std::cout << "m address " << &m << std::endl;
int* m2 = test2(13);
std::cout << "m2 point address " << m2 << std::endl;
return 0;
}
看c2和m2的值是一样的,b和m的值不一样
其实我好奇的是为什么b和c2的值也一样。
说白了,指针函数就是一个普通的函数,只是它的返回值是一个指针罢了。
函数指针
很烦c++里面很多知识就是名字打个颠倒。
让人记忆起来很麻烦。
函数指针说白了就是一个指针,但是它指向的并不是我们熟悉的常用基本类型,或者一个自定义person,student类型,而是一个函数
#include "iostream"
//返回两个数中较大的一个
int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
int (*myPoint)(int, int); //定义函数指针
typedef int (*otherMyPoint)(int, int);//定义了一个类型pFunc_t
int main() {
myPoint = max;
int x = 5;
int y = 7;
int maxval1, maxval2;
int minval1, minval2;
maxval1 = (*myPoint)(x, y);
printf("Max value 1: %d\n", maxval1);
maxval2 = myPoint(x, y);
printf("Max value 2: %d\n", maxval2);
myPoint = min;
maxval1 = (*myPoint)(x, y);
printf("min value 1: %d\n", maxval1);
maxval2 = myPoint(x, y);
printf("min value 2: %d\n", maxval2);
otherMyPoint o1 = max;
otherMyPoint o2 = max;
maxval1 = o2(x, y);
printf("otherMyPoint value 1: %d\n", maxval1);
maxval1 = o2(x, y);
printf("otherMyPoint value 1: %d\n", maxval1);
return 0;
}
我第一次看到看到下面两行代码的时候,还是有点疑惑,不明白区别在哪
int (*myPoint)(int, int); //定义函数指针
typedef int (*otherMyPoint)(int, int);//定义了一个类型pFunc_t
但是自己写两行代码,就明白了。
myPoint是一个变量名,就类似我们代码里面写的int 下面的a,b,c。
但是otherMyPoint是变量类型,类似int,doulbe,student,person等等。
otherMyPoint o1 = max;
otherMyPoint o2 = max;
看这段代码,你就应该懂了otherMyPoint是变量类型的意思了
c++ 里面那些麻烦的声明格式
c++方法模板
#include <iostream>
using namespace std;
template<typename T>//模板头,template关键字告诉编译器开始泛型编程
T add(T t1, T t2)//类型参数化为T
{
return t1 + t2;
}
int main()
{
cout << add(12, 34) << endl;
cout << add(12.2,45.6) << endl;
return 0;
}
实例化
分隐式类型转换与显式类型转换。
上面看到的直接调研的就是隐式转换
显式实例化语法格式:
template 函数返回值类型 函数名<实例化的类型>(参数列表);
注意:这是声明语句,要以分号结束,<>中是显式实例的数据类型,即要实例 化出一个什么类型的函数。如:显示实例化为int,则在调用时,不是int 类 型的数据会 转换为int类型进行计算。
看代码
#include <iostream>
using namespace std;
template<typename T>
T add(T t1, T t2)
{
return t1 + t2;
}
template int add<int>(int t1, int t2);//显示实例化为int类型
int main()
{
cout << add<int>(12, 'A') << endl;//函数模板调用:A-65
cout << add(1.4, 5.7) << endl;//隐式实例化:自动实参推演
cout << add<int>(23.4, 44.2) << endl;//显示声明可省,结果为67
cout << add(23.4, 44.2) << endl;
return 0;
}
结果如下:
77
7.1
67
67.6
其实我是没有懂使用显式实例化的好处是啥。
类模板
语法
template <class T>
class 类模板名
{
类的定义;
};
template <class T>
class MyArray {
private:
T* array;
int size;
public:
MyArray(int size) {
this->size = size;
array = new T[size];
}
~MyArray() {
delete[] array;
}
void set(int index, T value) {
array[index] = value;
}
T get(int index) {
return array[index];
}
};
function
共享内存
c++ 共享内存实现
https://blog.youkuaiyun.com/weixin_43055404/article/details/107936481
有界阻塞队列
https://www.cnblogs.com/loskyer/p/9541299.html
线程池
C++11并发学习之六:线程池的实现_// the constructor just launches some amount of wo-优快云博客
文件的读取与写入
二进制 C++文件操作——读写文件_c++文件读写操作-优快云博客
txt格式 C++对txt文件的写入读取操作_c++读取txt-优快云博客
TCP 通信
下面的是阻塞
https://zhuanlan.zhihu.com/p/360042334
下面的是select poll 和 epoll
https://zhuanlan.zhihu.com/p/452562548
状态机
ceph peering机制-状态机_51CTO博客_ceph peering
ceph peering机制 | Ivanzz
参考资料
http://blog.youkuaiyun.com/wuwenxiang91322/article/details/17038557
http://www.cnblogs.com/xudong-bupt/p/3509567.html
http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html
C++中字符数组与string的相互转换 - Boblim - 博客园
C++中数字与字符串之间的转换 - 阿凡卢 - 博客园