Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- sprintf
- haskell
- Chrono
- RAII
- c++
- async
- Android
- program
- SHA1
- ChromeTab
- template
- Scala
- coroutines
- ranges
- AES
- Clojure
- Reflect
- kotlin
- design pattern
- Observer
- SHA512
- type_traits
- go
- traits
- sha256
- WebView
- web
- CustomTab
- Functional
- stringprintf
Archives
- Today
- Total
프로그래밍 검색 블로그
C++ ranges accumulate 본문
ranges를 받아서 합을 구한다.
기본
1 2 3 | vector<int> v = view::ints(0, 10); int r = ranges::accumulate(v, 0); cout << r << endl; | cs |
출력: 45
ranges 조건 추가(짝수만 더한다)
1 2 3 4 5 | auto isEven = [](int e) { return (e & 1) == 0; }; vector<int> v = view::ints(0, 10); int r = ranges::accumulate(v | view::filter(isEven) , 0); cout << r << endl; | cs |
출력: 20
기존 stl과 비교
1 2 3 4 5 6 7 8 9 | vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int r = std::accumulate(v.begin(), v.end(), 0, [](int s, int e){ if((e & 1) == 0){ s += e; } return s; }); cout << r << endl; | cs |
'C++ ranges' 카테고리의 다른 글
c++ ranges view::zip_with (0) | 2017.10.16 |
---|---|
C++ ranges view::zip (0) | 2017.10.16 |
C++ ranges action::join (0) | 2017.10.15 |
C++ ranges action::unique (0) | 2017.10.15 |
C++ ranges action::sort (0) | 2017.10.15 |
Comments