C++ の STL アルゴリズム
-
C++ で汎用ベクトルの範囲をソートするには
std::sortアルゴリズムを使う -
C++ で要素の順序を逆にするために
std::reverseアルゴリズムを使用する -
std::accumulateアルゴリズムを使用して、C++ の範囲内の要素の合計を計算する -
std::countアルゴリズムを使用して、C++ の特定の基準を満たす要素の数をカウントする
この記事では、C++ の STL アルゴリズムライブラリのいくつかの関数を紹介します。
C++ で汎用ベクトルの範囲をソートするには std::sort アルゴリズムを使う
std::sort は、STL で最も利用されているアルゴリズムの 1つです。複数のオーバーロードがあり、最も単純なものは LegacyRandomAccessIterator 要件を満たす 2つのイテレータを受け入れ、要素を降順ではない順序でソートします。後者は、等しい要素の順序が保持されることが保証されていないために言われます。
std::sort は一般的に vector の範囲で使用されます。次のコードスニペットは、そのような使用法を示しています。アルゴリズムは、オプションで比較関数を使用できます。この関数は、要素のペアを評価し、それに応じて範囲を並べ替えるために使用されます。
次の例は、STL 関数オブジェクト-std::greater が比較関数として渡される 1 行を示しています。または、カスタム関数オブジェクトを定義するか、ラムダ式を std::sort の 3 番目のパラメーターとして直接指定することもできます。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
template <typename T>
void printRange(std::vector<T> v) {
for (const auto &item : v) {
cout << item << ", ";
}
cout << endl;
}
int main() {
std::vector<int> v1 = {1, 3, 5, 11, 9, 10, 13, 20};
std::sort(v1.begin(), v1.end());
printRange(v1);
std::sort(v1.begin(), v1.end(), std::greater<>());
printRange(v1);
std::sort(v1.begin(), v1.end(), [](int a, int b) { return (a - 2) != b; });
printRange(v1);
return EXIT_SUCCESS;
}
出力:
1, 3, 5, 9, 10, 11, 13, 20,
20, 13, 11, 10, 9, 5, 3, 1,
1, 3, 5, 9, 10, 11, 13, 20
C++ で要素の順序を逆にするために std::reverse アルゴリズムを使用する
std::reverse は、vector、list、deque などのシーケンスコンテナの内容を逆にするために使用できます。この関数は 2つのイテレーターパラメーターを受け入れ、任意のジェネリック型で操作できます。次のコードサンプルは、整数のベクトルと文字列のリストが逆になっている 2つのシナリオを示しています。また、std::reverse アルゴリズムを呼び出す前に、sort メンバー関数を使用して list オブジェクトをソートします。std::sort アルゴリズムは std::list コンテナでは機能しないことに注意してください。
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::list;
using std::string;
using std::vector;
template <typename T>
void printRange(std::vector<T> v) {
for (const auto &item : v) {
cout << item << ", ";
}
cout << endl;
}
template <typename T>
void printRange(std::list<T> v) {
for (const auto &item : v) {
cout << item << ", ";
}
cout << endl;
}
int main() {
std::vector<int> v1 = {1, 3, 5, 11, 9, 10, 13, 20};
std::list<string> l1 = {"htop", "wtop", "rtop", "ktop", "ktop", "ptop"};
std::reverse(v1.begin(), v1.end());
printRange(v1);
l1.sort();
std::reverse(l1.begin(), l1.end());
printRange(l1);
return EXIT_SUCCESS;
}
出力:
20, 13, 10, 9, 11, 5, 3, 1,
wtop, rtop, ptop, ktop, ktop, htop,
std::accumulate アルゴリズムを使用して、C++ の範囲内の要素の合計を計算する
std::accumulate は、指定された範囲のすべての要素に対して一般的な算術演算を実行するために利用できる数値アルゴリズムの一部です。この場合、指定されたアルゴリズムは、範囲内の各要素の合計を計算します。std::accumulate には 2つのオーバーロードがあり、最初のオーバーロードは範囲自体を示す 2つのイテレータと、合計の開始値を表す値 init を取ります。2 番目のオーバーロードは、オプションで、合計の代わりに適用される 4 番目のパラメーターとして関数オブジェクトを受け取ることができます。
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
std::vector<int> v1 = {1, 3, 5, 11, 9, 10, 13, 20};
auto sum = std::accumulate(v1.begin(), v1.end(), 0);
cout << "Sum of 'v1' vector = " << sum << endl;
sum = std::accumulate(v1.begin(), v1.end(), 1, std::multiplies());
cout << "Accumulate of 'v1' vector = " << sum << endl;
return EXIT_SUCCESS;
}
出力:
Sum of 'v1' vector = 72
Accumulate of 'v1' vector = 3861000
std::count アルゴリズムを使用して、C++ の特定の基準を満たす要素の数をカウントする
std::count 関数は、指定された範囲内の特定の要素をカウントするための便利な方法です。つまり、範囲イテレータと value を渡して、指定された値に等しいすべての要素に一致させることができます。別のオーバーロードは、有効な一致の評価を返す単項述語を受け入れることができ、アルゴリズムはそれに応じてカウント数を取得します。次の例では、vector オブジェクトの偶数をカウントするラムダ式を指定しました。
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
std::vector<int> v1 = {1, 3, 5, 11, 9, 10, 13, 20};
auto count_10 = std::count(v1.begin(), v1.end(), 10);
cout << count_10 << " occurrences of number 10" << endl;
auto count_even =
std::count_if(v1.begin(), v1.end(), [](int i) { return i % 2 == 0; });
cout << count_even << " even numbers in 'v1' vector" << endl;
return EXIT_SUCCESS;
}
出力:
1 occurrences of number 10
2 even numbers in 'v1' vector
