STL algorithm -2

...好累啊.一天没出去.

去学校拿个快递跑会步吧...

晚上回来把剩下的算法写完再整理一下吧..

#define pause system("pause");
#define fence puts("-------------");
#define line puts("")
#define linefence puts("\n-------------");
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;


int main()
{
	//-adjacent_find(first,last(,cmp))
	//非变易算法(Non-mutating algorithms)
	//是一组不破坏操作数据的模板函数,
	//用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。
	//功能:查找一个迭代器区间内,相等或满足自定义条件的第一个邻近元素对
	//返回:找到的一个元素对的第一个元素的位置,
	//     找不到返回last位置
	vector<int>a;
	a.push_back(0);
	a.push_back(1);
	a.push_back(1);
	a.push_back(2);
	auto j1 = adjacent_find(a.begin(), a.end());
	auto j2 = adjacent_find(a.begin(), a.end(), [](int n, int m) { return n < m; });
	cout << *j1 << " " << *j2;
	linefence;
	//-binary_search
	//功能:查找一个有序迭代器区间内是否存在某个元素
	//返回:true false
	auto bs = binary_search(a.begin(), a.end(), 1);
	cout << bs;
	linefence;
	//-copy
	//功能:将一个迭代器区间的内容复制到另一个迭代器区间
	//(利用这个函数进行输出输入见上一篇博客iterator)
	//返回:已被复制元素区间的结束位置
	vector<int>c(4);
	auto tc = copy(a.begin(), a.end(), c.begin());
	for (auto i : c)cout << i << " ";
	linefence;
	//-copy_backward
	//功能:复制一个迭代区间,从末尾位置开始复制
	//返回:c的first位置
	auto tc2 = copy_backward(a.begin(), a.end(), c.end());
	for (auto i : c)cout << i << " ";
	linefence;
	//-count
	//功能:计算区间内值为value的总个数
	//返回:元素个数
	auto value = 1;
	for (auto i : a)cout << i << " "; line;
	auto ca = count(a.begin(), a.end(), value);
	cout << ca;
	linefence;
	//-count_if
	//功能:计算区间内满足条件的元素总个数
	//返回:元素个数
	cout << count_if(a.begin(), a.end(), [](int i) {return i > 0; });
	linefence;
	//-equal
	//功能:判断两个容器元素是否相等
	//返回:bool
	cout << equal(a.begin(), a.end(), c.begin()); line;
	cout << equal(a.begin(), a.end(), c.begin(), [](int a, int b) {return abs(a) == abs(b); });
	linefence;
	//-equal_range
	//功能:查找区间内数值的第一次出现与最后一次出现的后一位
	//返回:pair
	auto epair = equal_range(a.begin(), a.end(), 1);
	cout << *epair.first << " " << *epair.second;
	linefence;
	//-fill
	//功能:用value填充区间
	vector<int>vtt(4);
	fill(vtt.begin(), vtt.end(), 123);
	//-for_each
	//功能:遍历区间执行操作
	for_each(vtt.begin(), vtt.end(), [](int i) {if (i == 123)cout << "z"; });
	linefence;
	//-find/find_if
	//功能:查找某个元素在区间内的位置
	//返回:第一个相等元素的迭代器/符合规则的迭代器
	auto zit = find(a.begin(), a.end(), 1);
	cout << *(zit);
	linefence;
	//-generate/generate_n
	//功能:给指定区间元素赋值
	generate(vtt.begin(), vtt.end(), []() {return 123; });
	for_each(vtt.begin(), vtt.end(), [](int i) {if (i == 123)cout << "z"; });
	linefence;
	//-includes
	//功能:一个区间是否是另一个区间的子集
	//返回:bool
	cout << includes(a.begin(), a.end(), c.begin(), c.end());
	linefence;
	//-inplace_merge
	//功能:将两个连接在一起的排序序列[first, middle)和[middle, last)结合成单一序列并保持有序
	//void
	c.resize(8);
	inplace_merge(c.begin(), c.begin() + 4, c.end());
	for (auto i : c)cout << i << " ";
	c.resize(4);
	for (auto &i : c)i = 1;
	linefence;
	//-iter_swap
	//功能:交换两个迭代器的元素的值
	iter_swap(c.begin() + 1, c.begin() + 2);
	for (auto &i : c)i = i + 1;
	linefence;
	//-lexicographical_compare
	//功能:按照字典序比较序列
	string str1 = "123", str2 = "132";
	cout << lexicographical_compare(str1.begin(), str1.end(), str2.begin(), str2.end());
	//-lower_bound/upper_bound
	//功能:找到第一个不小于value的迭代器的值/最后一个
	//返回:迭代器
	auto sit = lower_bound(str1.begin(), str1.end(), 2);
	auto sit2 = upper_bound(str1.begin(), str1.end(), 2);
	cout << *sit << " " << *sit2;
	linefence;
	//-make_heap/pop_heap/push_heap
	//功能:建立堆
	vector<int>heap(a);
	make_heap(heap.begin(), heap.end());
	heap.push_back(123);
	push_heap(heap.begin(), heap.end());
	for (auto i : heap)cout << i << " ";
	pop_heap(heap.begin(), heap.end());
	linefence;
	//-max/min
	//功能:找到最大值的迭代器
	auto Max = max({1,2,3,3,3,6,5,4,3,1});
	cout << Max;
	linefence;
	//-max_element/min_element
	//功能:找到最大元素
	auto maxe = max_element(a.begin(), a.end());
	cout << *maxe;
	linefence;
	//-transform
	//功能:
	transform(a.begin(), a.end(), c.begin(), [](int i) {return ++i; });
	for (auto i : c)cout << i << " ";
	linefence;
	transform(a.begin(), a.end(), c.begin(), c.begin(), [](int i, int j) {return i + j; });
	for (auto i : c)cout << i << " ";
	linefence;
	pause

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值