刷题的CPP准备

杂述

  • 综合使用C与CPP,scanfprintf 效率高
  • STL非常好用
  • string代替char数组
  • cin >> n;scanf("%d", &n); ⼀样。cout << n;printf("%d", n); ⼀样
  • bool变量,非零都是真
  • 官方网站 link

string

string s = "hello world"; // 赋值字符串
string s2 = s;
string s3 = s + s2; // 字符串拼接
string s4;
cin >> s4; // 读⼊字符串
cout << s; // 输出字符串
s.length();//字符串真实长度
getline(cin, s); // 读取一行字符串,包括空格
string s2 = s.substr(4); // 表示从下标4开始⼀直到结束
string s3 = s.substr(5, 3); // 表示从下标5开始,3个字符

STL之vector 动态数组

#include <iostream>
#include <vector>
using namespace std;
int main() {
	//定义时不指定大小,不指定默认值
	vector<int> v; // 定义⼀个vector v1,定义的时候没有分配⼤⼩
	cout << v.size(); // 输出vector v1的⼤⼩,此处应该为0
	v.resize(8); //动态分配大小为8,默认这8个元素都是0
	//定义时指定大小,指定默认值
	vector<int> c(100, 9);// 把100⻓度的数组中所有的值都初始化为9
	//增删改查
	v.push_back(1); // 在vector末尾添加⼀个元素1
	
	for (auto it = c.begin(); it != c.end(); it++) { // 使⽤迭代器的方式访问vector
		cout << *it << " ";
	}
	/*
	c.begin() 是⼀个指针,指向容器的第⼀个元素, c.end() 指向容器的最后⼀个元素的后⼀个位置
	*/
	return 0;
}

STL之set 集合

升序排列,无重复
无序的用#include <unordered_set>中的unordered_set

#include <iostream>
#include <set>
using namespace std;
int main() {
	set<int> s; // 定义⼀个空集合s
	s.insert(1); // 向集合s⾥⾯插⼊⼀个1
	cout << *(s.begin()) << endl; // 输出集合s的第⼀个元素 (前⾯的星号表示要对指针取值)
	for (int i = 0; i < 6; i++) {
		s.insert(i); // 向集合s⾥⾯插⼊i
	}
	for (auto it = s.begin(); it != s.end(); it++) { 
	// ⽤迭代器遍历集合s⾥⾯的每⼀个元素
		cout << *it << " ";
	}
	cout << endl << (s.find(2) != s.end()) << endl; 
	/* 查找集合s中的值,如果结果等于s.end()表示未找到 (因为s.end()表示s的最后⼀个元素的下⼀个元素所在的位置) cout << (s.find(10) != s.end()) << endl; */
	// s.find(10) != s.end()表示能找到10	这个元素
	s.erase(1); // 删除集合s中的1这个元素
	cout << (s.find(1) != s.end()) << endl; // 这时候元素1就应该找不到了
	return 0;
}

STL之map 映射

按key升序
无序的用#include <unordered_map>中的unordered_map

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
	map<string, int> m; // 定义⼀个空的map m,键是string类型的,值是int类型的
	m["hello"] = 2; // 将key为"hello", value为2的键值对(key-value)存⼊map中
	cout << m["hello"] << endl; 
	//访问map中key为"hello"的value, 如果key不存在,则返回0
	cout << m["world"] << endl;
	m["world"] = 3; // 将"world"键对应的值修改为3
	m[","] = 1; // 设⽴⼀组键值对,键为"," 值为1
	// ⽤迭代器遍历,输出map中所有的元素,键⽤it->first获取,值⽤it->second获取
	for (auto it = m.begin(); it != m.end(); it++) {
		cout << it->first << " " << it->second << endl;
	}
	// 访问map的第⼀个元素,输出它的键和值
	cout << m.begin()->first << " " << m.begin()->second << endl;
	// 访问map的最后⼀个元素,输出它的键和值
	cout << m.rbegin()->first << " " << m.rbegin()->second << endl;
	// 输出map的元素个数
	cout << m.size() << endl;
	return 0;
}

【注意】end()是最后一个元素的后一个,是空。而rbegin()才是最后一个元素

  • 排序问题
	map<string, int> m;	//key降序
	map<string, int, greater<string>> m;  //key升序

STL之栈stack

#include <iostream>
#include <stack>
using namespace std;
int main() {
	stack<int> s; // 定义⼀个空栈s
	for (int i = 0; i < 6; i++) {
		s.push(i); // 将元素i压⼊栈s中
	}
	cout << s.top() << endl; // 访问s的栈顶元素
	cout << s.size() << endl; // 输出s的元素个数
	s.pop(); // 移除栈顶元素
	return 0;
}

STL之队列queue

#include <iostream>
#include <queue>
using namespace std;
int main() {
	queue<int> q; // 定义⼀个空队列q
	for (int i = 0; i < 6; i++) {
		q.push(i); // 将i的值依次压⼊队列q中
	}
	cout << q.front() << " " << q.back() << endl; // 访问队列的队⾸元素和队尾元素
	cout << q.size() << endl; // 输出队列的元素个数
	q.pop(); // 移除队列的队⾸元素
	return 0;
}

位运算

排序函数

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) { // cmp函数返回的值是bool类型
	return a > b; // 从⼤到⼩排列
}
int main() {
	vector<int> v(10);
	for (int i = 0; i < 10; i++) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());// 因为这⾥没有传⼊参数cmp,所以按照默认,v从⼩到⼤排列
	
	int arr[10];
	for (int i = 0; i < 10; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + 10, cmp); // arr从⼤到⼩排列,因为cmp函数排序规则设置了从⼤到⼩
	return 0;
}

char 字母数字的判断转换函数

isalpha 字⺟(包括⼤写、⼩写)
islower (⼩写字⺟)
isupper (⼤写字⺟)
isalnum (字⺟⼤写⼩写+数字)
isblank (space和 \t )
isspace ( space 、 \t 、 \r 、 \n )
tolower 和 toupper <cctype>

CPP11的新特性

  • auto 自动推断变量类型,用在迭代器里
  • 基于范围的for循环
int arr[4] = {0, 1, 2, 3};
for (int i : arr)
	cout << i << endl; // 输出数组中的每⼀个元素的值,每个元素占据⼀⾏
	// i 依次表示数组中的每⼀个元素,此时read only
for (int &i : arr) // i为引⽤变量
	i = i * 2; // 将数组中的每⼀个元素都乘以2,arr[4]的内容变为了{0, 2, 4, 6}
  • 类型转换
    to_string 最常⽤的就是把⼀个 int 型变量或者⼀个数字转化为 string 类型的变量,当然也可以转 doublefloat 等类型的变量
    stoi stod
    stof (string to float)
    stold (string to long double)
    stol (string to long)
    stoll (string to long long)
    stoul(string to unsigned long)
    stoull (string to unsigned long long)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值