STL 之count,count_if,max,max_element,min,min_element和random_shuffle
2015-05-28 16:20
295 查看
count:在指定区间上统计指定值出现的次数。
count_if:条件统计
max:判断两个数值中的较大值
max_element:查找指定区间的最大元素
min:判断两个数值中的较小值
min_element:查找指定区间的最小元素
random_shuffle:用来将指定区间上的元素按随机顺序排列
声明:
#include <algorithm>
template <class inputItr,class Type>
iterator_traits<inputItr>::difference_type count(inputItr first,inputItr last,const Type& value);
template <class inputItr,class unaryPredicate>
iterator_traits<inputItr>::difference_type count_if(inputItr first,inputItr last,unaryPredicate op);
template <class Type>
const Type& max(const Type& aVal, const Type&bVal);
template <class Type,class compare>
const Type& max(const Type& aVal,const Type& bVal,compare comp);
template <class forwardItr>
forwardItr max_element(forwardItr first,forwardItr last);
template <class forwardItr, class compare>
forwardItr max_element(forwardItr first,forwardItr last, compare comp);
template <class Type>
const Type& min(const Type& aVal, const Type&bVal);
template <class Type,class compare>
const Type& min(const Type& aVal,const Type& bVal,compare comp);
template <class forwardItr>
forwardItr min_element(forwardItr first,forwardItr last);
template <class forwardItr, class compare>
forwardItr min_element(forwardItr first,forwardItr last, compare comp);
template <class randomAccessItr>
void random_shuffle(randomAccessItr first, randomAccessItr last);
template <class randomAccessItr, class randomAccessGenerator>
void random_shuffle(randomAccessItr first,randomAccessItr last,randomAccessGenerator rand);
示例代码:
#include <iostream>
#include <list>
#include <string>
#include <numeric>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main() {
char cList[10] = {'Z','a','Z','B','Z','c','D','e','F','Z'};
vector<char> charList(cList,cList+10);
ostream_iterator<char> screen(cout," ");
cout << "charList:" << endl;
copy(charList.begin(),charList.end(),screen);
cout << endl;
// count
int noofzs = count(charList.begin(),charList.end(),'Z');
cout << "count of Z = " << noofzs << endl;
// count_if
int noofupper = count_if(charList.begin(),charList.end(),isupper);
cout << "count of Upper = " << noofupper << endl;
int list[10] = {12,34,56,21,34,78,34,55,12,25};
ostream_iterator<int> screenInt(cout, " ");
cout << "list:" << endl;
copy(list,list+10,screenInt);
cout << endl;
// max_element
int * maxLoc = max_element(list,list+10);
cout << "the Largest element is " << *maxLoc << endl;
// min_element
int * minLoc = min_element(list,list+10);
cout << "the Smallest element is " << *minLoc << endl;
// random_shuffle
random_shuffle(list,list+10);
cout << "List.random_shuffle" << endl;
copy(list,list+10,screenInt);
cout << endl;
return 0;
}
运行结果:
charList:
Z a Z B Z c D e F Z
count of Z = 4
count of Upper = 7
list:
12 34 56 21 34 78 34 55 12 25
the Largest element is 78
the Smallest element is 12
List.random_shuffle
12 34 25 56 12 78 55 21 34 34
count_if:条件统计
max:判断两个数值中的较大值
max_element:查找指定区间的最大元素
min:判断两个数值中的较小值
min_element:查找指定区间的最小元素
random_shuffle:用来将指定区间上的元素按随机顺序排列
声明:
#include <algorithm>
template <class inputItr,class Type>
iterator_traits<inputItr>::difference_type count(inputItr first,inputItr last,const Type& value);
template <class inputItr,class unaryPredicate>
iterator_traits<inputItr>::difference_type count_if(inputItr first,inputItr last,unaryPredicate op);
template <class Type>
const Type& max(const Type& aVal, const Type&bVal);
template <class Type,class compare>
const Type& max(const Type& aVal,const Type& bVal,compare comp);
template <class forwardItr>
forwardItr max_element(forwardItr first,forwardItr last);
template <class forwardItr, class compare>
forwardItr max_element(forwardItr first,forwardItr last, compare comp);
template <class Type>
const Type& min(const Type& aVal, const Type&bVal);
template <class Type,class compare>
const Type& min(const Type& aVal,const Type& bVal,compare comp);
template <class forwardItr>
forwardItr min_element(forwardItr first,forwardItr last);
template <class forwardItr, class compare>
forwardItr min_element(forwardItr first,forwardItr last, compare comp);
template <class randomAccessItr>
void random_shuffle(randomAccessItr first, randomAccessItr last);
template <class randomAccessItr, class randomAccessGenerator>
void random_shuffle(randomAccessItr first,randomAccessItr last,randomAccessGenerator rand);
示例代码:
#include <iostream>
#include <list>
#include <string>
#include <numeric>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main() {
char cList[10] = {'Z','a','Z','B','Z','c','D','e','F','Z'};
vector<char> charList(cList,cList+10);
ostream_iterator<char> screen(cout," ");
cout << "charList:" << endl;
copy(charList.begin(),charList.end(),screen);
cout << endl;
// count
int noofzs = count(charList.begin(),charList.end(),'Z');
cout << "count of Z = " << noofzs << endl;
// count_if
int noofupper = count_if(charList.begin(),charList.end(),isupper);
cout << "count of Upper = " << noofupper << endl;
int list[10] = {12,34,56,21,34,78,34,55,12,25};
ostream_iterator<int> screenInt(cout, " ");
cout << "list:" << endl;
copy(list,list+10,screenInt);
cout << endl;
// max_element
int * maxLoc = max_element(list,list+10);
cout << "the Largest element is " << *maxLoc << endl;
// min_element
int * minLoc = min_element(list,list+10);
cout << "the Smallest element is " << *minLoc << endl;
// random_shuffle
random_shuffle(list,list+10);
cout << "List.random_shuffle" << endl;
copy(list,list+10,screenInt);
cout << endl;
return 0;
}
运行结果:
charList:
Z a Z B Z c D e F Z
count of Z = 4
count of Upper = 7
list:
12 34 56 21 34 78 34 55 12 25
the Largest element is 78
the Smallest element is 12
List.random_shuffle
12 34 25 56 12 78 55 21 34 34
相关文章推荐
- STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- STL常用算法: max_element,min_element, distance以及手动实现.
- STL fill()填充算法以及for_each()算法 取容器中的最大最小值min_element,max_element
- STL之min_element()与max_element()(取容器中的最大最小值)
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- 7、步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- STL_算法_最小值和最大值(min_element、max_element)
- 步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- C++ STL之min_element()与max_element()(取容器中的最大最小值)
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- C++ STL之max_element()/min_element()
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL_算法_最小值和最大值(min_element、max_element)
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- LINQ to SQL语句之 Count/Sum/Min/Max/Avg
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- mysql之avg(),count(),min()/max(),sum()函数
- oracle中的聚合函数count、max、min、sum、avg等等