CPP使用总结
CPP使用总结
0x01.基本概念
-
std命名空间:C++ 引入了命名空间的概念,计划重新编写库,将类、函数、宏等都统一纳入一个命名空间,这个命名空间的名字就是std。std 是 standard 的缩写,意思是“标准命名空间”
-
线程安全:
0x02.基础函数
0x02.1.virtual关键字使用
定义一个函数为虚函数,不代表函数为不被实现的函数。
定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。
定义一个函数为纯虚函数,才代表函数没有被实现。
定义纯虚函数是为了实现一个接口,起到一个规范的作用,规范继承这个类的程序员必须实现这个函数。
#include <iostream>
using namespace std;
class A{
public:
virtual void Foo() {
cout << "A Foo is called!" << endl;
}
};
class B:public A {
public:
void Foo() {
cout << "B Foo is called!" << endl;
}
};
class C :public A {
public:
void Foo() {
cout << "C Foo is called!" << endl;
}
};
void main(){
A* a = new B();
a->Foo();
return 0;
}
0x02.2.reinterpret_cast
-
顾名思义,就是把内存里的值重新解释。本质上内存里存的都是01位串,至于这些位串是什么意思全看怎么解释。举个例子,32位系统,int是32位,指针也是32位,我既可以把一个32位的值解释成一个整数,也可以解释成一个指针。至于究竟能不能这样解释,由程序员负责。而reinterpret_cast就是干这个事的
0x02.3.std::shared_ptr
- 作用:
a. 自动释放没有指针引用的资源
b. 使用引用计数记录指向改资源的指针数
c. 线程安全
0x02.4.set::reset用法
-
智能指针指向新的对象
\# std::reset用法
#include <iostream>
#include <future>
#include <thread>
using namespace std;
class Person
{
public:
Person(int v) {
value = v;
std::cout << "Cons" <<value<< std::endl;
}
~Person() {
std::cout << "Des" <<value<< std::endl;
}
int value;
};
int main()
{
std::shared_ptr<Person> p1(new Person(1));// Person(1)的引用计数为1
std::shared_ptr<Person> p2 = std::make_shared<Person>(2);
p1.reset(new Person(3));// 首先生成新对象,然后引用计数减1,引用计数为0,故析构Person(1)
// 最后将新对象的指针交给智能指针
std::shared_ptr<Person> p3 = p1;//现在p1和p3同时指向Person(3),Person(3)的引用计数为2
p1.reset();//Person(3)的引用计数为1
p3.reset();//Person(3)的引用计数为0,析构Person(3)
p3.reset();//再reset
return 0;
}
0x02.5.c++中std::stringstream使用
# nclude <iostream\>
# nclude <sstream\>
# nclude <string\>
using namespace std;
int main()
{
stringstream ostr("ccc");
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout<<gstr<<endl;
char a;
ostr>>a;
cout<<a
system("pause");
}
0x02.6.c++中vector的size和capacity区别
vector的size和capacity有什么区别?怎么使capacity值为0?
0x02.7.int与string的转换
#include "stdafx.h"
#include <string>
#include <sstream>
using namespace std;
void main()
{
// int 转 string
stringstream ss;
int n = 123;
string str;
ss<<n;
ss>>str;
// string 转 int
str = "456";
n = atoi(str.c_str());
}
0x02.8.c++中string::find的用法
string s;
string s1;
cin>>s>>s1;
s.find(s1);//返回s1在s中的位置,没找到返回\-1
s.find\_first\_of(s1);//返回任意字符s1在s中第一次出现的位置,s1是字符不可以为字符串
s.find(s1, a);//从s下标为a开始查找字符串s1,返回起始位置,找不到返回\-1
原文链接:https://blog.csdn.net/qq_41444888/article/details/79601846
0x02.9.c++中string::substr用法
1 int main()
2 {
3 string a;
4 string s("123456789");
5
6 a = s.substr(0,5);//拷贝字符串s中从第0位开始的长度为5的字符串
7 cout << a << endl;//输出12345
8
9 a=s.substr(); //不加参数即默认拷贝整个字符串s
10 cout<<a<<endl;//输出123456789
11
12 a=s.substr(4);//输出56789
13 cout<<a<<endl;//单个参数则默认拷贝从第4位开始至末尾的字符串
14 }
原文链接:https://www.cnblogs.com/HOLLAY/p/11324452.html
0x02.10.c++中#define用法
- 参考链接:c++ #ifdef的用法
- 作用:灵活使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码,防止发生重复定义的情况
- 使用示例:
/*在.h文件中定义,在cpp中来进行判断是否定义并执行不同的代码*/
#ifndef DEBUG
#define DEBUG
#endif
而在define.cpp源文件中,代码修改如下:
#include "iostream.h"
#include "head.h"
int main(){
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
0x03.高级使用
0x03.1.c++读写文件
#include <fstream> // ifstream, ifstream::in
using namespace std;
int main(){
// 1. 打开图片文件
// 评论区的 @霍鑫网络 帮忙发现一个隐藏的bug,在此表示感谢,已经修正
// ifstream相关资料:https://blog.csdn.net/sinat_36219858/article/details/80369255
ifstream is("test.jpg", ifstream::in | ios::binary);
// 2. 计算图片长度
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
// 3. 创建内存缓存区
char * buffer = new char[length];
// 4. 读取图片
is.read(buffer, length);
// 到此,图片已经成功的被读取到内存(buffer)中
delete [] buffer;
// 感谢评论区 @geocat 帮忙发现的bug,已经修正
is.close();
return 0;
}
0x03.2.string和char之间的转换
//这一段在cppreference里面在线编译运行的,
//用的VS2017的编译器不支持strcpy,
//而且不支持strcpy_s里面的参数类型为(char *,const char*)
//说没有匹配的重载函数
string str = "hello";
char *p;
p = (char *)malloc((str.length()+1)*sizeof(char));
strcpy(p, str.c_str());
0x03.3.c++中使用多线程
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
using namespace std;
//线程要做的事情就写在这个线程函数中
void GetSumT(vector<int>::iterator first,vector<int>::iterator last,int &result)
{
result = accumulate(first,last,0); //调用C++标准库算法
}
int main() //主线程
{
int result1,result2,result3,result4,result5;
vector<int> largeArrays;
for(int i=0;i<100000000;i++)
{
if(i%2==0)
largeArrays.push_back(i);
else
largeArrays.push_back(-1*i);
}
thread first(GetSumT,largeArrays.begin(),
largeArrays.begin()+20000000,std::ref(result1)); //子线程1
thread second(GetSumT,largeArrays.begin()+20000000,
largeArrays.begin()+40000000,std::ref(result2)); //子线程2
thread third(GetSumT,largeArrays.begin()+40000000,
largeArrays.begin()+60000000,std::ref(result3)); //子线程3
thread fouth(GetSumT,largeArrays.begin()+60000000,
largeArrays.begin()+80000000,std::ref(result4)); //子线程4
thread fifth(GetSumT,largeArrays.begin()+80000000,
largeArrays.end(),std::ref(result5)); //子线程5
first.join(); //主线程要等待子线程执行完毕
second.join();
third.join();
fouth.join();
fifth.join();
int resultSum = result1+result2+result3+result4+result5; //汇总各个子线程的结果
return 0;
}
0x03.4.c++中使用线程池
基于C++11的线程池(threadpool),简洁且可以带任意多的参数
0x03.5.C++中实现延时
#include<iostream>
#include <cstring>
void delay_msec(int msec)
{
clock_t now = clock();
while(clock()-now < msec);
}
Void main(){
std::cout << "sleep:100" << std::endl;
delay_msec(200);
}
0x03.6.Tokenize分割字符串到vector容器
https://blog.csdn.net/weixin_33713503/article/details/93781262
#include <string>
#include <vector>
using std::string;
using std::vector;
int splitStringToVect(const string & srcStr, vector<string> & destVect, const string & strFlag);
int main()
{
string str = "asdasdas \n, sadasd\n, ssdddsrr\n \n \n ss\n";
vector<string> destVect;
splitStringToVect(str, destVect, "\n"); //以"\n"为标记,分割字符串到vector中
return 1;
}
int splitStringToVect(const string & srcStr, vector<string> & destVect, const string & strFlag)
{
int pos = srcStr.find(strFlag, 0);
int startPos = 0;
int splitN = pos;
string lineText(strFlag);
while (pos > -1)
{
lineText = srcStr.substr(startPos, splitN);
startPos = pos + 1;
pos = srcStr.find(strFlag, pos + 1);
splitN = pos - startPos;
destVect.push_back(lineText);
}
lineText = srcStr.substr(startPos, srcStr.length() - startPos);
destVect.push_back(lineText);
return destVect.size();
}
0x03.7.通过/proc/stat计算cpu使用率
http://www.blogjava.net/fjzag/articles/317773.html
1、 采样两个足够短的时间间隔的Cpu快照,分别记作t1,t2,其中t1、t2的结构均为:
(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元组;
2、 计算总的Cpu时间片totalCpuTime
a) 把第一次的所有cpu使用情况求和,得到s1;
b) 把第二次的所有cpu使用情况求和,得到s2;
c) s2 - s1得到这个时间间隔内的所有时间片,即totalCpuTime = j2 - j1 ;
3、计算空闲时间idle
idle对应第四列的数据,用第二次的第四列 - 第一次的第四列即可
idle=第二次的第四列 - 第一次的第四列
6、计算cpu使用率
pcpu =100* (total-idle)/total
0x04.Questions
-
为什么在#include之后还需要使用std?
如果不使用using std::string,就在程序中使用string 类型变量,程序不能识别是标准库中的string 变量。因为程序自定义头文件中也可能含有string变量。所以一定要声明using std::string。这样程序里面的string类型变量就都是std标准库中的string变量了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界