C++遍历查找常见方法在容器中的使用

#include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#include<stack>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<functional>容器及方法所需头文件

遍历:

1.for_each

void printf01(int val)
{
	cout << val;
}
class printf02//函数对象
{
public:
	void operator()(int val)
	{
		cout << val<<"  ";
	}
};
void test2()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);//插入十个数

	}
	//三种打印方法
	for_each(v.begin(), v.end(), printf01);//for_each经常使用,熟练掌握
	for_each(v.begin(), v.end(), printf02());
	for (vector<int>::iterator it = v.begin();it != v.end();it++)
			{
				cout << *it << "";
			}
}

2.transform

class Transform//函数对象
{
public:
	int operator()(int v)
{
	return v;
}
};
void test3()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	cout << endl;
	vector<int>vTarget;
	vTarget.resize(v.size()); //给第二个容器空间大小
	transform(v.begin(), v.end(), vTarget.begin(), Transform());
	for_each(vTarget.begin(), vTarget.end(), printf02());
}

查找:

1.find

class Person
{
public:
	string name;
	int age;
	Person(string name, int age)
	{
		this->age = age;
		this->name = name;
	}
	bool operator==( Person p1)
	{
		if (p1.age == this->age && p1.name == this->name)
		{
			return true;
		}
		else
			return false;
	}
};
void test4()
{
	vector<Person>v;
	Person p1("aaa", 10);
	Person p3("ccc", 30);
	Person p2("bbb", 20);
	Person p4("ddd", 20);
	v.push_back(p1);
	v.push_back(p4);
	v.push_back(p3);
	v.push_back(p2);
	vector<Person>::iterator it=find(v.begin(), v.end(), p4);
	if (it != v.end())
	{
		cout << "找到了" << endl;
	}
	else if (it == v.end())
	{
		cout << "没找到" << endl;
	}

	
}

2.find_if

class GreatFive//寻找容器中大于五的数
{
public:
	bool operator()(Person p)
	{
		return p.age > 5;
	}
};
void test4()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it=find_if(v.begin(), v.end(), GreatFive());
	if (it != v.end())
	{
		cout << "找到了" << *it << endl;
	}
	else
		cout << "没有找到" << endl;
}

3.adjacent_find

void test5()
{
	vector<int>v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(3);
	v.push_back(1);
	v.push_back(2);
	v.push_back(2);
	v.push_back(5);
	vector<int>::iterator pos=adjacent_find(v.begin(), v.end());//查找相邻重复元素
	if (pos == v.end())
	{
		cout << "未找到相邻重复元素" << endl;
	}
	else
		cout << "找到相邻元素" << *pos<<endl;
}

4.binary_search

void test6()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);

	}
	//查找容器中是否有9的元素
	//该容器必需有序数列
	bool ret=binary_search(v.begin(), v.end(),9);
	if (ret)
	{
		cout << "该容器里有9" << endl;
	}
	else
		cout << "该容器里没9" << endl;
}

5.count

void test7()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(30);
	v.push_back(40);
	v.push_back(60);
	v.push_back(30);
	v.push_back(20);
	int num=count(v.begin(), v.end(), 40);//统计出现40的次数
	cout << num << endl;
}

6.count_if

class Great20
{
public:
	bool operator()(Person p)
	{
		return p.age > 20;
	}
};
void test8()
{
	vector<Person>v;
	Person p1("aaa", 10);
	Person p3("ccc", 30);
	Person p2("bbb", 20);
	Person p4("ddd", 20);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	int number=count_if(v.begin(), v.end(), Great20());//返回年龄大于20的人数
	cout << number << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值