算法设计C++必背STL函数

# stack

声明
stack<int> s;
stack<string> s;
stack<node> s;//node是结构体类型
s.push(ele)	元素ele入栈
s.pop()	移除栈顶元素 
s.top()	取得栈顶元素(但不删除)
s.empty()	检测栈内是否为空,空为真
s.size()	返回栈内元素的个数

栈遍历

```cpp
stack<int> st;
for (int i = 0; i < 10; ++i) st.push(i);
while (!st.empty()) {
    int tp = st.top(); // 栈顶元素
    st.pop();
}

queue

//头文件
#include
//定义初始化
queue q;
q.front() 返回队首元素
q.back() 返回队尾元素
q.push(element) 尾部添加一个元素element 进队
q.pop() 删除第一个元素 出队
q.size() 返回队列中元素个数,返回值类型unsigned int
q.empty() 判断是否为空,队列为空,返回true

deque

//添加头文件
#include
//初始化定义
deque dq;
push_back(x)/push_front(x) 把x插入队尾后 / 队首
back()/front() 返回队尾 / 队首元素
pop_back() / pop_front() 删除队尾 / 队首元素
erase(iterator it) 删除双端队列中的某一个元素
erase(iterator first,iterator last) 删除双端队列中[first,last)中的元素
empty() 判断deque是否空
size() 返回deque的元素数量
clear() 清空deque
4.3 注意点
deque可以进行排序

双端队列排序一般不用,感觉毫无用处,使用其他STL依然可以实现相同功能

//从小到大
sort(q.begin(), q.end())
//从大到小排序
sort(q.begin(), q.end(), greater());//deque里面的类型需要是int型
sort(q.begin(), q.end(), greater());//高版本C++才可以用

priority_queue

它的底层是通过堆来实现的。

//头文件
#include
//初始化定义
priority_queue q;
q.top() 访问队首元素
q.push() 入队
q.pop() 堆顶(队首)元素出队
q.size() 队列元素个数
q.empty() 是否为空
注意没有clear()!
优先队列只能通过top()访问队首元素(优先级最高的元素)

基本数据类型的优先级
priority_queue pq; // 默认大根堆, 即每次取出的元素是队列中的最大值
priority_queue<int, vector, greater> q; // 小根堆, 每次取出的元素是队列中的最小值

vector 是用来承载底层数据结构堆的容器,若优先队列中存放的是double型数据,就要填vector< double >
总之存的是什么类型的数据,就相应的填写对应类型。同时也要改动第三个参数里面的对应类型。
less 表示数字大的优先级大,堆顶为最大的数字
greater表示数字小的优先级大,堆顶为最小的数字
int代表的是数据类型,也要填优先队列中存储的数据类型

下面介绍基础数据类型优先级设置的写法:

基础写法(非常常用):
priority_queue q1; // 默认大根堆, 即每次取出的元素是队列中的最大值
priority_queue<int, vector, less > q2; // 大根堆, 每次取出的元素是队列中的最大值,同第一行

priority_queue<int, vector, greater > q3; // 小根堆, 每次取出的元素是队列中的最小值
自定义排序(不常见,主要是写着麻烦):
下面的代码比较长,基础类型优先级写着太麻烦,用第一种即可。

struct cmp1 {
bool operator()(int x, int y) {
return x > y;
}
};
struct cmp2 {
bool operator()(const int x, const int y) {
return x < y;
}
};
priority_queue<int, vector, cmp1> q1; // 小根堆
priority_queue<int, vector, cmp2> q2; // 大根堆
高级数据类型(结构体)优先级
即优先队列中存储结构体类型,必须要设置优先级,即结构体的比较运算(因为优先队列的堆中要比较大小,才能将对应最大或者最小元素移到堆顶)。

优先级设置可以定义在结构体内进行小于号重载,也可以定义在结构体外。

//要排序的结构体(存储在优先队列里面的)
struct Point {
int x, y;
};
版本一:自定义全局比较规则
//定义的比较结构体
//注意:cmp是个结构体
struct cmp {//自定义堆的排序规则
bool operator()(const Point& a,const Point& b) {
return a.x < b.x;
}
};

//初始化定义,
priority_queue<Point, vector, cmp> q; // x大的在堆顶
版本二:直接在结构体里面写
因为是在结构体内部自定义的规则,一旦需要比较结构体,自动调用结构体内部重载运算符规则。

结构体内部有两种方式:

方式一 :

struct node {
int x, y;
friend bool operator < (Point a, Point b) {//为两个结构体参数,结构体调用一定要写上friend
return a.x < b.x;//按x从小到大排,x大的在堆顶
}
};
方式二 :(推荐此种)

struct node {
int x, y;
bool operator < (const Point &a) const {//直接传入一个参数,不必要写friend
return x < a.x;//按x升序排列,x大的在堆顶
}
};

priority_queue q;
注意: 优先队列自定义排序规则和sort()函数定义cmp函数很相似,但是最后返回的情况是相反的。即相同的符号,最后定义的排列顺序是完全相反的。
所以只需要记住sort的排序规则和优先队列的排序规则是相反的就可以了。

当理解了堆的原理就会发现,堆调整时比较顺序是孩子和父亲节点进行比较,如果是 > ,那么孩子节点要大于父亲节点,堆顶自然是最小值。

存储特殊类型的优先级 存储pair类型
默认先对pair的first进行降序排序,然后再对second降序排序
对first先排序,大的排在前面,如果first元素相同,再对second元素排序,保持大的在前面。

#include<bits/stdc++.h>
using namespace std;
int main() {
    priority_queue<pair<int, int> >q;
	q.push({7, 8});
	q.push({7, 9});
	q.push(make_pair(8, 7));
    while(!q.empty()) {
        cout << q.top().first << " " << q.top().second << "\n";
        q.pop();
    }
    return 0;
}

map

//头文件
#include
//初始化定义
map<string, string> mp;
map<string, int> mp;
map<int, node> mp;//node是结构体类型
map特性:map会按照键的顺序从小到大自动排序,键的类型必须可以比较大小

6.2 函数方法
6.2.1 函数方法
代码 含义
mp.find(key) 返回键为key的映射的迭代器O(logN)注意:用find函数来定位数据出现位置,它返回一个迭代器。当数据存在时,返回数据所在位置的迭代器,数据不存在时,返回mp.end()
mp.erase(it) 删除迭代器对应的键和值
mp.erase(key) 根据映射的键删除键和值

mp.erase(first,last) 删除左闭右开区间迭代器对应的键和值O(last−first)
mp.size() 返回映射的对数
mp.clear() 清空map中的所有元素
mp.insert() 插入元素,插入时要构造键值对
mp.empty() 如果map为空,返回true,否则返回false
mp.begin() 返回指向map第一个元素的迭代器(地址)
mp.end() 返回指向map尾部的迭代器(最后一个元素的下一个地址)
mp.rbegin() 返回指向map最后一个元素的迭代器(地址)
mp.rend() 返回指向map第一个元素前面(上一个)的逆向迭代器(地址)
mp.count(key) 查看元素是否存在,因为map中键是唯一的,所以存在返回1,不存在返回0
mp.lower_bound() 返回一个迭代器,指向键值>= key的第一个元素
mp.upper_bound() 返回一个迭代器,指向键值> key的第一个元素
查找元素是否存在时,可以使用
①mp.find() ② mp.count() ③ mp[key]
但是第三种情况,如果不存在对应的key时,会自动创建一个键值对(产生一个额外的键值对空间)
所以为了不增加额外的空间负担,最好使用前两种方法

mp.begin()和mp.end()用法:
用于正向遍历map

map<int,int> mp;
mp[1] = 2;
mp[2] = 3;
mp[3] = 4;
auto it = mp.begin();
while(it != mp.end()) {
	cout << it->first << " " << it->second << "\n";
	it ++;
}

mp.rbegin()和mp.rend()
用于逆向遍历map

map<int,int> mp;
mp[1] = 2;
mp[2] = 3;
mp[3] = 4;
auto it = mp.rbegin();
while(it != mp.rend()) {
	cout << it->first << " " << it->second << "\n";
	it ++;
}

二分查找
二分查找lower_bound() upper_bound()

map的二分查找以第一个元素(即键为准),对键进行二分查找
返回值为map迭代器类型

#include<bits/stdc++.h>
using namespace std;

int main() {
	map<int, int> m{{1, 2}, {2, 2}, {1, 2}, {8, 2}, {6, 2}};//有序
	map<int, int>::iterator it1 = m.lower_bound(2);
	cout << it1->first << "\n";//it1->first=2
	map<int, int>::iterator it2 = m.upper_bound(2);
	cout << it2->first << "\n";//it2->first=6
	return 0;
}

//先声明
map<string, string> mp;
方式一:
mp[“学习”] = “看书”;
mp[“玩耍”] = “打游戏”;
方式二:插入元素构造键值对
mp.insert(make_pair(“vegetable”,“蔬菜”));
方式三:
mp.insert(pair<string,string>(“fruit”,“水果”));
方式四:
mp.insert({“hahaha”,“wawawa”});

mp[“菜哇菜”] = “强哇强”;
cout << mp[“菜哇菜”] << “\n”;//只是简写的一个例子,程序并不完整
遍历访问
方式一:迭代器访问
map<string,string>::iterator it;
for(it = mp.begin(); it != mp.end(); it++) {
// 键 值
// it是结构体指针访问所以要用 -> 访问
cout << it->first << " " << it->second << “\n”;
//*it是结构体变量 访问要用 . 访问
//cout<<(*it).first<<" "<<(*it).second;
}
方式二:智能指针访问
for(auto i : mp)
cout << i.first << " " << i.second << endl;//键,值
方式三:对指定单个元素访问
map<char,int>::iterator it = mp.find(‘a’);
cout << it -> first << " " << it->second << “\n”;
方式四:c++17特性才具有
for(auto [x, y] : mp)
cout << x << " " << y << “\n”;
//x,y对应键和值

set

set容器中的元素不会重复,当插入集合中已有的元素时,并不会插入进去,而且set容器里的元素自动从小到大排序。

即:set里面的元素不重复 且有序

//头文件
#include
//初始化定义
set s;

7.2 函数方法
代码 含义
s.begin() 返回set容器的第一个元素的地址(迭代器)O ( 1 ) O(1)O(1)
s.end() 返回set容器的最后一个元素的下一个地址(迭代器)O ( 1 ) O(1)O(1)
s.rbegin() 返回逆序迭代器,指向容器元素最后一个位置O ( 1 ) O(1)O(1)
s.rend() 返回逆序迭代器,指向容器第一个元素前面的位置O ( 1 ) O(1)O(1)
s.clear() 删除set容器中的所有的元素,返回unsigned int类型O ( N ) O(N)O(N)
s.empty() 判断set容器是否为空O ( 1 ) O(1)O(1)
s.insert() 插入一个元素
s.size() 返回当前set容器中的元素个数O ( 1 ) O(1)O(1)
erase(iterator) 删除定位器iterator指向的值
erase(first,second) 删除定位器first和second之间的值
erase(key_value) 删除键值key_value的值
查找
s.find(element) 查找set中的某一元素,有则返回该元素对应的迭代器,无则返回结束迭代器
s.count(element) 查找set中的元素出现的个数,由于set中元素唯一,此函数相当于查询element是否出现
s.lower_bound(k) 返回大于等于k的第一个元素的迭代器O ( l o g N ) O(logN)O(logN)
s.upper_bound(k) 返回大于k的第一个元素的迭代器O ( l o g N ) O(logN)O(logN)

迭代器访问
for(set::iterator it = s.begin(); it != s.end(); it++)
cout << *it << " ";
for(auto i : s)
cout << i << endl;
//第一种
cout << *s.rbegin() << endl;
//第二种
set::iterator iter = s.end();
iter–;
cout << (*iter) << endl; //打印2;
//第三种
cout << *(–s.end()) << endl;
重载<运算符
基础数据类型
方式一:改变set排序规则,set中默认使用less比较器,即从小到大排序。(常用)

set s1; // 默认从小到大排序
set<int, greater > s2; // 从大到小排序
方式二:重载运算符。(很麻烦,不太常用,没必要)

//重载 < 运算符
struct cmp {
    bool operator () (const int& u, const int& v) const {
       // return + 返回条件
       return u > v;
    }
};
set<int, cmp> s; 

for(int i = 1; i <= 10; i++)
    s.insert(i);
for(auto i : s)
    cout << i << " ";

方式三:初始化时使用匿名函数定义比较规则

set<int, function<bool(int, int)>> s([&](int i, int j){
    return i > j; // 从大到小
});
for(int i = 1; i <= 10; i++)
    s.insert(i);
for(auto x : s)
    cout << x << " ";

直接重载结构体运算符即可,让结构体可以比较。

struct Point {
	int x, y;
	bool operator < (const Point &p) const {
		// 按照点的横坐标从小到大排序,如果横坐标相同,纵坐标从小到大
		if(x == p.x)
			return y < p.y;
		return x < p.x;
	}
};

set<Point> s;
for(int i = 1; i <= 5; i++) {
    int x, y;
    cin >> x >> y;
    s.insert({x, y});
}

for(auto i : s)
    cout << i.x << " " << i.y << "\n";

pair

pair只含有两个元素,可以看作是只有两个元素的结构体。

代替二元结构体
作为map键值对进行插入(代码如下)

map<string, int> mp;
mp.insert(pair<string, int>("xingmaqi",1));
// mp.insert(make_pair("xingmaqi", 1));
// mp.insert({"xingmaqi", 1});

//头文件
#include<utility>

//1.初始化定义
pair<string, int> p("wangyaqi",1);//带初始值的
pair<string, int> p;//不带初始值的

//2.赋值
p = {"wang", 18};
p = make_pair("wang", 18);
p = pair<string, int>("wang", 18);
//定义结构体数组
pair<int,int> p[20];
for(int i = 0; i < 20; i++) {
	//和结构体类似,first代表第一个元素,second代表第二个元素
	cout << p[i].first << " " << p[i].second;
}

string

一、初始化
初始化有两种方式,其中使用等号的是拷贝初始化,不使用等号的是直接初始化。

string str1 = “hello world”; // str1 = “hello world”

string str2(“hello world”); // str2 = “hello world”

string str3 = str1; // str3 = “hello world”

string str4(str2); // str4 = “hello world”

string str5(10,‘h’); // str5 = “hhhhhhhhhh”

string str6 = string(10,‘h’); // str6 = “hhhhhhhhhh”

string str7(str1,6); // str7 = “world” 从字符串str1第6个字符开始到结束,拷贝到str7中

string str8 = string(str1,6); // str8 = “world”

string str9(str1,0,5); // str9 = “hello” 从字符串str1第0个字符开始,拷贝5个字符到str9中

string str10 = string(str1,0,5); // str10 = “hello”

char c[] = “hello world”;

string str11(c,5); // str11 = “hello” 将字符数组c的前5个字符拷贝到str11中

string str12 = string(c,5); // str12 = “hello”

值得一提的是,如果:

string str13 = string(“hello world”,5) // str13 = “hello” 而非 " world"

此时,"hello world"应看作字符数组(参见str11),而非string对象(参见str7)

为了避免发生意外,在字符串插入、替换、添加、赋值、比较中去除了关于后一种的相关操作(参见后文)。

二、获取长度(length、size)
length()函数与size()函数均可获取字符串长度。

string str = “hello world”;

cout << str.length() << str.size(); // 11 11

当str.length()与其他类型比较时,建议先强制转换为该类型,否则会意想之外的错误。

比如:-1 > str.length() 返回 true。

三、插入(insert)
基本情况为以下四种,其余变形函数自行摸索即可。

string str = “hello world”;

string str2 = "hard ";

string str3 = “it is so happy wow”;

//s.insert(pos,n,ch) 在字符串s的pos位置上面插入n个字符ch

str.insert(6,4,‘z’); // str = “hello zzzzworld”

//s.insert(pos,str) 在字符串s的pos位置插入字符串str

str.insert(6,str2); // str = “hello hard world”

//s.insert(pos,str,a,n) 在字符串s的pos位置插入字符串str中位置a到后面的n个字符

str.insert(6,str3,6,9); // str = “hello so happy world”

//s.insert(pos,cstr,n) 在字符串s的pos位置插入字符数组cstr从开始到后面的n个字符

//此处不可将"it is so happy wow"替换为str3

str.insert(6,“it is so happy wow”,6); // str = “hello it is world”

四、替换(replace)
替换与插入对应,对比理解更为简单。

string str = “hello world”;

string str2 = "hard ";

string str3 = “it is so happy wow”;

//s.replace(p0,n0,n,ch) 删除p0开始的n0个字符,然后在p0处插入n个字符ch

str.replace(0,6,4,‘z’); // str = “zzzzworld”

//s.replace(p0,n0,str) 删除从p0开始的n0个字符,然后在p0处插入字符串str

str.replace(0,6,str2); // str = “hard world”

//s.replace(p0,n0,str,pos,n) 删除p0开始的n0个字符,然后在p0处插入字符串str中从pos开始的n个字符

str.replace(0,6,str3,6,9); // str = “so happy world”

//s.replace(p0,n0,cstr,n) 删除p0开始的n0个字符,然后在p0处插入字符数组cstr的前n个字符

//此处不可将"it is so happy wow"替换为str3

str.replace(0,6,“it is so happy wow”,6); // str = “it is world”

五、添加(append)
append函数用在字符串的末尾添加字符和字符串。(同样与插入、替换对应理解)

string str = “hello world”;

string str2 = "hard ";

string str3 = “it is so happy wow”;

//s.append(n,ch) 在当前字符串结尾添加n个字符c

str.append(4,‘z’); // str = “hello worldzzzz”

//s.append(str) 把字符串str连接到当前字符串的结尾

str.append(str2); // str = "hello worldhard "

//s.append(str,pos,n) 把字符串str中从pos开始的n个字符连接到当前字符串的结尾

str.append(str3,6,9); // str = "hello worldso happy "

//append(cstr,int n) 把字符数组cstr的前n个字符连接到当前字符串结尾

//此处不可将"it is so happy wow"替换为str3

str.append(“it is so happy wow”,6); // str = "hello worldit is "

六、赋值(assign)
赋值也是一种初始化方法,与插入、替换、添加对应理解较为简单。

string str;

string temp = “welcome to my blog”;

//s.assign(n,ch) 将n个ch字符赋值给字符串s

str.assign(10,‘h’); // str = “hhhhhhhhhh”

//s.assign(str) 将字符串str赋值给字符串s

str.assign(temp); // str = “welcome to my blog”

//s.assign(str,pos,n) 将字符串str从pos开始的n个字符赋值给字符串s

str.assign(temp,3,7); // str = “come to”

//s.assaign(cstr,n) 将字符数组cstr的前n个字符赋值给字符串s

//此处不可将"it is so happy wow"替换为temp

str.assign(“welcome to my blog”,7); //welcome

七、删除(erase)
string str = “welcome to my blog”;

//s.erase(pos,n) 把字符串s从pos开始的n个字符删除

str.erase(11,3); // str = “welcome to blog”

八、剪切(substr)
string str = “The apple thinks apple is delicious”;

//s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串

string s1 = str.substr(4,5); // s1 = “apple”

//s.substr(pos) 得到字符串s从pos到结尾的串

string s2 = str.substr(17); // s2 = “apple is delicious”

九、比较(compare)
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。

若是遇到‘\0’结束比较,则长的子串大于短的子串,如:“9856” > “985”。

如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。

string str1 = “small leaf”;

string str2 = “big leaf”;

//s.compare(str) 比较当前字符串s和str的大小

cout << str1.compare(str2); // 1

//s.compare(pos,n,str) 比较当前字符串s从pos开始的n个字符与str的大小

cout << str1.compare(2,7,str2); // -1

//s.compare(pos,n0,str,pos2,n) 比较当前字符串s从pos开始的n0个字符与str中pos2开始的n个字符组成的字符串的大小

cout << str1.compare(6,4,str2,4,4); // 0

//s.compare(pos,n0,cstr,n) 比较当前字符串s从pos开始的n0个字符与字符数组cstr中前n个字符的大小

//此处不可将"big leaf"替换为str2

cout << str1.compare(6,4,“big leaf”,4); // 1

十、交换(swap)
交换两个字符串的值。

string str1 = “small leaf”;

string str2 = “big leaf”;

//或者str1.swap(str2) ,输出结果相同

swap(str1,str2); // str1 = “big leaf” str2 = “small leaf”

swap(str1[0],str1[1]); // str1 = “ibg leaf”

十一、反转(reverse)
反转字符串。

string str = “abcdefghijklmn”;

reverse(str.begin(),str.end()); // str = “nmlkjihgfedcba”

十二、数值转化(sto*)
详情参见前篇文章《int、string 类型相互转换》。

(本小白调试时,只有将p设置为0或nullptr才运行成功)

to_string(val) //把val转换成string

stoi(s,p,b) //把字符串s从p开始转换成b进制的int

stol(s,p,b) //把字符串s从p开始转换成b进制的long

stoul(s,p,b) //把字符串s从p开始转换成b进制的unsigned long

stoll(s,p,b) //把字符串s从p开始转换成b进制的long long

stoull(s,p,b) //把字符串s从p开始转换成b进制的unsigned long long

stof(s,p) //把字符串s从p开始转换成float

stod(s,p) //把字符串s从p开始转换成double

stold(s,p) //把字符串s从p开始转换成long double

十三、迭代器(iterator)
详情参见下篇文章《string类型中迭代器的使用》。

string str = “abcdefghijklmn”;

//s.begin() 返回字符串s第一个字符的位置

char a = *(str.begin()); // a

//s.end() 返回字符串s最后一个字符串的后一个位置

char b = *(str.end()-1); // n

//s.rbegin() 返回字符串s最后一个字符的位置

char c = *(str.rbegin()); // n

//s.rend() 返回字符串s第一个字符的前一个位置

char d = *(str.rend()-1); // a

十四、查找(find)
14.1 find函数

string str = “The apple thinks apple is delicious”; //长度34

string key = “apple”;

//s.find(str) 查找字符串str在当前字符串s中第一次出现的位置

int pos1 = str.find(key); // 4

//s.find(str,pos) 查找字符串str在当前字符串s的[pos,end]中第一次出现的位置

int pos2 = str.find(key, 10); // 17

//s.find(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[pos,end]中第一次出现的位置

//此处不可将"delete"替换为str2(如果定义str2 = “delete”)

int pos3 = str.find(“delete”, 0, 2); // 26

//s.find(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置

int pos4 = str.find(‘s’, 0); // 15

14.2 rfind函数
//s.rfind(str) 查找字符串str在当前字符串s中最后一次出现的位置

int pos5 = str.rfind(key); // 17

//s.rfind(str,pos) 查找字符串str在当前字符串s的[0,pos+str.length()-1]中最后一次出现的位置

int pos6 = str.rfind(key, 16); // 4

//s.rfind(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[0,pos+n-1]中最后一次出现的位置

//此处不可将"apple"替换为key

int pos7 = str.rfind(“apple”, 40, 2); // 17

//s.rfind(ch.pos) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置

int pos8 = str.rfind(‘s’, 30); // 24

14.3 find_xxx_of函数
string str = “The early birds catch the warm”; //长度30

string key = “aeiou”;

find_first_of

// s.find_first_of(str) 查找字符串str中的任意字符在当前字符串s中第一次出现的位置

int pos1 = str.find_first_of(key); // 2

//s.find_first_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[pos,end]中第一次出现的位置

int pos2 = str.find_first_of(key, 10); // 11

//s.find_first_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[pos,end]中第一次出现的位置

//此处不可将"aeiou"替换为key

int pos3 = str.find_first_of(“aeiou”, 7, 2); // 17

//s.find_first_of(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置

int pos4 = str.find_first_of(‘r’, 0); // 6

find_first_not_of

//s.find_first_not_of(str) 查找字符串str之外的任意字符在当前字符串s中第一次出现的位置

int pos1 = str.find_first_not_of(key); // 0

//s.find_first_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[pos,end]中第一次出现的位置

int pos2 = str.find_first_not_of(key, 10); // 10

//s.find_first_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[pos,end]中第一次出现的位置

//此处不可将"aeiou"替换为key

int pos3 = str.find_first_not_of(“aeiou”, 7, 2); // 7

//s.find_first_not_of(str) 查找字符ch之外任意字符在当前字符串s的[pos,end]中第一次出现的位置

int pos4 = str.find_first_not_of(‘r’, 0); // 0

find_last_of

//s.find_last_of(str) 查找字符串str中的任意字符在当前字符串s中最后一次出现的位置

int pos1 = str.find_last_of(key); // 27

//s.find_last_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[0,pos]中最后一次出现的位置

int pos2 = str.find_last_of(key, 15); // 11

//s.find_last_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[0,pos]中最后一次出现的位置

//此处不可将"aeiou"替换为key

int pos3 = str.find_last_of(“aeiou”, 20, 2); // 17

//s.find_last_of(str) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置

int pos4 = str.find_last_of(‘r’, 30); // 28

find_last_not_of

//s.find_last_not_of(str) 查找字符串str之外的任意字符在当前字符串s中最后一次出现的位置

int pos1 = str.find_last_not_of(key); // 29

//s.find_last_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[0,pos]中最后一次出现的位置

int pos2 = str.find_last_not_of(key, 15); // 15

//s.find_last_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置

//此处不可将"aeiou"替换为key

int pos3 = str.find_last_not_of(“aeiou”, 20, 2); // 20

//s.find_last_not_of(str) 查找字符ch之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置

int pos4 = str.find_last_not_of(‘r’, 30); // 29


  • 15
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值