非变动性算法

1.元素计数

  • count
  • count_if
示例1:
根据不同的准则对元素进行计数。
#include <print.hpp>		//自定义头文件
using namespace std;

bool isEven(int elem)
{
	return elem % 2 ==0;
}
int main()
{
	vector<int> coll;
	INSERT_ELEMENTS(coll,1,9);
	int num;
	PRINT_ELEMENTS(coll,"coll: ");
	num = count(coll.begin(),coll.end(),4);
	cout<<" number of elements equal to 4: "<<num <<endl;
	num = count_if(coll.begin(),coll.end(),isEven);
	cout<<" number of elements with even value: "<<num<<endl;
	num = count_if(coll.begin(),coll.end(),bind2nd(greater<int>(),4));
	cout<<" number of elements greater than 4: "<<num<<endl;
}


2.最小值和最大值

  • min_element(beg ,end)
  • min_element(beg ,end, op)
  • max_element(beg, end)
  • max_element(beg, end, op)
示例1:
#include <print.hpp>		//自定义头文件
using namespace std;

bool absLess(int elem1, int elem2)
{
	return abs(elem1) < abs(elem2);
}
int main()
{
	deque<int> coll;
	INSERT_ELEMENTS(coll,2,8);
	INSERT_ELEMENTS(coll,-3,5);
	PRINT_ELEMENTS(coll);
	cout<<"minnum: "<< *min_element(coll.begin(),coll.end())<<endl;
	cout<<"maxnum: "<< *max_element(coll.begin(),coll.end())<<endl;
	cout<<"minnum of absolute values: "<< *min_element(coll.begin(),coll.end(),absLess)<<endl;
	cout<<"maxnum of absolute values: "<< *max_element(coll.begin(),coll.end(),absLess)<<endl;
}


3.搜寻元素

  • find
  • find_if
  • search_n(beg, end, count, value)//返回区间[beg,end)第一组"连续count个元素值全等于value"的元素位置。
  • search_n(beg, end, count, value, BinaryPredicate op)//返回区间[beg,end)第一组"连续count个元素造成二元判断式op结果为true"的元素位置。op为二元判断式。
搜寻第一个子区间:
  • search(beg, end, searchBeg, searchEnd)//返回在区间[beg,end)内“和区间[searchBeg,searchEnd)完全吻合"的第一个子区间内的第一个元素位置。
  • search(beg, end, searchBeg, searchEnd, BinaryPredicate op)//见第一个search,对应元素必须造成二元判断式的结果为true。(P348)
搜寻最后一个子区间:(见search)
  • find_end(beg, end, searchBeg, searchEnd)
  • find_end(beg, end, searchBeg, searchEnd, BinaryPredicate op)
搜寻某些元素的第一次出现地点(P352)
  • find_first_of(beg, end, searchBeg, searchEnd)//返回第一个"既在区间[beg, end)中出现,也在区间[searchBeg, searchEnd)中出现"的元素的位置。
  • find_first_of(beg, end, searchBeg, searchEnd, BinaryPredicate op)//见find_first_of,将条件换成二元判断式op。
搜寻两个连续且相等的元素(P354)
  • adjacent_find(beg, end) //返回区间[beg, end)中第一对"连续两个相等的元素“之中的第一个元素位置。
  • adjacent_find(beg, end, BinaryPredicate op)

4.区间的比较(p356)

检验相等性
  • equal(beg, end, cmpBeg) //判断区间[beg, end)内的元素是否都和”以cmpBeg开头的区间“内的元素相等。(注:以cmpBeg开头的区间内含足够的元素,即>=[beg,end)内的元素个数。
  • equal(beg, end, cmpBeg, BinaryPredicate op) //
搜寻第一个不同点
  • pair<InputIterator1, InputIterator2> mismatch(beg, end, cmpBeg) //返回区间[beg, end)和"以cmpBeg开头的区间”之中第一组两两相异的对应元素。
  • pair<InputIteartor1, InputIteartor2> mismatch(beg, end, cmpBeg, BinaryPredicate op) //返回使二元判断式op或得false的对应元素。
检验“小于”(P360)
  • bool lexicographical_compare(beg1, end1, beg2, end2) //判断区间[beg1, end1)的元素是否小于区间[beg2, end2)的元素。(按"字典顺序") 
  • bool lexicographical_compare(beg1, end1, beg2, end2, CompFunc op) //按op排序:如果elem1<elem2,则返回true。
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
#include <iostream>
using namespace std;

void printCollection(const list<int>& l)
{
	PRINT_ELEMENTS(l);
}
bool lessForCollection(const list<int>& l1, const list<int>& l2)
{
	return lexicographical_compare(l1.begin(),l1.end(),
		l2.begin(),l2.end());
}
int _tmain(int argc, _TCHAR* argv[])
{
	list<int> c1,c2,c3,c4;
	INSERT_ELEMENTS(c1,1,5);
	c4=c3=c2=c1;
	c1.push_back(7);
	c3.push_back(2);
	c3.push_back(0);
	c4.push_back(2);

	vector<list<int> >	cc;
	cc.push_back(c1);
	cc.push_back(c2);
	cc.push_back(c3);
	cc.push_back(c4);
	cc.push_back(c3);
	cc.push_back(c1);
	cc.push_back(c4);
	cc.push_back(c2);

	for_each(cc.begin(),cc.end(),printCollection);
	cout<<endl;
	sort(cc.begin(),cc.end(),lessForCollection);
	for_each(cc.begin(),cc.end(),printCollection);
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值