下面只讲干货,方便C语言初学者快速上手cpp来刷算法题,如果是希望系统学习cpp的不建议继续阅读,仅限于快速使用
 
文章目录
using namespace std
#include<iostream>
using namespace std;
int main(){
   
	int n;
	cin >> n;
	cout << "violentayang" << n <<endl;
	return 0;
}

iostream 是 input out stream 标准输入输出流 相当于C语言中的stdio.h 是个必备的头文件
using namespace std 这是使用std这个命名空间
cin cout 全部在std这个名称空间
cin 类似于 scanf 输入语句
cout 就是输出 多个输出就用<<隔开
endl 是end of line就是换行\n的意思

如果不用命名空间

需要对代码进行修改
#include<iostream>
int main(){
   
	int n;
	std::cin >> n;
	std::cout << "violentayang" << n<<std::endl;
	return 0;
}
cin cout 头文件
cin cout 运算速度不如scanf printf
头文件
#include<iostream>
#include<cstring>
#include<cmath>
int main(){
   
	int n;
	std::cin >> n;
	std::cout << "violentayang" << n<<std::endl;
	return 0;
}
都是去掉C语言中的.h,在前面加上一个c就可以直接使用
变量声明
cpp变量声明可以直接在for循环中,而C语言有些古老标准均不支持
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
   
	int n;
	cin >> n;
	for(int i=0;i<=10;i++){
   
		cout << n << " ";
	}
	for(int i=0;i<=10;i++){
   
		cout << n+1 << " ";
	}
	return 0;
}
 bool变量
有两个值 分别为 true 和 false
非零值为true 0为false
#include<iostream>
using namespace std;
int main(){
   
	bool flag = true;
	bool flag2 = -1;
	bool flag3 = 0;
    cout << flag << endl;
	cout << flag2 << endl;
	cout << flag3 << endl;
	return 0;
}

const定义常量
const是常数、常量的意思
类似C语言#define
#include<iostream>
using namespace std;
int main(){
   
	const int max = 150;
	cout << max << endl;
	return 0;
}

 我们试着修改max看编译器是否允许通过

 可以把他看做一个宏定义,是不可改变的。
string类
字符串的定义与拼接
#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
	string a = "hello";
	string b = "world";
	string c = a + b;
	cout << c ;
	return 0;
}
直接进行相加即可拼接在一起
字符串的输入
#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
	string a = "hello";
	string b = "world";
	string c = a + b;
	string s;
	cin >> s;
	cout << s ;
	return 0;
}

 cin只可输入这种不带空格的单词,如果我们输入一个带空格的句子

 就会出现问题,所以需要使用getline这个函数
#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
	string a = "hello";
	string b = "world";
	string c = a + b;
	string s;
	getline(cin,s);
	cout << s ;
	return 0;
}

字符串的处理
获取字符串长度
可以通过s.length()来进行获取
#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
	string s;
	getline(cin,s);
	cout << s.length() << endl;
	cout << s ;
	return 0;
}

字符串的截取
s.substr(2,5) 从第二个字符开始,取五个字符
#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
	string s;
	getline(cin,s);
	string s_sub = s.substr(2,5);
	cout << s_sub << endl;
	return 0;
}

结构体
cpp中可以省略struct 相当于不用去写typedef了
#include<iostream>
#include<cstring>
using namespace std;
struct stu{
   
	string name;
	int age;
};
int main(){
   
	stu a[10];
	return 0;
}
引用&
引用相当于C语言中的指针,用于在子函数中改变变量的值。
#include<iostream>
#include<cstring>
using namespace std;
void change(int &a){
   
	a = a + 1;
}
int main(){
   
	int a = 10;
	change(a);
	cout << a;
	return 0;
}

STL

vector
vector是不定长数组,即动态数组
vector、map、stack、queue、set等在c++里都叫容器。
vector的使用
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> a(10,2);
	int len = a.size();
	cout << len;
	return 0;
}
vector是存在于std这个命名空间的,using namespace std必不可少。如果不写,引入的头文件将毫无作用。
vector创建数组方式
vector<int> v
先创建一个数组,然后对他分配大小
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v; //定义一个空数组v
	//分配数组大小
	v.resize(10);
	for(int i=0;i<10;i++){
   
		v[i] = i;
	}
	for(int i=0;i<10;i++){
   
		cout << v[i] << " ";
	}
	cout << endl;
	cout << v.size();
	return 0;
}
 
 push_back用于在数组最后追加元素
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v; //定义一个空数组v
	//分配数组大小
	v.resize(10);
	for(int i=0;i<10;i++){
   
		v[i] = i;
	}
	v.push_back(11);
	for(int i=0;i<11;i++){
   
		cout << v[i] << " ";
	}
	cout << endl;
	cout << v.size();
	return 0;
}
 
