2017-08-20 17:26:07
writer:pprp
1、adjacent_find()
下面是源码实现:
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
{
if (first != last)
{
ForwardIterator next=first; ++next;
while (next != last) {
if (*first == *next) // or: if (pred(*first,*next)), for version (2)
return first;
++first; ++next;
}
}
return last;
}
测试:
/*
name : STL的其他用法
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h> using namespace std;
typedef list<int> LIST; bool Equal(int a, int b)
{
return (a - b)% == ?:;
} void print(LIST&q)
{
LIST::iterator it;
for(it = q.begin() ; it != q.end(); it++)
{
cout << *it << " ";
}
cout << endl;
} int main()
{
//adjacent_find 查找相邻元素
LIST l; for(int i = ; i < ; i++)
{
l.push_back(i);
l.push_back(i+);
l.push_back(i+);
l.push_back(i+);
}
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back(); print(l); LIST::iterator it = adjacent_find(l.begin(),l.end()); if(it != l.end())
{
cout << "两个相邻元素相等" << *it << " ";
it++;
cout << *it << endl;
} list <int>::iterator ii = adjacent_find(l.begin(), l.end(), Equal);
if(ii != l.end())
{
cout <<"找出首个两个相邻元素相同"<< *ii << " ";
ii++;
cout << *ii << endl;
} return ;
}
2、find_first_of查找第一个匹配字符串(不推荐使用,查看源代码采用最高复杂度的算法)
/*
name : STL的其他用法:find_first_of
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h> using namespace std; int main()
{
char * s1 = "abcdefu7ghijklmn";
char * s2 = "zyx3yu7ys";
char * i = find_first_of(s1,s1 + strlen(s1),s2,s2 + strlen(s2));
//第一个出现在s2中的字符为 *i
cout << * i << endl;
return ;
}
3、堆排序(有点慢)
/*
name : STL中的堆排序
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector> using namespace std; void print(int x)
{
cout << x << " ";
} int main()
{
vector<int> v;
int tmp;
//每次执行的种子不同,生成的随机数才不相同
srand((int)time(NULL));
//产生10个随机数,记录在vector中
for(int i = ; i < ; i++)
{
tmp = rand() % ;
v.push_back(tmp);
} for_each(v.begin(), v.end(),print);
cout << endl; make_heap(v.begin(),v.end());
sort_heap(v.begin(),v.end()); for_each(v.begin(),v.end(),print);
cout << endl; return ;
}
4、归并算法(合并两个有序的序列)
/*
name : 归并排序,合并两个有序序列
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime> using namespace std;
const int maxn = ;
int a[maxn];
int b[maxn]; void print(int *x,int n)
{
for(int i = ; i < n ; i++)
{
cout << x[i] << " ";
}
cout << endl;
} int main()
{
srand((int)time(NULL));
//产生10个随机数,记录在vector中
for(int i = ; i < ; i++)
{
a[i] = rand()%;
b[i] = rand()%;
}
//升序
sort(a,a+);
sort(b,b+); print(a,);
print(b,); int result[maxn*]; merge(a,a+,b,b+,result,less<int>()); print(result,); //降序
sort(a,a+,greater<int>());
sort(b,b+,greater<int>()); merge(a,a+,b,b+,result,greater<int>()); print(result,); return ;
}
5、binary_search折半查找(用在有序区间中)
bool binary_search(a,a+size,key)
6、includes判断集合包含关系
int a[] = {,,,,};
int b[] = {,,,,,,,,,}; if(includes(a,a+,b,b+))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
7、最值
max(,);
min(,);
//查找线性容器中的最大最小
list <int> :: iterator it = min_element(l.begin(), l.end());
list <int> :: iterator it = max_element(l.begin(), l.end());
//字典序比较大小
lexicographical_compare(s1,s1+len1,s2,s2+len2);
8、组合数生成
/*
name : STL中的堆排序
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime> using namespace std; void print(int a[])
{
for(int i = ; i < ; i++)
{
cout << a[i] << " ";
}
cout << endl;
} int main()
{
int a[] = {,,,,};
while(next_permutation(a,a+))
{
print(a);
}
cout << endl; while(prev_permutation(a,a+))
{
print(a);
} return ;
}