5个常用的遍历算法

8 篇文章 0 订阅
6 篇文章 0 订阅

for_each

#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

void printfs(int val)
{
	cout << val << endl;
}
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	for_each(v.begin(), v.end(), printfs);
}
int main()
{
	test01();
	return 0;
}

transform

#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class Printfs
{
public:
	void operator()(int val)
	{
		cout << val << "\t";
	}
};
class TransForm
{
public:
	int operator()(int val)
	{
		return val;
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int> v1;
	//重新指定大小
	v1.resize(v.size());
	//替换
	transform(v.begin(), v.end(), v1.begin(),TransForm());
	//遍历
	for_each(v1.begin(), v1.end(), Printfs());
}
int main()
{
	test01();
	return 0;
}

find

#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

//默认数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find(v.begin(),v.end(),5);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到:\t" << *it << endl;
	}
}
//自定义数据类型
class Person
{
public:
	Person(string name, int age) :Name(name), Age(age) {}
	string getName()
	{
		return Name;
	}
	int getAge()
	{
		return Age;
	}
	bool operator==(const Person &p)
	{
		if (this->Name == p.Name && this->Age == p.Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	string Name;
	int Age;
};
void test02()
{
	vector<Person> v;
	Person p1("a", 1);
	Person p2("b", 2);
	Person p3("c", 3);
	Person p4("d", 4);
	Person p5("e", 5);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	vector<Person>::iterator it = find(v.begin(),v.end(),p2);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到:\t" << it->getName() << "\t" << it->getAge() << endl;
	}
}
int main()
{
	//test01();
	test02();
	return 0;
}

find_if

#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

class Person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
class Person_tow
{
public:
	Person_tow(string name, int age) :Name(name), Age(age) {}
	string getName()
	{
		return Name;
	}
	int getAge()
	{
		return Age;
	}
private:
	string Name;
	int Age;
};
class Ppp
{
public:
	bool operator()(Person_tow &p)
	{
		return p.getAge() > 2;
	}
};

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), Person());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到:\t" << *it << endl;
	}
}
void test02()
{
	vector<Person_tow> v;
	Person_tow p1("aaa",1);
	Person_tow p2("bbb",2);
	Person_tow p3("ccc",3);
	Person_tow p4("ddd",4);
	Person_tow p5("eee",5);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	vector<Person_tow>::iterator it = find_if(v.begin(),v.end(),Ppp());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到:\t" << it->getName() << "\t" << it->getAge() << endl;
	}

}
int main()
{
	//test01();
	test02();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值