vector<int> v(10,2)
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v(10,2); 
	for(int i=0;i<10;i++){
   
		cout << v[i] << " ";
	}
	return 0;
}
v后面两个参数,第一个参数是开辟空间为10的数组,后面一个参数意思是数组没个数的初始值为2

vector<int> v(10)
指定一个长度为10的数组,默认为0
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v(10); 
	for(int i=0;i<10;i++){
   
		cout << v[i] << " ";
	}
	return 0;
}

迭代器
类似for循环,进行一个遍历
#include<iostream>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v(10); 
	for(auto p =v.begin(); p!=v.end(); p++)
		cout << *p << " ";
	return 0;
}
set
set是集合,它里面的元素各不相同,而且元素会按照从小到大排序
头文件 #include<set>
set的创建
#include<stdio.h>
#include<set>
using namespace std;
int main(){
   
	set<int> s;
	return 0;
}
s的后面不可以加内容
set基本操作
插入操作
#include<stdio.h>
#include<set>
using namespace std;
int main(){
   
	set<int> s;
	s.insert(0);
	s.insert(1);
	s.insert(2);
	for(auto p=s.begin();p!=s.end();p++){
   
		printf("%d ",*p);
	}
	return 0;
}
遍历使用迭代器进行 for auto
查找操作
s.find是个指针,比较查找后的值和s.end是否相等,相等就意味着不在,不等就意味着在
#include<stdio.h>
#include<set>
using namespace std;
int main(){
   
	set<int> s;
	s.insert(0);
	s.insert(1);
	s.insert(2);
	for(auto p=s.begin();p!=s.end();p++){
   
		printf("%d ",*p);
	}
	printf("\n");
	printf("%d ",s.find(2)!=s.end());  //1
	printf("%d ",s.find(1)!=s.end());  //1
	printf("%d ",s.find(3)!=s.end());  //0
	return 0;
}
 删除操作
#include<stdio.h>
#include<set>
using namespace std;
int main(){
   
	set<int> s;
	s.insert(0);
	s.insert(1);
	s.insert(2);
	for(auto p=s.begin();p!=s.end();p++){
   
		printf("%d ",*p);
	}
	printf("\n");
	s.erase(1);
	for(auto p=s.begin();p!=s.end();p++){
   
		printf("%d ",*p);
	}
	return 0;
}
 
map
map是键值对,它会自动将所有的键值对按照键从小到大排序
头文件 #include<map
创建键值对
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
int main(){
   
	map<string,int> m;
	return 0;
}
添加操作
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
int main(){
   
	map<string,int> m;
	m["ayang"] = 20;
	m["zxx"] = 20;
	return 0;
}
访问操作
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
int main(){
   
	map<string,int> m;
	m["ayang"] = 20;
	m["zxx"] = 20;
	printf("%d",m["ayang"]);
	return 0;
}
遍历操作
仍然使用迭代器进行遍历,但是其中指针有两个指向,一个是first,指向的是键,一个是second,指向的是值。
其本质是一个结构体
struct s{
   
	string key;
	int value;
};
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
int main(){
   
	map<string,int> m;
	m["ayang"] = 20;
	m["zxx"] = 20;
	for(auto p=m.begin();p!=m.end();p++){
   
		cout << p->first << " ";
		printf("%d ",p->second);
	}
	return 0;
}
 stack
栈是基本的数据结构,先进后出。
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
int main(){
   
	stack<int> s;
	//压栈
	s.push(1);
	s.push(2);
	s.push(3);
	//访问 栈只能访问栈顶
	printf("%d\n",s.top()); //s.top可以获取栈顶元素
	//出栈
	s.pop();
	cout << s.top() << endl;
	printf("栈的长度:%d",s.size());
	return 0;
}
 栈不可以使用迭代器进行遍历,因为没有begin和end

