日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa

發布時間:2023/12/13 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//版本1 template <class InputIterator,class T> T accumulate(InputIterator first,InputIterator last,T init){for(;first != last; ++first){init += *first; //將每個元素數值累加到init身上}return init; }//版本2 template <class InputIterator,class T,class BinaryOperation> T accumulate(InputIterator first,InputIterator last,T init,BinaryOperation binary_op){for(;first!=last;++first){init = binary_op(init, *first);//對每一個元素執行二元操作}return init; }
  • accumulate用于計算init和[first , last)內部所有元素的總和。需要提供一個init,表示當[first,last)為空的區間仍然可以獲取一個明確定義的數值,如果想獲得[first,last)內所有數值的總和,應將init設為0
  • 二元操作符不必滿足交換律和結合律,是的accumulate的行為順序有著明確的定義:先將init初始化,然后針對[first,last)區間內的每一個迭代器i,依次執行init = init + *i(第一版本) 或者 init = binary_op(init, *i) (第二版本)

使用例子

#include <iostream> //std::cout #include <functional> //std::minuc #include <numeric> //std::accumulateint my_function(int x,int y){return x + 2*y;} struct my_class{int operator()(int x,int y){return x + 3*y;} }my_object;int main(){int init = 100;int numbers[] = {10,20,30}; // std::cout << << std::endl;std::cout << "using default accumulate" << std::endl;std::cout << std::accumulate(numbers,numbers+3,init) << std::endl;std::cout << "using functional's accumulate" << std::endl;std::cout << std::accumulate(numbers,numbers+3,init,std::minus<int>()) << std::endl;std::cout << "using custom function" << std::endl;std::cout << std::accumulate(numbers,numbers+3,init, my_function) << std::endl;std::cout << "using custom object" << std::endl;std::cout << std::accumulate(numbers,numbers+3,init, my_object) << std::endl;}

adjacent_difference

  • 用于計算[first,last)中相鄰元素的差額
  • 將*first 賦值給*result,并針對[first+1,last)內的每個迭代器i 將*i-*(i-1)的數值賦值給*(result+(i-first))
  • 使用就地方式 也就是讓result 等于first 函數為質變算法
  • 存儲第一元素的數值然后存儲后繼元素的差值,便可=可以重建輸入區間的原始內容
  • adjacent_difference 和 partial_sum互為逆運算,如果對區間1 2 3 4 5 執行adjacent_difference結果為1 1 1 1 1,如果對這個結果執行partial_sum 便會得到原始的區間數值為1 2 3 4 5
#include <iostream> //std::cout #include <functional> //std::minuc #include <numeric> //std::adjacent_differencetemplate <class InputIterator,class OutputIterator> OutputIterator adjacent_difference(InputIterator first,InputIterator last,OutputIterator result){if(first != last){typename std::iterator_traits<InputIterator>::value_type val,prev;*result = prev = *first;while (++first != last){val = *first;*++result = val - prev; //*++result = binary_op(val,prev);prev = val;}++result;}return result; }template <class InputIterator,class OutputIterator,class BinaryOperation> OutputIterator adjacent_difference(InputIterator first,InputIterator last,OutputIterator result,BinaryOperation binary_op){if (first!=last){typename std::iterator_traits<InputIterator>::value_type val,prev;*result = prev = *first;while(++first != last){val = *first;*++result = binary_op(val,prev);prev = val;}}return result; }//int my_function(int x,int y){return x + 2*y;} //struct my_class{ // int operator()(int x,int y){ // return x + 3*y; // } //}my_object;int myop(int x,int y){return x+y; }int main(){int val[] = {4,2,3,5,9,11,12};int result[7];std::adjacent_difference(val,val+7,result);std::cout << "using default adjacent_difference" << std::endl;for (int i = 0; i < 7; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl;std::adjacent_difference(val,val+7,result,std::multiplies<int>());std::cout << "using functional operation multiplies" << std::endl;for (int i = 0; i < 7; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl;std::adjacent_difference(val,val+7,result, myop);std::cout << "using custom function" << std::endl;for (int i = 0; i < 7; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl;// using default adjacent_difference // 4 -2 1 2 4 2 1 // using functional operation multiplies // 4 8 6 15 45 99 132 // using custom function // 4 6 5 8 14 20 23}

