C++中的算法(一):非修改性序列算法

非修改性序列算法

非修改性序列算法不会改变容器中的元素值,也不会改变元素的次序。

void OutToScreen(double& Fli) {
	cout.width(1);
	cout.precision(3);
	cout << fixed<<Fli << " ";
}
template<class T>void FillValue(T& vec, int first, int last) {
	if (last > first) {
		for(int i=first;i<last;i++)
		vec.insert(vec.end(), i);
	}
	else
	{
		cout << "错误!!" << endl;
	}
}
bool relationship(double t1, double t2) {
	double temp = 2 * t1;
	return(t2 == temp);
}
bool AbsLess(double t1, double t2) {
	return(abs(t1) < abs(t2));
}
bool isEven(int elem) {
	return(elem % 2 == 0);
}
#pragma region 非修改性序列算法
//for_each的使用方法
vector<double>myvector;
FillValue(myvector, 0, 10);
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
for_each(myvector.begin(), myvector.end(), Multiple<double>(2));//仿函数排序
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
double sum = for_each(myvector.begin(), myvector.end(), SUM());//仿函数求和
cout << "sum=" << sum << endl;
//count统计数目使用方法
bool isEven(int elem);
int ct = count(myvector.begin(), myvector.end(), 2);//2的个数
cout << "2的个数为:" << ct << endl;
ct = count_if(myvector.begin(), myvector.end(),isEven);
cout << "偶数的个数为:" << ct << endl;
ct = count_if(myvector.begin(), myvector.end(), bind2nd(greater<double>(),2));
cout << "大于的个数为:" << ct << endl;
//最大最小值算法
bool AbsLess(double t1, double t2);
cout << "min=" << *min_element(myvector.begin(), myvector.end()) << endl;
cout << "max=" << *max_element(myvector.begin(), myvector.end()) << endl;
cout << "absmin=" << *min_element(myvector.begin(), myvector.end(),AbsLess) << endl;
cout << "absmax=" << *max_element(myvector.begin(), myvector.end(), AbsLess) << endl;
//pair<vector<double>::iterator, vector<double>::iterator>k;
auto k=minmax_element(myvector.begin(), myvector.end());
cout <<"min=" << *k.first << endl;
cout << "max=" << *k.second << endl;
//搜索算法
myvector.push_back(3);
myvector.push_back(5);
myvector.push_back(6);
myvector.push_back(7);
myvector.push_back(8);
myvector.push_back(2);
myvector.push_back(2);
myvector.push_back(5);
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
vector<double>::iterator pos1;
pos1 = find(myvector.begin(), myvector.end(), 3);
cout << "第一个3的位置:" << *pos1 << endl;
pos1 = find_if(myvector.begin(), myvector.end(), bind2nd(greater<int>(), 3));
cout << "第一个大于3的元素的位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = search_n(myvector.begin(), myvector.end(), 4, 3, greater<int>());
if (pos1 != myvector.end()) {
	cout << "连续4个大于3的元素的起始位置:" << distance(myvector.begin(), pos1) << endl;
}
else
{
	cout << "没有合适的元素" << endl;
}
vector<double>myvector1;
myvector1.push_back(5);
myvector1.push_back(6);
myvector1.push_back(7);
myvector1.push_back(8);
for_each(myvector1.begin(), myvector1.end(), OutToScreen);
cout << endl;
pos1 = search(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的第一个的起始位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = find_end(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的最后一个序列的起始位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = find_first_of(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的第一个的起始位置:" << distance(myvector.begin(), pos1) << endl;
vector<double>::reverse_iterator pos2;//反向迭代器
pos2 = find_first_of(myvector.rbegin(), myvector.rend(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的最后一个序列的起始位置:" << distance(myvector.begin(), pos2.base()) << endl;
pos1 = adjacent_find(myvector.begin(), myvector.end());
cout << "第一组相等元素的起始位置:" << distance(myvector.begin(), pos1) << endl;
//区间比较算法
bool relationship(double t1, double t2);
list<double>L1, L2,L3;
cout << "L1" << endl;
L1.push_back(1);
L1.push_back(2);
L1.push_back(3);
L1.push_back(4);
L1.push_back(5);
for_each(L1.begin(), L1.end(), OutToScreen);
cout << endl;
cout << "L2" << endl;
L2.push_back(2);
L2.push_back(4);
L2.push_back(6);
L2.push_back(8);
L2.push_back(10);
for_each(L2.begin(), L2.end(), OutToScreen);
cout << endl;
cout << "L3" << endl;
L3.push_back(1);
L3.push_back(2);
L3.push_back(4);
L3.push_back(0);
L3.push_back(8);
for_each(L3.begin(), L3.end(), OutToScreen);
cout << endl;
bool equ = equal(L1.begin(),L1.end(),L2.begin());
if (equ) {
	cout << "L1==L2" << endl;
}
else
{
	cout << "L1!=L2" << endl;
}
equ = equal(L1.begin(), L1.end(), L2.begin(),greater<double>());
if (equ) {
	cout << "L1>L2" << endl;
}
else
{
	cout << "L1<L2" << endl;
}
//equ = equal(L1.begin(), L1.end(), L2.begin(),relationship);//无法运行
//if (equ) {
//	cout << "L2=2*L1" << endl;
//}
//else
//{
//	cout << "L2!=2*L1" << endl;
//}
pair<list<double>::iterator, list<double>::iterator>p1;
p1 = mismatch(L1.begin(), L1.end(), L3.begin(), L3.end());//不相等的地方
cout << *p1.first << " " << *p1.second << endl;
p1 = mismatch(L1.begin(), L1.end(), L3.begin(), L3.end(),less_equal<double>());//不小于的地方
cout << *p1.first << " " << *p1.second << endl;

bool b = lexicographical_compare(L1.begin(), L1.end(), L3.begin(), L3.end());//返回是bool
if (b) {
	cout << "L1<L3" << endl;//依据第一个不相等的地方进行判断
}
else
{
	cout << "L1>L3" << endl;
}
#pragma endregion

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Algorithms   本次README修订为算法仓库Algorithms的第100次commit,首先我们庆祝自2016年8月4日本仓库建立以来Dev-XYS在算法学习方面取得的显著进步!   这里有各种算法C++代码,任何人可以在自己的任何程序使用,欢迎大家指出代码的错误以及有待改进的地方。   本仓库内所有代码的授权方式为Unlicense,大家如果使用我的代码开发自己的软件挣了大钱,或是参考我的代码在NOI得了金牌,我都会很高兴的。使用这里的代码之后,你可以自主选择是否公开源代码。总而言之,你可以把这里的代码当作你自己写的一样,无论怎样使用都是被允许的。但是,我不对本仓库内代码的正确负责。大家要是使用我的代码开发软件而导致程序崩溃,或是参考我的代码在考试时出错,请不要向我抱怨。如果你愿意,遇到问题可以在Issues提出来,我们共同解决。我们不赞成Pull Request,因为本仓库主要储存作者已经学习的算法,全部代码均由作者本人负责维护与更新。   以下索引提供了本仓库内算法文名,方便大家查找。更新可能有很长时间的延迟,不保证所有算法的名称都在列表出现。 Index --------------------------Contents-------------------------- --------------------------FileName-------------------------- AC自动机 Aho-Corasick-Automation 单源最短路径(SPFA) Bellman-Ford(Queue-Optimised) 单源最短路径(Bellman-Ford) Bellman-Ford 使用Edmonds-Karp进行二分图匹配 Bigrpah-Matching(Edmonds-Karp) 普通的二叉搜索树 Binary-Search-Tree 广度优先搜索 Breadth-First-Search 冒泡排序 Bubble-Sort 桶排序 Bucket-Sort 组合数的递推求解 Combination(Recursion) 枚举组合 Combination 基本的复数类 Complex-Number 割点 Cut-Vertex 深度优先搜索 Depth-First-Search 堆优化的Dijkstra算法 Dijkstra(Heap-Optimised) 并查集 Disjoint-Set-Union 最大流Edmonds-Karp算法 Edmonds-Karp 欧拉函数 Euler's-Totient-Function 有向图的欧拉回路 Eulerian-Tour(Digraph) 拓展欧几里得算法 Extended-Euclid 简单的快速幂 Fast-Exponentiation 树状数组 Fenwick-Tree 所有结点对之间的最短路径(Floyd) Floyd-Warshall 凸包算法(Graham扫描法) Graham-Scan 辗转相除法求最大公约数 Greatest-Common-Divisor 堆排序 Heap-Sort ISAP算法 Improved-Shortest-Augmenting-Path(Naive) 插入排序 Insertion-Sort 字符串匹配(KMP) Knuth-Morris-Pratt 最小生成树(Kruskal) Kruskal 最近公共祖先(Tarjan) Least-Common-Ancestor(Tarjan) 使用后缀数组求解最长公共子串 Longest-Common-Substring 最长上升子序列(n·log(n)) Longest-Increasing-Subsequence(n·log(n)) 倍增法求最近公共祖先 Lowest-Common-Ancestor(Doubling) 朴素的矩阵乘法 Matrix-Multiplication(Naive) 归并排序 Merge-Sort 最小堆 Min-Heap 乘法逆元 Modular-Multiplicative-Inverse 仅支持单点修改的可持久化线段树(维护区间和值) Persistent-Segment-Tree(Sum) 试除法素数测试 Prime-Check(Naive) 线的素数筛法 Prime-Sieve(Linear) 队列的基本操作 Queue 快速排序的优化版本 Quick-Sort(Extra-Optimised) 快速排序的随机化版本 Quick-Sort(Randomized) 快速排序 Quick-Sort 使用向量叉积判断两个有向线段的时针关系 Segment-Direction 线段树维护区间最大值 Segment-Tree(Maximum) 线段树维护区间最小值 Segment-Tree(Minimum) 线段树维护区间和值 Segment-Tree(Sum) 普通的选择算法 Selection Eratosthenes素数筛法 Sieve-of-Erotosthenes 指针版的单向链表 Singly-Linked-List(Pointer) 跳表 Skip-List ST表 Sparse-Table 伸展树 Splay 博弈论SG函数 Sprague-Grundy 栈的基本操作 Stack 递推法求解无符号第一类斯特林数 Stirling-Number(Cycle,Unsigned,Recursion) 递推法求解第二类斯特林数 Stirling-Number(Subset,Recursion) 倍增法求解后缀数组 Suffix-Array(Doubling) 倍增法求解后缀数组(附带Height数组) Suffix-Array-with-Height(Doubling) 使用Tarjan算法求解强连通分量 Tarjan(Strongly-Connected-Components) 数组版的字典树 Trie(Array) 指针版的字典树 Trie(Pointer)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值