1.元素计数
- count
- count_if
根据不同的准则对元素进行计数。
#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;
}
![](https://img-my.csdn.net/uploads/201204/25/1335336010_5203.png)
2.最小值和最大值
- min_element(beg ,end)
- min_element(beg ,end, op)
- max_element(beg, end)
- max_element(beg, end, op)
#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;
}
![](https://img-my.csdn.net/uploads/201204/25/1335336739_8360.png)
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)
- find_end(beg, end, searchBeg, searchEnd)
- find_end(beg, end, searchBeg, searchEnd, BinaryPredicate op)
- find_first_of(beg, end, searchBeg, searchEnd)//返回第一个"既在区间[beg, end)中出现,也在区间[searchBeg, searchEnd)中出现"的元素的位置。
- find_first_of(beg, end, searchBeg, searchEnd, BinaryPredicate op)//见find_first_of,将条件换成二元判断式op。
- 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的对应元素。
- 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;
}
![](https://img-my.csdn.net/uploads/201205/01/1335859058_1530.png)