提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
继续学习STL算法,本文主要讲:
random_shuffle:随机洗牌,打乱位置。
count:统计某个值的个数,比如说:vector中值为8的元素有几个呢?
count_if:统计满足某个条件的元素的个数,比如说:vector中值大于9的元素有几个呢?
min_element:得到最小的元素的位置,它返回一个迭代器,指向最小的那个元素。
max_element:得到最大的元素的位置,它返回一个迭代器,指向最大的那个元素。
accumulate:计算区间的元素的和,第三个参数值为总和的初始值。
for_each:对区间的元素执行一个通用函数,通用函数把当前元素作为参数,并且不能修改它。
transform:对区间的每一个元素执行一个通用函数,通用函数把当前元素作为参数,并且不能修改它,然后返回变换的结果。第三个参数指出存放变换结果的位置,可以和第一个参数相同。
提示:以下是本篇文章正文内容,下面案例可供参考
一、源代码
// Fig22_29_STL_Alg_shuffle_count_minmax_accu_for_transform.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <algorithm>
#include <numeric>//accumulate is defined here
#include <vector>
#include <iterator>//ostream_iterator
using namespace std;
bool greater9(int);//prototype
void outputSquare(int);//output square of a value
int calculateCube(int);//caculate cube of a value
int main()
{
//std::cout << "Hello World!\n";
const int SIZE = 10;
int a1[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
int a1_1[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(a1, a1 + SIZE);//copy of a1
vector<int> v_1(a1_1, a1_1 + SIZE);//copy of a1
ostream_iterator<int> output(cout, " ");
cout << "Vector v before random_shuffle: \n";
copy(v.begin(), v.end(), output);
random_shuffle(v.begin(),v.end());//shuffle element of v
random_shuffle(v_1.begin(), v_1.end());//shuffle element of v_1
cout << "\nVector v after random_shuffle: \n";
copy(v.begin(), v.end(), output);//多次运行,每一次都是:9 2 10 3 1 6 8 4 5 7
//多次运行,每一次都是:9 2 10 3 1 6 8 4 5 7
cout << "\nBefore shuffle,Vector v_1 is same with v,so after random_shuffle: \n";
copy(v_1.begin(), v_1.end(), output);//原始数据一样的话,洗牌后结果却是不一样的
cout << "\nVector v now is:9 2 10 3 1 6 8 4 5 7 \n";
random_shuffle(v.begin(), v.end());//shuffle again
cout << "\nVector v after shuffle again is:\n";
copy(v.begin(), v.end(), output);//多次运行,每一次都是:10 7 6 3 4 9 8 5 1 2
//多次运行,每一次都是:10 7 6 3 4 9 8 5 1 2
/////////////////////
int a2[SIZE] = { 100,2,8,1,50,3,8,8,9,10 };
vector<int> v2(a2, a2 + SIZE);//copy of a2
cout << "\nVector v2 contains: \n";
copy(v2.begin(), v2.end(), output);
//count number of element in v2 with value 8
int result = count(v2.begin(),v2.end(),8);
cout << "\nNumber of elements matching 8:" << result;//3
//count number of element in v2 that greater than 9
int result2 = count_if(v2.begin(), v2.end(), greater9);
cout << "\nNumber of elements greater than 9:" << result2;//3
//locate minimum element in v2
cout << "\nMinimum element in v2 is:"<<*(min_element(v2.begin(),v2.end()));
//locate maximum element in v2
cout << "\nMaximum element in v2 is:" << *(max_element(v2.begin(), v2.end()));
cout << "\nNow v is:" << endl;
copy(v.begin(), v.end(), output);
//caculate sum of element in v
cout << "\nThe total of the element in Vector v is:" << accumulate(v.begin(), v.end(),0);
//caculate square of the element in v
cout << "\nThe square of every int in Vector v is:\n";
for_each(v.begin(),v.end(),outputSquare );
vector<int> cubes(SIZE);//实例化vector cubes
//caculate cube of each element in Vector v;place result in cubes
transform(v.begin(),v.end(),cubes.begin(),calculateCube);
cout << "\nThe cube of every int in Vector v is:\n";
copy(cubes.begin(),cubes.end(),output);
cout << endl;
}
bool greater9(int x)
{
return x > 9;
}
//output square of argument
void outputSquare(int value)
{
cout << value * value << ' ';
}
//return cube of argument
int calculateCube(int value)
{
return value * value * value;
}
运行结果:
Vector v before random_shuffle:
1 2 3 4 5 6 7 8 9 10
Vector v after random_shuffle:
9 2 10 3 1 6 8 4 5 7
Before shuffle,Vector v_1 is same with v,so after random_shuffle:
7 5 10 8 4 1 2 9 6 3
Vector v now is:9 2 10 3 1 6 8 4 5 7
Vector v after shuffle again is:
10 7 6 3 4 9 8 5 1 2
Vector v2 contains:
100 2 8 1 50 3 8 8 9 10
Number of elements matching 8:3
Number of elements greater than 9:3
Minimum element in v2 is:1
Maximum element in v2 is:100
Now v is:
10 7 6 3 4 9 8 5 1 2
The total of the element in Vector v is:55
The square of every int in Vector v is:
100 49 36 9 16 81 64 25 1 4
The cube of every int in Vector v is:
1000 343 216 27 64 729 512 125 1 8
总结
random_shuffle:随机洗牌,打乱位置。
count:统计某个值的个数,比如说:vector中值为8的元素有几个呢?
count_if:统计满足某个条件的元素的个数,比如说:vector中值大于9的元素有几个呢?
min_element:得到最小的元素的位置,它返回一个迭代器,指向最小的那个元素。
max_element:得到最大的元素的位置,它返回一个迭代器,指向最大的那个元素。
accumulate:计算区间的元素的和,第三个参数值为总和的初始值。
for_each:对区间的元素执行一个通用函数,通用函数把当前元素作为参数,并且不能修改它。
transform:对区间的每一个元素执行一个通用函数,通用函数把当前元素作为参数,并且不能修改它,然后返回变换的结果。第三个参数指出存放变换结果的位置,可以和第一个参数相同。