queue
基本数据结构,先进先出。
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int main(){
   
	queue<int> q;
	//入队操作
	for(int i=0;i<=10;i++){
   
		q.push(i);
	}
	//队可以访问队尾和队首
	cout << "队首为:" << q.front() << endl;
	cout << "队尾为:"  << q.back() << endl;
	return 0;
}
 
 出队只能队首出队,入队只能队尾入队
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int main(){
   
	queue<int> q;
	//入队操作
	for(int i=0;i<=10;i++){
   
		q.push(i);
	}
	//队可以访问队尾和队首
	cout << "队首为:" << q.front() << endl;
	cout << "队尾为:"  << q.back() << endl;
	q.pop();
	cout << "队首为:" << q.front() << endl;
	cout << "队尾为:"  << q.back() << endl;
	return 0;
}
 
unordered_map 和 unordered_set
这连两个其实就是不会排序的map(键值对)和set(集合),它们省去了排序的过程,如果刷题时超时了,可以采用这两个。
需要注意的是这两个输出的是无序的,随机输出。
#include<stdio.h>
#include<iostream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main(){
   
	unordered_map<string,int> m;
	unordered_set<int> s;
	m["ayang"] = 18;
	m["yangde"] = 20;
	for(auto p=m.begin();p!=m.end();p++){
   
		cout << p->first << p->second << endl;
	}
	s.insert(1);
	s.insert(2);
	for(auto p = s.begin();p!=s.end();p++){
   
		cout << *p << endl;
	}
	return 0;
}
 
bitset位运算
bitset类似于一个字符数组,但是它是从二进制的低位到高位依次为b[0],b[1],…,所以按照b[i]的方式输出和直接输出b的结果相反
初始化
bitset<5> b; 5表示5个二进制位,初始化为0
bitset<5> b(u); u为unsigned int,如果为u = 1,则输出b的结果为00001
bitset<8> b(s); s为字符串,如“1001”,则输出b的结果为00001101,在前面补0
bitset<5> b(s,pos,n) 从字符串s[pos]开始,n位长度
#include<stdio.h>
#include<bitset>
#include<iostream>
using namespace std;
int main(){
   
	bitset<5> b(19); //获得19这个数字对应的二进制
	cout << b << endl;
	for(int i=0;i<b.size();i++){
   
		cout << b[i] << " ";
	}
	return 0;
}

处理
| 方法 | 作用 | 
|---|---|
| b.any() | b是否有1的二进制位 | 
| b.none() | b不存在1吗 | 
| b.count() | b中1的个数 | 
| b.size() | b中元素的个数 | 
| b.test(i) | 下标为i处是不是1 | 
| b.set(i) | 把下标i处设置为1 | 
| b.reset() | 所有位归零 | 
| b.reset(i) | 把第i位归零 | 
| b.flip() | 所有位取反 | 
| b.flip(i) | 把第i位取反 | 
| b.to_ulong | 转换为unsigned long类型 | 
#include<stdio.h>
#include<bitset>
#include<iostream>
using namespace std;
int main(){
   
	bitset<5> b(19);
	cout << b << endl;
	for(int i=0;i<b.size();i++){
   
		cout << b[i] << " ";
	}
	cout << endl;
	cout << "是否有1:" << b.any() << endl;
	cout << "是否不存在1:" << b.none() << endl;
	cout << "1的个数" << b.count() << endl;
	cout << "b的元素个数:" << b.size() << endl;
	cout << "下标为i的元素不是1:" << b.test(2) << endl;
	b.flip(1);
	cout << b << endl;
	unsigned long a = b.to_ulong();
	cout << a << endl;
	return 0;
}
 
bitset<5> b(s,pos,n)	从字符串s[pos]开始,n位长度
#include<stdio.h>
#include<bitset>
#include<iostream>
using namespace std;
int main(){
   
	string m = "1100011010101011";
	bitset<7> b(m,2,7);
	cout << b << endl;
	return 0;
}

sort函数
主要功能是对一个数组(int arr[]或者vector进行排序,vector是容器,需要用v.begin()表示头,v.end()表示尾,而int arr[]使用arr表示数组的首地址,arr+n表示尾部)
#include<stdio.h>
#include<bitset>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
   
	vector<int> v(10);
	for(int i=0;i<10;i++){
   
		v[i] = i;
	}
	v.push_back(-1);
	for(auto p = v.begin();p!=v.end();p++){
   
		cout << *p << " ";
	}
	sort(v.begin(),v.end());
	cout << endl;
	for(auto p = v.begin();p!=v.end();p++){
   
		cout << *p << " ";
	}
	return 0;
}
 
