C++大学教程(第七版)Chapter22.5标准模板库-算法:Fig22_30.cpp

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

继续学习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:对区间的每一个元素执行一个通用函数,通用函数把当前元素作为参数,并且不能修改它,然后返回变换的结果。第三个参数指出存放变换结果的位置,可以和第一个参数相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++大学教程(第七版)的示例源代码,详细而规范。 例:// Fig. 2.5: fig02_05.cpp // Addition program that displays the sum of two integers. #include <iostream> // allows program to perform input and output // function main begins program execution int main() { // variable declarations int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2 std::cout << "Enter first integer: "; // prompt user for data std::cin >> number1; // read first integer from user into number1 std::cout << "Enter second integer: "; // prompt user for data std::cin >> number2; // read second integer from user into number2 sum = number1 + number2; // add the numbers; store result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line } // end function main /************************************************************************** * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值