C++ STL摘记

发布时间 2023-03-25 21:39:07作者: Qiansui

一、string类补充

1.函数示例:

  (1)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

二、STL函数

头文件:#include

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>