「std::count if」の版間の差分
提供: C++入門
(ページの作成:「std::count_if とは、第3引数で与えられる単項関数(Unary function)の条件を満たす要素が配列やコンテナにいくつ含まれている...」) |
|||
| 行15: | 行15: | ||
#include <iostream> | #include <iostream> | ||
#include <vector> | #include <vector> | ||
| + | #include <algorithm> | ||
using namespace std; | using namespace std; | ||
bool is_odd (int i) { | bool is_odd (int i) { | ||
| 行44: | 行45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | == count_if2.cpp の例 == | ||
| + | === ソースコード count_if2.cpp === | ||
| + | このサンプルは、[[ラムダ式]]を用いたバージョンです。 | ||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <iostream> | ||
| + | #include <vector> | ||
| + | #include <algorithm> | ||
| + | using namespace std; | ||
| + | bool is_odd (int i) { | ||
| + | return ( (i%2) == 1); | ||
| + | } | ||
| + | int main(int argc, char const* argv[]) | ||
| + | { | ||
| + | vector<int> v; | ||
| + | for (int i = 0; i < 10; i++) { | ||
| + | v.push_back(i); | ||
| + | } | ||
| + | size_t n_count = std::count_if(v.begin(), v.end(), | ||
| + | [](int i)->bool{return ( (i%2) == 1);} | ||
| + | ); | ||
| + | std::cout << n_count << std::endl; | ||
| + | return 0; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === コンパイル === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | clang++ -std=c++11 count_if2.cpp -o count_if2 | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 実行例 === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./count_if2 | ||
| + | 5 | ||
| + | </syntaxhighlight> | ||
== 関連項目 == | == 関連項目 == | ||
| 行50: | 行86: | ||
* [[std::count_if]] | * [[std::count_if]] | ||
* [[C++ライブラリ]] | * [[C++ライブラリ]] | ||
| + | * [[ラムダ式]] | ||
<!-- vim: filetype=mediawiki --> | <!-- vim: filetype=mediawiki --> | ||
2013年12月26日 (木) 19:24時点における最新版
std::count_if とは、第3引数で与えられる単項関数(Unary function)の条件を満たす要素が配列やコンテナにいくつ含まれているか返すテンプレート関数です。
読み方
- std::count_if
- えすてぃーでぃー かうんと いふ
目次
概要
std::countやboost::countでは、コンテナに含まれる特定の要素を数えることができますが、奇数(odd)を数える、といったことはできませんでした。 std::count_ifでは、単項関数を与えることにより、柔軟な条件を指定して、数えることができます。。
count_if1.cpp の例
ソースコード count_if1.cpp
これは、std::vectorの奇数(odd)の数を数える例です。
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool is_odd (int i) { return ( (i%2) == 1); } int main(int argc, char const* argv[]) { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } size_t n_count = std::count_if(v.begin(), v.end(), is_odd); std::cout << n_count << std::endl; return 0; }
コンパイル
g++ count_if1.cpp -o count_if1
実行例
% ./count_if1 5
count_if2.cpp の例
ソースコード count_if2.cpp
このサンプルは、ラムダ式を用いたバージョンです。
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool is_odd (int i) { return ( (i%2) == 1); } int main(int argc, char const* argv[]) { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } size_t n_count = std::count_if(v.begin(), v.end(), [](int i)->bool{return ( (i%2) == 1);} ); std::cout << n_count << std::endl; return 0; }
コンパイル
clang++ -std=c++11 count_if2.cpp -o count_if2
実行例
% ./count_if2 5
関連項目
- boost::count
- std::count
- std::count_if
- C++ライブラリ
- ラムダ式