1.关于lower_bound( )和upper_bound( )的常见用法(转自 https://blog.csdn.net/qq_40160605/article/details/80150252)
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
const int INF=2*int(1e9)+10;
#define LL long long
int cmd(int a,int b){
return a>b;
}
int main(){
int num[6]={1,2,4,7,15,34};
sort(num,num+6); //按从小到大排序
int pos1=lower_bound(num,num+6,7)-num; //返回数组中第一个大于或等于被查数的值
int pos2=upper_bound(num,num+6,7)-num; //返回数组中第一个大于被查数的值
cout<<pos1<<" "<<num[pos1]<<endl;
cout<<pos2<<" "<<num[pos2]<<endl;
sort(num,num+6,cmd); //按从大到小排序
int pos3=lower_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于或等于被查数的值
int pos4=upper_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于被查数的值
cout<<pos3<<" "<<num[pos3]<<endl;
cout<<pos4<<" "<<num[pos4]<<endl;
return 0;
}
2.
C++头文件algorithm的reverse函数
reverse(beg,end)
reverse()会将区间[beg,end)内的元素全部逆序;
3.erase的3种用法:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
1
2
3
erase(pos,n);
删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
erase(position);
删除position处的一个字符(position是个string类型的迭代器)
erase(first,last);
删除从first到last之间的字符(first和last都是迭代器)
int test()
{
string str("This is an example phrase.");
string::iterator it;
// 第(1)种用法
str.erase(10, 8);
cout << str << endl; // "This is an phrase."
// 第(2)种用法
it = str.begin() + 9;
str.erase(it);
cout << str << endl; // "This is a phrase."
// 第(3)种用法
str.erase(str.begin() + 5, str.end() - 7);
cout << str << endl; // "This phrase."
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
结果:
This is an phrase.
This is a phrase.
This phrase.
请按任意键继续. . .
1
2
3
4
例子:去掉字符串种的重复字符
int color_brick(string str){
int res = 0;
sort(str.begin(), str.end());
cout << "sorted: " << endl << str << endl;
str.erase(unique(str.begin(), str.end()), str.end());
cout << "erased: " << endl << str << endl;
if (str.size() == 1)
res = 1;
else if (str.size() == 2)
res = 2;
return res;
}
void test(){
string str;
while (true){
cin >> str;
cout << color_brick(str) << endl;
}
}
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
结果:
sakdnasd9oqwdnas
sorted:
9aaadddknnoqsssw
erased:
9adknoqsw
4.C++ 中string.find() 函数的用法总结
- find函数返回类型 size_type
- string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
- string flag;
- string::size_type position;
- //find 函数 返回jk 在s 中的下标位置
- position = s.find("jk");
- if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
- {
- cout << "position is : " << position << endl;
- }
- else
- {
- cout << "Not found the flag" + flag;
- }
[cpp] view plaincopy
- //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
- flag = "c";
- position = s.find_first_of(flag);
- cout << "s.find_first_of(flag) is : " << position << endl;
[cpp] view plaincopy
- //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
- position=s.find("b",5);
- cout<<"s.find(b,5) is : "<<position<<endl;
[cpp] view plaincopy
- //查找s 中flag 出现的所有位置。
- flag="a";
- position=0;
- int i=1;
- while((position=s.find_first_of(flag,position))!=string::npos)
- {
- //position=s.find_first_of(flag,position);
- cout<<"position "<<i<<" : "<<position<<endl;
- position++;
- i++;
- }
- //查找flag 中与s 第一个不匹配的位置
- flag="acb12389efgxyz789";
- position=flag.find_first_not_of (s);
- cout<<"flag.find_first_not_of (s) :"<<position<<endl;
- //反向查找,flag 在s 中最后出现的位置
- flag="3";
- position=s.rfind (flag);
- cout<<"s.rfind (flag) :"<<position<<endl;
- }
说明:
1. 如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 这四个函数,查找s中含有sub中任意字母的索引。
2. 如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。