使用sort自定义cmp
sort默认是从小到大排序,cmp允许我们定义一些比较复杂的规则
bool cmp(int x,int y)
如果返回值为真,那么x放在y前面(返回值为假的时,交换两个数),否则放在后面
注意:cmp返回值部分必须使用>或者<,不能有≥或者≤
#include<stdio.h>
#include<bitset>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct stu{
   
	string name;
	int age;
};
bool cmp(stu a,stu b){
   
	if(a.age!=b.age){
   
		return a.age < b.age;
	}else{
   
		return a.name < b.name;
	}
}
int main(){
   
	stu s[3];
	for(int i=0;i<3;i++){
   
		cin >> s[i].name >> s[i].age; 
	}
	sort(s,s+3,cmp);
	for(int i=0;i<3;i++){
   
		cout << s[i].name << " " << s[i].age << endl;
	}
	return 0;
}
 
cctype头文件
#include<iostream>
#include<cctype>
using namespace std;
int main(){
   
	char c = 'A';
	cout << "isalpha: " << isalpha(c) << endl; //是否是字母
	cout << "islower: " << islower(c) << endl; //是否是小写字母
	cout << "isupper: " << isupper(c) << endl; //是否是大写字母
	cout << "isalnum: " << isalnum(c) << endl; //是否是数字或字母
	cout << "isspace: " << isspace(c) << endl; //判断是否是空格 \t \r \n
	char s = tolower(c); //转换为小写字母
	cout << s << endl;
	char s1 = toupper(s); //转换为大写字母
	cout << s1 << endl;
	return 0;
}
 
c++11的新特性
auto声明
作用:可以让编辑器根据初始值直接推断变量的类型
 推广:迭代器 替换 set<int>::iterator
#include<iostream>
#include<cctype>
using namespace std;
int main(){
   
	auto x = 19;
	auto y = 1.8;
	cout << x << " " << y << endl;
	return 0;
}
auto使用必须定义一个变量进行初始化。

 会报错。
#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	auto x = 19;
	auto y = 1.8;
	vector<int> v(10,1);
	for(auto p = v.begin();p != v.end();p++){
   
		cout << *p << endl;
	}
	return 0;
}

 容器中可以使用迭代器的:
- 数组
- 集合
- 键值对
- 不排序的也可以
基于范围的for循环
传值
for(int i:arr)
	cout << i << endl;
输出数组中的每一个元素的值,这里不能改变元素的数值
#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	int arr[5] = {
   1};
	for(int i : arr){
   
		cout << i << " ";
	}
	return 0;
}

#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	int arr[5] = {
   1};
	for(int i : arr){
   
		i++;
		cout << i << " ";
	}
	return 0;
}

#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	int arr[5] = {
   1};
	for(int i : arr){
   
		i++;
	}
	for(int i : arr){
   
		cout << i << " ";
	}
	return 0;
}
 
 我们可以发现,这里数组的值并没有被改变,相当于只是一个函数,函数外的值不改变。
传址
for(int &i:arr)
	i = i*2;
将数组中的每一个元素都乘以2,只有在引用的时候才能改变元素的值
#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	int arr[5] = {
   1};
	for(int &i : arr){
   
		i++;
	}
	for(int i : arr){
   
		cout << i << " ";
	}
	return 0;
}
  这样我们再去运行就会发现,数组里的值都被改变了。
这样我们再去运行就会发现,数组里的值都被改变了。
推广
for(auto i:v)
	cout << i << " ";
这里的v是一个vector,其实所有的容器都可以使用这种方式来循环。(配合auto)
#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
int main(){
   
	int arr[5] = {
   1};
	for(auto i : arr){
   
		cout << i << " ";
	}
	return 0;
}

 容器全部可以使用这种方式进行for循环
to_string
#include<iostream>
#include<string>
using namespace std;
int main(){
   
	string s = to_string(123.1);
	cout << s << endl;
	return 0;
}

stoi stod
作用:将字符串转换为其他变量
处理:
- stoi 转化为int型 int a = stoi(“123”);
- stod 转化为double型 double b = stod(“123.56”);
#include<iostream>
#include<string>
using namespace std;
int main(){
   
	string s = "123";
	int a = stoi(s);
	cout << a ;
	return 0;
}
补充:stof stold stol stoll stoul stoull
转载:https://blog.csdn.net/L6666688888/article/details/128319512
 
					