inner_product

  • 執行兩個序列的一般內積
  • 算法計算 [first1,last1) 和 [first2,first2 + (last1 - first1))的一般內積
  • 需要提供初始數值,用于區間為空的時候得到一個明確定義的結果
#include <iostream> //std::cout #include <functional> //std::minuc #include <numeric> //std::adjacent_differencetemplate <class InputIterator,class OutputIterator,class T> T inner_product(InputIterator first1,InputIterator last1,InputIterator first2,T init){while (first1 != last1){init =init + (*first1) * (*first2);++first1;++first2;}return init; }template <class InputIterator,class OutputIterator,class T,class BinaryOperation1,class BinaryOperation2> T inner_product(InputIterator first1,InputIterator last1,InputIterator first2,T init,BinaryOperation1 binary_op1,BinaryOperation2 binary_op2){//以第一序列的元素個數為依據,將兩個序列都遍歷一遍while (first1 != last1){init = binary_op1(init,binary_op2(*first1,*first2));++first1;++first2;}return init; }int myaccumulator(int x,int y){return x-y;} int myproduct(int x,int y){return x+y;}int main(){int init = 100;int series1[] = {10,20,30};int series2[] = {1,2,3};std::cout << "using default inner_product" << std::endl;std::cout << std::inner_product(series1,series1+3,series2,init); // for (int i = 0; i < 7; ++i) { // std::cout << result[i] << ' '; // }std::cout << std::endl;std::cout << "using functional operations" << std::endl;std::cout << std::inner_product(series1,series1+3,series2,init,std::minus<int>(),std::divides<int>()); // for (int i = 0; i < 7; ++i) { // std::cout << result[i] << ' '; // }std::cout << std::endl;std::cout << "using custom functions" << std::endl;std::cout << std::inner_product(series1,series1+3,series2,init, myaccumulator, myproduct); // for (int i = 0; i < 7; ++i) { // std::cout << result[i] << ' '; // }std::cout << std::endl; // using default inner_product // 240 = 100 + (10*1) + (20*2) + (30*3) // using functional operations // 70 = 100 - (10/1) - (20/2) - (30/3) // using custom functions // 34 = 100 - (10+1) - (20+2) - (30+3) }

partial_sum

  • partial_sum用于計算局部總和,將*first賦值給*result,將*first和*(first+1)的數值的和賦值給*(result+1)
  • 可以轉化為質變算法
  • 函數返回 result + (last - first)
  • 和先前介紹的adjacent_difference互為逆運算

?

#include <iostream> //std::cout #include <functional> //std::minuc #include <numeric> //std::adjacent_differencetemplate <class InputIterator,class OutputIterator> OutputIterator partial_sum(InputIterator first,InputIterator last,OutputIterator result){if (first != last){typename std::iterator_traits<InputIterator>::value_type val = *first;*result = val;while (++first!=last){val = val + *first;*++result = val;}++result;}return result; }template <class InputIterator,class OutputIterator,class BinaryOperation> OutputIterator partial_sum(InputIterator first,InputIterator last,BinaryOperation binary_op,OutputIterator result){if (first != last){typename std::iterator_traits<InputIterator>::value_type val = *first;*result = val;while (++first!=last){val = binary_op(val,*first);*++result = val;}++result;}return result; }//int myaccumulator(int x,int y){return x-y;} //int myproduct(int x,int y){return x+y;}int myop(int x,int y){return x+y+1;}int main(){ // int init = 100;int series1[] = {1,2,3,4,5};int result[5];std::cout << "using default partial_sum" << std::endl;std::partial_sum(series1,series1+5,result);for (int i = 0; i < 5; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl;std::cout << "using functional operation multiplies" << std::endl;std::partial_sum(series1,series1+5,result,std::multiplies<int>());for (int i = 0; i < 5; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl;std::cout << "using custom functions" << std::endl;std::partial_sum(series1,series1+5, result,myop);for (int i = 0; i < 5; ++i) {std::cout << result[i] << ' ';}std::cout << std::endl; // using default partial_sum // 1 3 6 10 15 // using functional operation multiplies // 1 2 6 24 120 // using custom functions // 1 4 8 13 19 }

Power

  • ?計算某數的n冪次方 即自己對自己進行某種運算到達n次
  • 運算類型可以由外界指定,如果指定為乘法那就是 乘冪
  • 目前使用pow替代
