一 概述
C++ 标准库中提供了很多算法,定义于头文件 < algorithm >,少量定义于< numeric >。本文主要探究以下用于 数值区间 的算法,它们定义于< numeric >:
std::accumulate 对一个范围内的元素求和std::inner_product 计算两个范围的元素的内积std::adjacent_difference 计算范围内各相邻元素之间的差std::partial_sum 计算范围内元素的部分和
二 辅助函数
template < typename C>
void print ( const std:: string& pre, C container) {
std:: cout << pre;
std:: copy ( container. begin ( ) , container. end ( ) ,
std:: ostream_iterator< int > ( std:: cout, " " ) ) ;
std:: cout << std:: endl;
}
三 std::accumulate
template < class InputIt , class T >
T accumulate ( InputIt first, InputIt last, T init ) ; ( 1 ) ( until C++ 20 )
template < class InputIt , class T >
constexpr T accumulate ( InputIt first, InputIt last, T init ) ; ( 1 ) ( since C++ 20 )
template < class InputIt , class T , class BinaryOperation >
T accumulate ( InputIt first, InputIt last, T init,
BinaryOperation op ) ; ( 2 ) ( until C++ 20 )
template < class InputIt , class T , class BinaryOperation >
constexpr T accumulate ( InputIt first, InputIt last, T init,
BinaryOperation op ) ; ( 2 ) ( since C++ 20 )
if ( 1 ) {
std:: vector< int > vc;
fill ( vc, 1 , 6 ) ;
print ( "vc: " , vc) ;
std:: cout << "accumulate vc: " << std:: accumulate ( vc. begin ( ) , vc. end ( ) , 0 ) << std:: endl;
}
vc: 1 2 3 4 5 6
accumulate vc: 21
四 std::inner_product
template < class InputIt1 , class InputIt2 , class T >
T inner_product ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init ) ; ( 1 ) ( C++ 20 前)
template < class InputIt1 , class InputIt2 , class T >
constexpr T inner_product ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init ) ; ( 1 ) ( C++ 20 起)
template < class InputIt1 , class InputIt2 , class T ,
class BinaryOperation1 , class BinaryOperation2 >
T inner_product ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOperation1 op1,
BinaryOperation2 op2 ) ; ( 2 ) ( C++ 20 前)
template < class InputIt1 , class InputIt2 , class T ,
class BinaryOperation1 , class BinaryOperation2 >
constexpr T inner_product ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOperation1 op1,
BinaryOperation2 op2 ) ; ( 2 ) ( C++ 20 起)
if ( 1 ) {
std:: vector< int > vc1;
fill ( vc1, 2 , 4 ) ;
std:: vector< int > vc2;
fill ( vc2, 3 , 5 ) ;
print ( "vc1: " , vc1) ;
print ( "vc2: " , vc2) ;
std:: cout << "inner_product vc1 and vc2: "
<< std:: inner_product ( vc1. begin ( ) , vc1. end ( ) , vc2. begin ( ) , 0 )
<< std:: endl;
}
vc1: 2 3 4
vc2: 3 4 5
inner_product vc1 and vc2: 38
五 std::adjacent_difference
template < class InputIt , class OutputIt >
OutputIt adjacent_difference ( InputIt first, InputIt last,
OutputIt d_first ) ; ( 1 ) ( C++ 20 前)
template < class InputIt , class OutputIt >
constexpr OutputIt adjacent_difference ( InputIt first, InputIt last,
OutputIt d_first ) ; ( 1 ) ( C++ 20 起)
template < class ExecutionPolicy , class ForwardIt1 , class ForwardIt2 >
ForwardIt2 adjacent_difference ( ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 last, ForwardIt2 d_first ) ; ( 2 ) ( C++ 17 起)
template < class InputIt , class OutputIt , class BinaryOperation >
OutputIt adjacent_difference ( InputIt first, InputIt last,
OutputIt d_first, BinaryOperation op ) ; ( 3 ) ( C++ 20 前)
template < class InputIt , class OutputIt , class BinaryOperation >
constexpr OutputIt adjacent_difference ( InputIt first, InputIt last,
OutputIt d_first, BinaryOperation op ) ; ( 3 ) ( C++ 20 起)
template < class ExecutionPolicy , class ForwardIt1 , class ForwardIt2 , class BinaryOperation >
ForwardIt2 adjacent_difference ( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, BinaryOperation op ) ; ( 4 ) ( C++ 17 起)
if ( 1 ) {
std:: vector< int > vc{ 1 , 3 , 5 , 7 } ;
print ( "vc: " , vc) ;
std:: adjacent_difference ( vc. begin ( ) , vc. end ( ) , vc. begin ( ) ) ;
std:: cout << "adjacent_difference vc: " << std:: endl;
print ( "vc: " , vc) ;
}
vc: 1 3 5 7
adjacent_difference vc:
vc: 1 2 2 2
六 std::partial_sum
template < class InputIt , class OutputIt >
OutputIt partial_sum ( InputIt first, InputIt last, OutputIt d_first ) ; ( 1 ) ( C++ 20 前)
template < class InputIt , class OutputIt >
constexpr OutputIt partial_sum ( InputIt first, InputIt last, OutputIt d_first ) ; ( 1 ) ( C++ 20 起)
template < class InputIt , class OutputIt , class BinaryOperation >
OutputIt partial_sum ( InputIt first, InputIt last, OutputIt d_first,
BinaryOperation op ) ; ( 2 ) ( C++ 20 前)
template < class InputIt , class OutputIt , class BinaryOperation >
constexpr OutputIt partial_sum ( InputIt first, InputIt last, OutputIt d_first,
BinaryOperation op ) ; ( 2 ) ( C++ 20 起)
if ( 1 ) {
std:: vector< int > vc{ 1 , 3 , 5 , 7 } ;
print ( "vc: " , vc) ;
std:: partial_sum ( vc. begin ( ) , vc. end ( ) , vc. begin ( ) ) ;
std:: cout << "partial_sum vc: " << std:: endl;
print ( "vc: " , vc) ;
}
vc: 1 3 5 7
partial_sum vc:
vc: 1 4 9 16
七 github
所有Demo 已上传github cplusplus numeric_algotithm.cpp
八 参考