STL算法之accumulate函数学习

Accumulate函数学习、

头文件#include<numeric>

当以迭代器first和last及值init作为参数调用时

accumulate(first,last, init);将把init 和从 first 到last 指向的值进行累加,并返回累加得到的和,但不包括last指向的值。通过实例学习accumulate 函数。

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main()
{
   cout<<"Demonstratint the accumulatefunction."<<endl;
   int x[5] = {2,3,5,7,11};
    vector<int>vectorl(&x[0],&x[5]);
   int sum = accumulate(vectorl.begin(),vectorl.end(),0);
cout<<"sum ="<<sum<<endl;
//accumulate 直接对数组进行操作
   int sum1 = accumulate(&x[0],&x[5],0);
   cout<<"sum1 = "<<sum1<<endl;
double y[5] = {2.1,3.123,5.3,7.0,11.0};
// accumulate 对浮点型数据相加。
   vector<double>vectorl1(&y[0],&y[5]);
   double sum2 = accumulate(vectorl1.begin(),vectorl1.end(),0.0);
   cout<<"sum2 = "<<sum2<<endl;
   return 0;
}

结果是

Demonstratint the accumulate function.

sum = 28

sum1 = 28

sum2 = 28.523

 

accumulate 计算连乘,

#include<iostream>
#include<vector>
#include<cassert>
#include<numeric>
using namespace std;
int mult(int x, int y)
{
   return x * y;
}
int main()
{
   int x[5] = {2, 3, 5, 7, 11};
   vector<int>vectorl(&x[0], &x[5]);
   int product = accumulate(vectorl.begin(), vectorl.end(), 1, mult);
   cout<<"product = "<<product<<endl;
   return 0;
}


结果为

product = 2310

 

这里传递给accumulate 函数一个普通函数 mult ,实际上传递的是该函数的地址。

用multiplies 和 accumulate 求连乘

#include<iostream>
#include<vector>
#include<numeric>
#include<functional>
using namespace std;
int main()
{
   int x[5] = {2, 3, 5, 7, 11};
   vector<int>vectorl(&x[0], &x[5]);
   int product = accumulate(vectorl.begin(), vectorl.end(), 1,multiplies<int>());
   cout<<"product = "<<product<<endl;
   return 0;
}

结果为

product = 2310

通过multiplies<int>() 调用了以int类型实例化的multiplies类的默认构造函数。

multiplies头文件为#include <functional>

一个实例在 http://www.linuxidc.com/Linux/2015-04/116423.htm 搬过来学习。

#include <vector>
#include <numeric>
#include <functional>
#include <iostream>
using namespace std;
int main( )
{
   vector <int> v1, v2( 20 );
   vector <int>::iterator Iter1, Iter2;


   int i;
   for ( i = 1 ; i < 21 ; i++ )
   {
      v1.push_back( i );
   }


   cout << "最初向量v1中个元素的值为:\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;


   // accumulate函数的第一个功能,求和
   int total;
   total = accumulate ( v1.begin ( ) , v1.end ( ) , 0 );


   cout << "整数从1到20的和为: "
        << total << "." << endl;


   // 构造一个前n项和的向量
   int j = 0, partotal;
   for ( Iter1 = v1.begin( ) + 1; Iter1 != v1.end( ) + 1 ; Iter1++ )
   {
      partotal = accumulate ( v1.begin ( ) , Iter1 , 0 );
      v2 [ j ] = partotal;
      j++;
   }


   cout << "前n项和分别为:\n ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl << endl;


   // accumulate函数的第二个功能,计算连乘积
   vector <int> v3, v4( 10 );
   vector <int>::iterator Iter3, Iter4;


   int s;
   for ( s = 1 ; s < 11 ; s++ )
   {
      v3.push_back( s );
   }


   cout << "向量v3的初始值分别为:\n ( " ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")." << endl;


   int ptotal;
   ptotal = accumulate ( v3.begin ( ) , v3.end ( ) , 1 , multiplies<int>( ) );


   cout << "整数1到10的连乘积为: "
        << ptotal << "." << endl;


   // 构造一个前n项积的向量
   int k = 0, ppartotal;
   for ( Iter3 = v3.begin( ) + 1; Iter3 != v3.end( ) + 1 ; Iter3++ ) {
      ppartotal = accumulate ( v3.begin ( ) , Iter3 , 1 , multiplies<int>( ) );
      v4 [ k ] = ppartotal;
      k++;
   }


   cout << "前n项积分别为:\n ( " ;
   for ( Iter4 = v4.begin( ) ; Iter4 != v4.end( ) ; Iter4++ )
      cout << *Iter4 << " ";
   cout << ")." << endl;
}

刚开始学习STL,当做记笔记了!!!



STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值