C++ STL摘记

一、string类补充

1.函数-find/rfind

find和rfind函数,返回的是下标或者string::npos
index=ss.find(s1,pos,num)
find从pos(包括)开始往右查找(num的作用待补充)
index=ss.rfind(s1,poss,num)
rfind从pos(包括)开始往左查找(num的作用待补充)
代码示例:

//>>>>Qiansui
#include<iostream>
#include<algorithm>
#include<cmath>
#define ll long long

using namespace std;
const int maxm=1e4+5;
string ss[3]{"123asd123","a123gh123","ea123s123"},s1="123";

int main(){
	int index;
	cout<<"find\n";
	for(int i=0;i<3;++i){
		index=ss[i].find(s1);
		if(index!=string::npos) cout<<index<<endl;
		else cout<<"Error\n";
	}
	cout<<"rfind\n";
	for(int i=0;i<3;++i){
		index=ss[i].rfind(s1,6);
		if(index!=string::npos) cout<<index<<endl;
		else cout<<"Error\n";
	}
	cout<<string::npos;
	return 0;
}

运行结果:
image

2.遍历

代码示例:

#include<iostream>
#include <string>
using namespace std;
int main(){
	string s1{"world"};
	for(int i=0;i<s1.size(); ++i)
		cout<<s1[i]<<endl;
	for(auto c : s1)
		cout<<c<<endl;
 	return 0;
}

运行结果:
image

3.string的修改器

push_back()和pop_back()实现string类对象的前后的单个字符的改变

string ss;
char ch='a';
ss.push_back(ch);//Append character to string (public member function)
cout<<ss<<"\n";
ss.push_back('b');
cout<<ss<<"\n";
ss.pop_back();//Delete last character (public member function)
cout<<ss<<'\n';
/*
a
ab
a
*/

二、STL函数

头文件:#include<algorithm>

1. reverse函数

https://en.cppreference.com/w/cpp/algorithm/reverse
可以翻转数组、字符串、容器等
reverse函数用于反转在[first,last)范围内的顺序[first,last)(左闭右开),reverse函数没有返回值

例题:
简单的翻转 https://atcoder.jp/contests/abc284/tasks/abc284_a?lang=en vector<string>

2. fill函数

用于对容器的值进行填充,可以填充普通数组、vector等

fill(a.begin(),a.end(),10);
int arr[10];
fill(arr, arr + 10, 2);

相关资料:

  1. C++ 中 fill() 的使用
  2. fill函数,fill与memset函数的区别

3. unique函数

该函数的作用是“去除”容器或者数组中相邻元素的重复出现的元素
相关资料:
https://blog.csdn.net/qq_36561697/article/details/82356053


三、priority_queue 优先队列

头文件#include<queue>

相关资料

https://blog.csdn.net/c20182030/article/details/70757660

摘记

  1. 默认从大到小,如:
	priority_queue<int> a;
	a.push(1);
	cout<<a.top()<<'\n';
	a.push(10);
	cout<<a.top()<<'\n';
	a.push(8);
	cout<<a.top()<<'\n';
	a.push(3);
	cout<<a.top()<<'\n';
	while(a.size()){
		cout<<a.size()<<" "<<a.top()<<'\n';
		a.pop();
	}
	return ;

image
greater<int>——从小到大——priority_queue<int,vector<int>,greater<int>> a
less<int>——从大到小——priority_queue<int,vector<int>,less<int>> a
top返回第一个,pop删除第一个,即队列头

自定义排序

struct node{
	ll val,id,c;
	bool operator <(const node &a)const{
		return a.val<val;//必须重载小于号和使用小于号
	}
};
priority_queue<node> q;//此处为最小堆

四、multiset

c++语言中,multiset是set库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存在重复的数。

操作

c.erase(elem) 删除与elem相等的所有元素,返回被移除的元素个数。

相关资料

multiset用法总结 https://blog.csdn.net/sodacoco/article/details/84798621

例题

双multiset维护数组的k个最大值Best Performances做法见提交和题解


五、map

map 由红黑树实现,插入、查找、删除的时间复杂度近似为\(O(log N)\)
unordered_map 由哈希表实现,插入、查找的时间复杂度为\(O(常数)\)

unordered_map 自定义哈希函数

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

unordered_map<int, int, custom_hash> c;

相关资料

map的默认排序和自定义排序 https://www.cnblogs.com/stay-alive/p/8215400.html


六、std :: function

例如:

void solve(){
	function <void(int, int)> output = [](int x, int y) -> void {
		cout << x + y << '\n';
	};
	auto output2 = [](int x, int y){
		return x + y;
	};
	output(1, 2);
	cout << output2(2, 34) << '\n';
	return ;
}

末尾 - 杂项(此处不一定都是STL的内容)

memset 函数

头文件:C 中 #include<string.h> ,C++ 中 #include<cstring>

memset()函数的用法详解

memset 是按字节赋值,而 int 有 4 个字节,所以 memset(a, 127, sizeof(a)) 不是将 a 全赋 127,而是赋了一个很大很大的数($ > 2^{30} $)

__lg() 函数和 log2() 函数

__lg() 函数接受一个整数,返回一个整数,表示该数二进制表示下最高一位 1 的位置。事实上这个数就是 \(\lfloor \log_2 n \rfloor\) 的准确值。

log2() 函数接受一个浮点数,返回一个浮点数,表示该数以 2 为底的对数的具体值。由于是浮点数,因此精度有限。当输入的数绝对值很大时,很可能最终结果与准确值相差 eps。

posted on 2023-03-25 21:35  Qiansui  阅读(27)  评论(0编辑  收藏  举报