範囲の要素をチェックする all_of, any_of, none_of(C++11/C++20)

C++

C++11/C++20 all_of, any_of, none_of 関数について調べました。

all_of: 範囲の全ての要素が条件を満たすか
any_of: 範囲のいずれかの要素が条件を満たすか
none_of: 範囲の全ての要素が条件を満たさないか

使用しないで実装した例

#include <iostream>
#include <vector>

int main(int argc, char* argv[]) {
	std::cout << std::boolalpha;

	std::cout << "// 範囲判定" << std::endl;
	std::vector<int> v = { 1, 2, 3, 4, 5 };
	std::cout << "std::vector<int> v = [0, " << v.size() << "): ";
	for (const auto& x : v) { std::cout << x << (&x != &v.back() ? ", " : "\n"); }

	std::cout << "// 全ての要素が条件を満たすか" << std::endl;
	bool all_of = true;
	for (int i = 0; i < v.size(); ++i) {
		if (v[i] < 3) {
			all_of = false;
			break;
		}
	}
	std::cout << "> all_of = " << all_of << std::endl;

	std::cout << "// いずれかの要素が条件を満たすか" << std::endl;
	bool any_of = false;
	for (int i = 0; i < v.size(); ++i) {
		if (v[i] >= 3) {
			any_of = true;
			break;
		}
	}
	std::cout << "> any_of = " << any_of << std::endl;

	std::cout << "// いずれの要素も条件を満たさないか" << std::endl;
	bool none_of = true;
	for (int i = 0; i < v.size(); ++i) {
		if (v[i] >= 3) {
			none_of = false;
			break;
		}
	}
	std::cout << "> none_of = " << none_of << std::endl;
}
// 範囲判定
std::vector<int> v = [0, 5): 1, 2, 3, 4, 5
// 全ての要素が条件を満たすか
> all_of = false
// いずれかの要素が条件を満たすか
> any_of = true
// いずれの要素も条件を満たさないか
> none_of = false

std::all_of, std::any_of, std::none_of (C++11)

第1,2引数に指定した範囲をチェックします。

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char* argv[]) {
	std::cout << std::boolalpha;

	std::cout << "// 範囲判定" << std::endl;
	std::vector<int> v = { 1, 2, 3, 4, 5 };
	std::cout << "std::vector<int> v = [0, " << v.size() << "): ";
	for (const auto& x : v) { std::cout << x << (&x != &v.back() ? ", " : "\n"); }

	std::cout << "// 全ての要素が条件を満たすか" << std::endl;
	std::cout << "std::all_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::all_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれかの要素が条件を満たすか" << std::endl;
	std::cout << "std::any_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::any_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれの要素も条件を満たさないか" << std::endl;
	std::cout << "std::none_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::none_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;
}
// 範囲判定
std::vector<int> v = [0, 5): 1, 2, 3, 4, 5
// 全ての要素が条件を満たすか
std::all_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> false
// いずれかの要素が条件を満たすか
std::any_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> true
// いずれの要素も条件を満たさないか
std::none_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> false

std::ranges::all_of, std::ranges::any_of, std::ranges::none_of (C++20)

ranges の関数は第1引数に範囲を指定します。第1,2引数に範囲を指定するバージョンも用意されています。

#include <algorithm>
#include <iostream>
#include <vector>

int main(int argc, char* argv[]) {
	std::cout << std::boolalpha;

	std::cout << "//// 範囲判定" << std::endl;
	std::vector<int> v = { 1, 2, 3, 4, 5 };
	std::cout << "std::vector<int> v = [0, " << v.size() << "): ";
	for (const auto& x : v) { std::cout << x << (&x != &v.back() ? ", " : "\n"); }

	std::cout << std::endl;
	std::cout << "// 第1引数に範囲を指定" << std::endl;

	std::cout << "// 全ての要素が条件を満たすか" << std::endl;
	std::cout << "std::ranges::all_of(v, [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::all_of(v, [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれかの要素が条件を満たすか" << std::endl;
	std::cout << "std::ranges::any_of(v, [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::any_of(v, [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれの要素も条件を満たさないか" << std::endl;
	std::cout << "std::ranges::none_of(v, [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::none_of(v, [](int x) { return x >= 3; }) << std::endl;

	std::cout << std::endl;
	std::cout << "// 第1,2引数に範囲を指定" << std::endl;

	std::cout << "// 全ての要素が条件を満たすか" << std::endl;
	std::cout << "std::ranges::all_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::all_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれかの要素が条件を満たすか" << std::endl;
	std::cout << "std::ranges::any_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::any_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;

	std::cout << "// いずれの要素も条件を満たさないか" << std::endl;
	std::cout << "std::ranges::none_of(v.begin(), v.end(), [](int x) { return x >= 3; });" << std::endl;
	std::cout << "> " << std::ranges::none_of(v.begin(), v.end(), [](int x) { return x >= 3; }) << std::endl;
}
//// 範囲判定
std::vector<int> v = [0, 5): 1, 2, 3, 4, 5

// 第1引数に範囲を指定
// 全ての要素が条件を満たすか
std::ranges::all_of(v, [](int x) { return x >= 3; });
> false
// いずれかの要素が条件を満たすか
std::ranges::any_of(v, [](int x) { return x >= 3; });
> true
// いずれの要素も条件を満たさないか
std::ranges::none_of(v, [](int x) { return x >= 3; });
> false

// 第1,2引数に範囲を指定
// 全ての要素が条件を満たすか
std::ranges::all_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> false
// いずれかの要素が条件を満たすか
std::ranges::any_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> true
// いずれの要素も条件を満たさないか
std::ranges::none_of(v.begin(), v.end(), [](int x) { return x >= 3; });
> false

サポートするコレクション

vector, list, map などの STL コレクション、配列をサポートします。

#include <algorithm>
#include <iostream>
#include <list>
#include <map>

int main(int argc, char* argv[]) {
	std::cout << std::boolalpha;
	{
		// C++11
		int a[] = { 1, 2, 3, 4, 5 };
		std::cout << "> " << std::all_of(a, a + 5, [](int x) { return x >= 3; }) << std::endl;

		std::list<int> l = { 1, 2, 3, 4, 5 };
		std::cout << "> " << std::all_of(l.begin(), l.end(), [](int x) { return x >= 3; }) << std::endl;

		std::map<int, std::string> m = { {1, "aaa"}, {2, "bbb"}, {3, "ccc"}, {4, "ddd"}, {5, "eee"} };
		std::cout << "> " << std::all_of(m.begin(), m.end(), [](const auto& x) { return x.first >= 3; }) << std::endl;
	}
	{
		// C++20
		int a[] = { 1, 2, 3, 4, 5 };
		std::cout << "> " << std::ranges::all_of(a, [](int x) { return x >= 3; }) << std::endl;

		std::list<int> l = { 1, 2, 3, 4, 5 };
		std::cout << "> " << std::ranges::all_of(l, [](int x) { return x >= 3; }) << std::endl;

		std::map<int, std::string> m = { {1, "aaa"}, {2, "bbb"}, {3, "ccc"}, {4, "ddd"}, {5, "eee"} };
		std::cout << "> " << std::ranges::all_of(m, [](const auto& x) { return x.first >= 3; }) << std::endl;
	}
}

コメント

タイトルとURLをコピーしました