如何使for_each()傳回值? (C/C++) (STL)

來源:互聯網
上載者:User

Abstract
for_each()是STL中少數可以回傳值的algorithm,此範例在展示for_each()此特殊功能與function object可以留住state的特性。

Introduction
function object與global function的差別除了function object可以傳入參數外,還可以不使用static就可以留住state。

一個簡單的需求,想要每印n個數字就換行,並且統計出所有iterator的和,所以function object必須能留住state才知道目前印了幾個數字,且統計sum為多少。

Sample Code

 1/**//* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_state_returnValue.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each to return value and remain state.
 7Release     : 05/13/2007 1.0
 8              05/15/2007 2.0
 9*/
10
11#include <iostream>
12#include <algorithm>
13#include <vector>
14
15using namespace std;
16
17class printElem {
18private:
19  int _n;
20  int _cnt;
21  int _sum;
22  
23public:
24  printElem(int n = 5) : _n(n), _cnt(0), _sum(0) {}
25
26  void operator() (int elem) {
27    ++_cnt;
28    _sum += elem;
29    
30    cout << elem;
31    
32    (_cnt % _n) ? cout << " " : cout << endl;
33  }
34  
35  operator int() {
36    return _sum;
37  }
38};
39
40int main() {
41  vector<int> ivec;
42  
43  for(int i = 0; i != 20; ++i) 
44    ivec.push_back(i);
45    
46  int sum = for_each(ivec.begin(), ivec.end(), printElem(5));
47  
48  cout << "sum is " << sum << endl;
49}

 

執行結果

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
sum is 190

17行

private:
  int _n;
  int _cnt;
  int _sum;

_n為設定幾個字跳行
_cnt統計目前已經印了幾個字
_sum統計目前加總結果

29行

if (_cnt % _n) 
  cout << elem << " ";
else
  cout << elem << endl;

若每印n個字,就加印換行

35行

operator int() {
  return _sum;
}

為了讓for_each()能傳回值,特別改寫operator int(),讓function object能夠傳回值。

46行

int sum = for_each(ivec.begin(), ivec.end(), printElem(5));

這樣for_each()就能風風光光的每n個字就換行,還可以順便加總結果。

Conclusion
STL真的很神奇,以上的程式想一行一行翻成C#還真的做不到呢!!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.