随笔 - 164  文章 - 0  评论 - 4  阅读 - 9903

C++ STL摘记

1. string类补充

1.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

1.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

1.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
*/

2. STL函数

头文件:#include<algorithm>

2.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.2. fill函数

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

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

相关资料:

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

2.3. unique函数

该函数的作用是“去除”容器或者数组中相邻元素的重复出现的元素
返回值是一个迭代器,它指向的是去重后容器中不重复序列的最后一个元素的下一个元素。

sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());

相关资料:
https://www.cnblogs.com/wangkundentisy/p/9033782.html
https://blog.csdn.net/qq_36561697/article/details/82356053


3. priority_queue 优先队列

头文件#include<queue>

3.1. 相关资料

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

3.2. 摘记

  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删除第一个,即队列头

3.3. 自定义排序

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

4. multiset

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

4.1. 操作

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

4.2. 相关资料

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

4.3. 例题

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


5. map

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

5.1. 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;

5.2. 相关资料

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

6. vector

类似于数组的容器,但是动态扩容(细节不清楚,日后摘记)


7. 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 ;
}

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

8.1. STL 自定义排序

参考链接:https://zhuanlan.zhihu.com/p/600531550

远古图片,保留一下哈哈哈~
img

8.2. memset 函数

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

memset()函数的用法详解

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

8.3. __lg() 函数和 log2() 函数

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

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

posted on   Qiansui  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示