STL中非修改式序列操作

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool testall_of(int num)
{
	if (num > 0)
		return true;
	return false;
}
bool testany_of(int num)
{
	if (num > 4)
		return true;
	return false;
}
bool testnone_of(int num)
{
	if (num > 10)
		return true;
	return false;
}
bool testfind_if(int num)
{
	if (num > 7)
		return true;
	return false;
}
bool testfind_if_not(int num)
{
	if (num > 7)
		return true;
	return false;
}
bool testfind_end(int a, int b)
{
	return a == b;
}
bool testcount_if(int a)
{
	return a >6;
}
int main()
{
	vector<int> t;
	t.push_back(1);
	t.push_back(3);
	t.push_back(1);
	t.push_back(4);
	t.push_back(5);
	t.push_back(1);
	t.push_back(1);
	t.push_back(8);
	t.push_back(3);
	t.push_back(8);
	vector<int> t1;
	t1.push_back(1);
	t1.push_back(5);
	/*非修改式序列操作*/

	cout << all_of(t.begin(), t.end(), testall_of) << endl;;//判断是否所有元素测试为true(都大于0)
	cout << any_of(t.begin(), t.end(), testany_of) << endl;;//判断是否有元素测试为true(大于4)
	cout << none_of(t.begin(), t.end(), testnone_of)<< endl;//判断是否所有元素测试都为false(都不满足条件)
	cout << *find(t.begin(), t.end(), 5) << endl;//查找某元素首次出现的位置
	cout << *find_if(t.begin(), t.end(), testfind_if) << endl;//查找测试首次为true的位置
	cout << *find_if_not(t.begin(), t.end(), testfind_if_not) << endl;//查找测试不满足首次为true的位置
	cout <<find_end(t.begin(), t.end(),t1.begin(),t1.end()) - t.begin() << endl;//查找最后一个与另一序列匹配的值
	auto itr = find_first_of(t.begin(), t.end(), t1.begin(), t1.end());//返回在第一序列区间内包含第二任意元素的位置
	if(itr!=t.end())
	      cout << itr- t.begin() << endl;
	cout << adjacent_find(t.begin(), t.end()) - t.begin() << endl;//查找第一个与后面元素相等的元素的位置
	cout << count(t.begin(), t.end(), 1) << endl;//返回1的个数
	cout << count_if(t.begin(), t.end(), testcount_if) << endl;//返回大于6的个数
	pair<vector<int> ::iterator, vector<int> ::iterator> mypair;
	mypair = mismatch(t.begin(), t.end(), t1.begin(), t1.end());//返回第一个与另一区间中元素不匹配的元素,返回指向这两个元素的迭代器
	cout << *mypair.first<< *mypair.second<<endl;
	cout << equal(t.begin()+4, t.begin() + 6, t1.begin(), t1.end()) << endl;//查看第二区间是否与第一区间对应元素都匹配
	cout << is_permutation(t.begin() + 4, t.begin() + 6, t1.begin(), t1.end()) << endl;;//若可重新排列第二区间使得第二区间与第一区间对应元素都匹配,则返回true
	cout << search(t.begin(), t.end(), t1.begin(), t1.end()) - t.begin() << endl;
	cout << search_n(t.begin(), t.end(),1,8) - t.begin() << endl;//在第一区间查找第一个与count个value组成的序列匹配的位置
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wonitawonitawonita/article/details/79992021