STL标准库算法测试 algorithm (二)

头文件algorithm find, find_of, find_if_not, find_end, find_first_of, adjacent_fing, count, count_if, mismatch, equal

#include <iostream>
#include <vector>
#include <algorithm>
//all_of
template <typename T>
class Test_std_algorithm {
public:

	void add_vector_para(const T& a) {
		vec.push_back(a);
	}
	
	//none_of. Return true if prefunction returns false for all elements in the range[first, end) or if range is empty, and false otherwise.
	void test_none_of() {
		if (std::none_of(vec.begin(), vec.end(), [](const T& i) {return i < 0; })) {
			std::cout << "true" << std::endl;
		}
		else
		{
			std::cout << "false" << std::endl;
		}
	}

	//for_each. Applies function to each of the elements in the range [first, last).
	void test_for_each() {
		std::for_each(vec.begin(), vec.end(), [](const T& i) {std::cout << i << std::endl; });
	}

	void test_find(const T& val) {
		it = std::find(vec.begin(), vec.end(), val);
		if (vec.end() != it) {
			std::cout << "found at the position" << ": " << (it - vec.begin()) << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//find_if. Returns an iterator to the first element in the range [first, last) for which predfunction return true.
	//if no_elements is found, the function returns last.
	void test_find_if(bool (*func)(const T& i)) {
		it = std::find_if(vec.begin(), vec.end(), (*func));
		if (vec.end() != it) {
			std::cout << "found at the position" << ": " << (it - vec.begin()) << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//find_if_not. Returns an iterator to the first element in the range [first, last) for which predfunction return false.
	//if no_elements is found, the function returns last.
	void test_find_if_not(bool (*func)(const T& i)) {
		it = std::find_if_not(vec.begin(), vec.end(), (*func));
		if (vec.end() != it) {
			std::cout << "found at the position" << ": " << (it - vec.begin()) << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//在指定搜索范围查找指定序列,返回找到的第一个元素的迭代器
	void test_find_end() {
		T needle[] = { 2,3,4 };
		it = std::find_end(vec.begin(), vec.end(), needle, needle + 3);
		if (it != vec.end()) {
			std::cout << "found at the position " << (it - vec.begin()) << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	void test_find_end(bool (*func)(const T& i, const T& j)) {
		T needle[] = { 4,5,6 };
		it = std::find_end(vec.begin(), vec.end(), needle, needle + 3, (*func));
		if (it != vec.end()) {
			std::cout << "found at the position " << (it - vec.begin()) << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//返回匹配到的第一个元素的迭代器
	void test_find_first_of() {
		T needle[] = { 2,3,4 };
		it = std::find_first_of(vec.begin(), vec.end(), needle, needle + 3);
		if (it != vec.end()) {
			std::cout << "The first match is: " << *it << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	void test_find_first_of(bool (*func)(const T& i, const T& j)) {
		T needle[] = { 4,5,6 };
		it = std::find_first_of(vec.begin(), vec.end(), needle, needle + 3, (*func));
		if (it != vec.end()) {
			std::cout << "The first match is: " << *it << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//Searches the range [first,last) for the first occurrence of two consecutive elements that match,
	//and returns an iterator to the first of these two elements, or last if no such pair is found.
	void test_adjacent_find() {
		it = std::adjacent_find(vec.begin(), vec.end());
		if (it != vec.end()) {
			std::cout << "The first match is: " << *it << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	void test_adjacent_find(bool (*func)(const T& i, const T& j)) {
		it = std::adjacent_find(vec.begin(), vec.end());
		if (it != vec.end()) {
			std::cout << "The first match is: " << *it << std::endl;
		}
		else {
			std::cout << "No element" << std::endl;
		}
	}

	//Returns the number of elements in the range [first,last) that compare equal to val.
	void test_count(const T& val) {
		std::cout << "The number of " << val << " is " << std::count(vec.begin(), vec.end(), val) << std::endl;
	}

	void test_count(const T& val, bool (*func)(const T& i, const T& j)) {
		std::cout << "The number of " << val << " is " << std::count(vec.begin(), vec.end(), val) << std::endl;
	}

	//Reuturn the number of elements in the range [first, last) for whitch pred is true.
	void test_conut_of(bool (*func)(const T& i)) {
		std::cout << "The number of True is " << std::count_if(vec.begin(), vec.end(), (*func)) << std::endl;
	}

	//将范围[first1,last1) 中的元素与从first2开始的范围中的元素进行比较,并返回两个序列中不匹配的第一个元素。
	//该函数返回一个一对每个范围中不匹配的第一个元素的迭代器。一种 一对, 其成员 第一的 和 第二指向两个序列中比较不相等的第一个元素。
	void test_mismatch() {
		T ints[] = { 1,2,11 };
		std::pair<std::vector<T>::iterator, T*> mypair;
		mypair = std::mismatch(vec.begin(), vec.end(), ints);
		std::cout << "First mismatching elements: " << *mypair.first;
		std::cout << " and " << *mypair.second << std::endl;
	}

	void test_mismatch(bool (*func)(const T& i, const T& j)) {
		T ints[] = { 1,2,11 };
		std::pair<std::vector<T>::iterator, T*> mypair;
		mypair = std::mismatch(vec.begin(), vec.end(), ints, (*func));
		std::cout << "First mismatching elements: " << *mypair.first;
		std::cout << " and " << *mypair.second << std::endl;
	}

	//将范围内的元素[first1,last1)与从first2开始的范围内的元素进行比较,如果两个范围内的所有元素都匹配,则返回true。
	void test_equal() {
		T ints[] = { 1,2,3,4,5,6,7,8,9,10 };
		if (std::equal(vec.begin(), vec.end(), ints))
			std::cout << "The contents of both sequences are equal.\n";
		else
			std::cout << "The contents of both sequences differ.\n";
	}

	void test_equal(bool (*func)(const T& i, const T& j)) {
		T ints[] = { 1,2,3 };
		if (std::equal(vec.begin(), vec.end(), ints, (*func)))
			std::cout << "The contents of both sequences are equal.\n";
		else
			std::cout << "The contents of both sequences differ.\n";
	}
private:
	std::vector<T> vec = { 1,2,3,4,5,6,7,8,9,10 };
	typename std::vector<T>::iterator it;
};

int main() {
	Test_std_algorithm<int> test_std;
	//test_std.test_find(5);
	//test_std.test_find_if([](const int& i)->bool {return i > 5; });
	//test_std.test_find_if_not([](const int& i)->bool {return i > 5; });
	//test_std.test_find_end();
	//test_std.test_find_end([](const int& i, const int& j)->bool {return i == j; });
	//test_std.test_find_first_of();
	//test_std.test_find_first_of([](const int& i, const int& j)->bool {return i == j; });
	//test_std.add_vector_para(10);
	//test_std.test_adjacent_find();
	//test_std.test_adjacent_find([](const int& i, const int& j)->bool {return i == j; });
	//test_std.test_count(10);
	//test_std.test_count(10, [](const int& i, const int& j)->bool {return i == j; });
	//test_std.test_conut_of([](const int& i)->bool {return (i > 5); });
	//test_std.test_mismatch();
	//test_std.test_mismatch([](const int& i, const int& j)->bool {return i == j; });
	//test_std.test_equal();
	test_std.test_equal([](const int& i, const int& j)->bool {return i == j; });
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值