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 ;
}

最新文章

  1. RabbitMQ Config
  2. C/C++语言,自学资源,滚动更新中&hellip;&hellip;
  3. jstatd命令
  4. BZOJ3613 南园满地堆轻絮-二分法
  5. Android串口通信(基于Tiny6410平台)
  6. MVC3 使用NPOI导出excel
  7. 404 Not Find When using Owin with OAuth
  8. Windows 路径问题
  9. [APIO2012]
  10. 基于友善之臂ARM-tiny4412--uboot源码分析
  11. thinkphp5分页传参
  12. openssl 交叉编译
  13. 鸟哥的 Linux 私房菜Shell Scripts篇(四)
  14. 【Linux】阿里云服务器部署--禅道
  15. Linux ssh将命令放入后台
  16. Android开发教程 - 使用Data Binding(二)集成与配置
  17. 基于Vue手写一个下拉刷新
  18. 循序渐进学.Net Core Web Api开发系列【5】:文件上传
  19. BZOJ2468 : [中山市选2010]三核苷酸
  20. Thunder团队--Alpha发布用户报告

热门文章

  1. 简单JS旋转实现转盘抽奖效果
  2. 【转】通过ionice和nice降低shell脚本运行的优先级
  3. mysql一个特殊的条件.字符串除以0的结果.
  4. Ckeditor事件绑定
  5. Redis 搜索引擎优化
  6. UVA10700:Camel trading(栈和队列)
  7. Spring基本功能-扫描与继承
  8. Python 集合(set)的使用总结
  9. Flask之wtforms源码分析
  10. 如何将python3.6软件的py文件打包成exe程序