//版本1 乘冪 template <class T,class Integer> inline T power(T x,Integer n){return power(x,n,std::multiplies<T>()); }//冪次方 指定為乘法運算,則當n >= 0 返回 x^n //需要滿足結合律 但是不需要滿足交換律 template <class T,class Integer,class MonoidOperation> T power(T x,Integer n,MonoidOperation op){if (n==0){return identity_element(op);} else{while((n&1)==0){n>>=1;x = op(x,x);}T result = x;n >> 1;while(n!=0){x = op(x,x);if ((n&1)!=0)result = op(result,x);n >>= 1;}return result;} }

itoa

  • ?用于設定某個區間的內容,使其內的每一個元素從指定的value數值開始,呈現遞增的狀態
  • 質變算法
  • 在[first,last)區域內填入 value ,value+1,value+2
template <class ForwardIterator,class T> void iota(ForwardIterator first,ForwardIterator last,T value){while(first != last){*first++ = value++;} }

參考鏈接

  • accumulate - C++ Reference
  • adjacent_difference - C++ Reference
  • inner_product - C++ Reference
  • https://www.cplusplus.com/reference/numeric/partial_sum/?kw=partial_sum
  • pow - C++ Reference
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 嘿咻视频在线观看 | av免费片 | 国产黄色片在线观看 | 大号bbwassbigav女 | 草草影院国产第一页 | 手机在线观看日韩av | 欧美丰满一区二区免费视频 | 亚洲网站在线观看 | 神马午夜51 | 日韩在线视频不卡 | 在线天堂中文字幕 | 精品日韩在线播放 | 91av日本 | 亚洲一区偷拍 | 久久国产小视频 | 一级黄色在线播放 | 亚洲专区中文字幕 | 欧美一性一交 | 99热这里只有精品1 亚洲人交配视频 | 无套中出丰满人妻无码 | 久久九精品 | 日韩www| 精品乱| 女~淫辱の触手3d动漫 | 精品aaa| 操一操| 亚洲欧美国产高清 | 欧美香蕉在线 | 国产一级一片免费播放放a 丁香六月色 | 超91在线 | 清冷男神被c的合不拢腿男男 | 三级欧美日韩 | 少妇一级淫片日本 | 日韩精品视频网站 | 720url在线观看免费版 | 尤物视频在线免费观看 | 国产永久免费无遮挡 | 亚洲色成人网站www永久四虎 | 国产免费高清视频 | 久久大香 | 粉嫩精品久久99综合一区 | 可以在线观看的av网站 | 传媒av在线| 久热免费| 老熟妇午夜毛片一区二区三区 | 成年人看的黄色 | av福利在线看 | 欧美日本一区二区 | 成人污在线观看 | 女人扒开屁股让我添 | 污视频免费网站 | 亚洲tv在线观看 | 国产三级免费观看 | 精品婷婷| 中文字幕av在线播放 | 欧美a级网站 | 日美av | 国产在线xx| 中国av免费 | 日韩久久不卡 | 久久va| 不卡一区二区三区四区 | 精东av在线| 精品在线不卡 | www黄色网 | 黄色刺激视频 | 午夜福利啪啪片 | 免费亚洲婷婷 | 精品久久久久久久久久久久久 | 香港三级韩国三级日本三级 | 极品久久久久 | 亚洲精品一二区 | 亚洲码视频 | 又粗又猛又爽又黄少妇视频网站 | 国产性生活片 | 国产一区二区三区四区三区四 | 精品一区二区在线看 | 日韩视频在线免费观看 | 国产剧情精品 | 成人在线免费看片 | 67194成人| 日本免费一区二区三区四区 | 亚洲中文一区二区三区 | 午夜合集 | 欧美大尺度床戏做爰 | 欧美精品久久久久a | 中国女人内谢69xxxxⅹ视频 | 91精品视频免费看 | 国产一区二区三区精品视频 | 国产视频第一区 | 少女国产免费观看 | 日韩二区三区四区 | 日日爱99 | 青青伊人久久 | 97视频免费在线观看 | 午夜亚洲aⅴ无码高潮片苍井空 | 日韩在线视频精品 | 久久精品国产亚洲av麻豆图片 | 丝袜性爱视频 |