C++ STL

  1. vector
#include <cstdio>
#include <vector> 
using namespace std;
// 定义 
vector<int> name;
vector<vector<int> > name;
vector<int> name[size];

// 访问
name[index];  //0 ~ name.size() - 1 
// 迭代器访问 
vector<int>::iterator it = name.begin();	//*it访问vector的元素 
for(int i = 0; i < name.size(); i++) {
	printf("%d ", *(it + i));
}
for(vector<int>::iterator it = name.begin(); it < name.begin(); it++) {
	printf("%d ", *it);
}
// 常用函数 
name.push_back(x);		// 尾部插入x 
name.pop_back();		// 删除尾部元素
name.size();
name.clear();
name.insert(it, x);		// 将x插入it(迭代器, 一般name.begin() + i)位置   O(N)
name.erase(name.begin() + i);						// 删除[3]元素 		  O(N) 
name.erase(name.begin() + 1, name.begin() + 3) ;	// 删除[1, 3)的元素  O(N) 
  1. set
// 内部自动有序, 且不含重复元素
#include <cstdio>
#include <set>
using namespace std; 

set<int> name;

// 仅迭代器访问
 set<int>::iterator it;
for(set<int>::iterator it = name.begin(); it != name.end(); it++) {
	printf("%d ", *it);
} 

// 常用函数
name.insert(x);		// O(logN)
set<int>::iterator it = name.find(x);			// O(logN)
name.erase(name.find(x));	// erase O(1)
name.erase(x);			// O(logN)
name.erase(name.find(x), name.end());	// 删除[x, end]区间内的元素
name.size();
name.clear();

// unordered_set 用散列代替红黑树 只去重不排序 速度比set快很多
  1. string
#include <cstdio>
#include <string>
using namespace std;

// 定义
string std;
string std = "string";

// 访问
str[i];
// 迭代器访问
string::iterator = str.begin() + 3;
for(string::iterator it = str.begin(); it != str.end(); it++) {
	printf("%c", *it);
} 

// 输入输出
#include <iostream> 
cin >> str;
cout << str;
printf("%s\n", str.c_str());	// 将string 变为 字符数组

// 常用函数 
+=
str = str1 + str2;	// 拼接str1 和str2 赋值给str
str += str1;		// str2 拼接到 str 上
// == != < <= > >= 字典顺序比较

str.length();
str.size();
str.insert(pos, string);	// 在pos位置插入string字符串
str.insert(str.begin() + i, str2.begin(), str2.end());	// 迭代器 在i位置插入str2某一区间[ , )
str.erase(it);
str.erase(it1, it2);	// [ , )
str.erase(pos, length);
str.clear();
str.substr(pos, length);	// 返回pos开始 长度length 的子字符串
string::npos 				// 用作find 匹配失败时的返回值, 为-1 或(unsigned_int)4294967295
str.find(str2);				// 匹配str2子字符串, 成功返回开始位置, 失败返回string::npos
str.find(str2, pos);		// 从pos位置开始匹配 O(MN)
str.replace(pos, len, str2); // 将从pos位置开始 长为len的部分替换为 str2 O(str.length()) 
str.replase(it, it2, str2);  // 将[it1, it2)部分替换为str2 				 O(str.length())
  1. map
// 自小到大排好序 
#include <cstdio>
#include <map>
using namespace std;

// 定义
map<tp1, tp2> name;

// 下标访问
map<char, int> mp;
mp['c'] = 20;
printf("%c", mp['c']); 		// 20 
// 迭代器访问
map<char, int>::iterator it;
for(it = mp.begin(); it != mp.end(); it++) {
	printf("%c %d\n", it->first, it->second);
} 

// 常用函数
it = mp.find('c');	//20 
mp.erase(it);
mp.erase('c');
mp.erase(it, mp.end());		//[first, last)
mp.size();
mp.clear();

// 建立字符串与数字的联系
// 判断大整数或其他类型数据是否存在
// multimap 一键多值, unordered_map 不排序 
  1. queue 和 priority_queue
// 先进先出
#include <cstdio>
#include <queue>
using namespace std;

// 定义
queue<int> q;

// 访问
q.front();
q.back(); 
 
// 常用函数
q.push(i);
q.pop();
q.empty();	// bool
q.size();

// deque 双端队列,首尾都可插入删除
// priority_queue 优先队列, 堆实现使队列最大位于队首

// 定义
priority_queue<int> pq;
// 访问
pq.top();
// 常用函数
pq.push(x);
int i = pq.top(); 
pq.pop();
pq.size();
pq.empty();

// 设置元素优先级
#include <functional> 
priority_queue<int, vector<int>, less<int> > pq;	//less 表示 数字大的优先	greater 表示 数字效小的优先 
// 结构体优先级设置
struct pen {
	string name;
	int price;
	friend bool operator < (pen &a, pen &b) {	// 重载 < 实现 价格高的优先级高, 不重载 > 
		return a.price < b.price;
	}
}; 
// OR 
struct cmp {
	bool operatro () (pen &a, pen &b) {	// cmp 返回 a>b 实现从高到低排序; 优先队列则相反 
		return a.price < b.price;		// 实现 价格低的优先级高 
	}
}; 

  1. stack
// 先进后出 
#include <cstdio>
#include <stack>
using namespace std;

// 定义
stack<int> st;

// 访问
st.top();

// 常用函数
st.push(x);
st.top();
st.pop();
st.empty();
st.size(); 
  1. pair
// 键值对 
#include <cstdio>
#include <utility>
using namespace std; 

// 定义 
pair<int, string> p;
pair<int, string> p(1, "str");	//初始化 
p = make_pair(1, "str");

// 访问
p.first; p.second;

// 常用函数
// == != < <= > >=, 先比较first,相等时比较second

map<string, int> mp;
mp.insert(make_pair(1, "str"));
mp.insert(pair<string, int> (1, "str"));
for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
	cout << it->first << " " << it->second << endl;
} 
  1. algorithm 头文件
// algorithm
#include <cstdio>
#include <algorithm>
using namespace std;
// 1
int a[10] = {1, 2, 3};
max(x, y);
min(x, y);
abs(x);	// 浮点数用 math头文件下的 fabs
swap(x, y);
// 2
reverse(a + 1, a + 3);		// [ , )
reverse(it1, it2); 	// [ , )

// 3  next_permutation(a, a + 3) 给出一个序列在全排列中的下一个序列 
do {
	printf("%d %d %d\n", a[0], a[1], a[2]);
}while(next_permutation(a, a + 3));
// 4
fill(a, a + 3, 123);	// a[0] ~ a[2] 赋值 123, 与memset不同 fill 的赋值可以是元素对应范围内任意值 
// 5
sort(a, a + 3);

bool cmp(int a, int b) {	// 降序排列 
	return a > b;
}
sort(a, a + 3, cmp); 
// vector, string, deque 可以使用sort进行排序 

// 6 
int* pos = lower_bound(a, a + 3, x);	// [first, last) 范围内第一个 大于等于 x的元素的位置 
int* pos = upper_bound(a, a + 3, x);	// [first, last) 范围内第一个 大于     x的元素的位置
int a[10] {1, 2, 3, 3, 3, 3, 4, 5, 6, 6};
// 寻找元素 3 的范围 
printf("[%d, %d)\n", lower_bound(a, a + 10, 3), upper_bound(a, a + 10, 3));	// 返回 [2, 6) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值