RT
首先我们需要这个
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string s("111144551144");保证你的dev是c++98或更晚的版本
pred=判断条件的函数的复杂度
find O(n)
干啥的应该知道吧
int main()
{
string::iterator ita = find(s.begin(), s.end(), '5');
string::iterator itb = find(s.begin(), s.end(), '6');
if(ita != s.end()) cout << "'5' is in the string" << endl;
else cout << "'5' isn't in the string" << endl;
if(itb != s.end()) cout << "'6' is in the string" << endl;
else cout << "'6' isn't in the string" << endl;
}输出
'5' is in the string
'6' isn't in the stringfind_if O(n·pred)
找第一个符合条件的
bool isjishu(char c)
{
return (c - '0') % 2;
}
int main()
{
string::iterator it = find_if(s.begin(), s.end(), isjishu);
cout << "the first jishu in the string is " << *it;
}输出
the first jishu in the string is 1注意这里是1而不是5(因为是找第一个符合条件的)
find_end O(nm·pred)
找最后一个符合条件的子串
bool comp(char c, int n)
{
return c - '0' == n;
}
int main()
{
string subsa("55");
int arr[2] = {1, 1};
vector<int> subsb(arr, arr + 2);
cout << "the last '55' found at xiabiao " << find_end(s.begin(), s.end(), subsa.begin(), subsa.end()) - s.begin() << endl;
cout << "the last '11' found at xiabiao " << find_end(s.begin(), s.end(), subsb.begin(), subsb.end(), comp) - s.begin() << endl;
}输出
the last '55' found at xiabiao 6
the last '11' found at xiabiao 8这里的comp用于比较char和int
注意”11”输出的是在下标8的位置(因为是找最后一个符合条件的)
find_first_of O(nm·pred)
找第一个串中第一个在第二个串中找得到的
bool comp(char c, int n)
{
return c - '0' == n;
}
int main()
{
string subsa("6451");
int arr[4] = {6, 4, 5, 2};
vector<int> subsb(arr, arr + 4);
cout << "the first right num of sa is " << *find_first_of(s.begin(), s.end(), subsa.begin(), subsa.end()) << endl;
cout << "the first right num of sb is " << *find_first_of(s.begin(), s.end(), subsb.begin(), subsb.end(), comp) << endl;
}输出
the first right num of sa is 1
the first right num of sb is 4-
扫一遍s,发现1可以在sa中找到, 输出1
-
扫一遍s,发现1在sb中找不到, 接着扫, 发现4在sb中找得到, 输出4
这个comp和上面那个一样
adjacent_find O(n·pred)
找串中第一个相邻相等的两个元素(返回第一个的位置)
bool comp(char i, char j)
{
return i > '3' && j > '3' && i == j;
}
int main()
{
cout << "the first pair of the string is " << *adjacent_find(s.begin(), s.end()) << endl;
cout << "the first >3 pair of the string is " << *adjacent_find(s.begin(), s.end(), comp) << endl;
}输出
the first pair of the string is 1
the first >3 pair of the string is 4-
发现(1,1)相邻相等,输出1
-
发现(1,1)相邻相等但<3,接着扫,发现(4,4)>3且相邻相等, 输出4
这个comp用来判断两个元素相邻相等且都>3
没了