常用算法

概念

  • 由头文件algorithm functional numeric 组成-
  • algorithm最大,涉及比较、交换、查找、遍历、复制、修改等
  • functional模板类,声明函数对象
  • numeric 体积很小,只包括几个序列上面进行简单运算的模板函数

遍历

for_each

class print01
{
public:
	void operator()(int i)
	{
		cout << i << " ";
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), print01());
	cout << endl;
}

transform

class TransForm
{
public:
	int operator()(int v)
	{
		return v+1;
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int> vTarget;
	vTarget.resize(v.size());
	transform(v.begin(), v.end(), vTarget.begin(), TransForm());
	for_each(vTarget.begin(), vTarget.end(), print01());
}

查找算法

find

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;

	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else {
			return false;
		}
	}
};
void test01()
{
	vector<int> v;
	for (int i = 3; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it == v.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << it - v.begin()  << endl; //return index
	}

	Person p1("Tom", 20);
	Person p2("Jerry", 18);
	Person p3("Trump", 80);
	Person p4("Biden", 70);

	vector<Person> v2;

	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	Person p("Biden", 70);
	vector<Person>::iterator it2 = find(v2.begin(), v2.end(), p);
	if (it2 == v2.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << it2->m_Name << endl;
	}
}

find_if(条件查找)

class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
};

class Greater80
{
public:
	bool operator()(Person& p) const
	{
		return p.m_Age > 80;
	}
};

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterFive());

	if (it == v1.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << it - v1.begin() << endl;
	}

	vector<Person> v2;
	Person p1("Tom", 20);
	Person p2("Jerry", 18);
	Person p3("Trump", 80);
	Person p4("Biden", 70);
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);

	vector<Person>::iterator it2 = find_if(v2.begin(), v2.end(), Greater80());
	if (it2 == v2.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << it2->m_Name << " " << it2->m_Age << endl;
	}
}

adjacent_find

  • 查找相邻重复元素
void test01()
{
	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(3);
	v.push_back(3);

	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos == v.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << pos - v.begin() << endl;
	}
}

二分查找

  • 无序序列不可用
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	bool result = binary_search(v.begin(), v.end(), 9);
	cout << result << endl;
}

count

class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	bool operator==(const int age)const
	{
		if (this->m_Age == age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(10);
	v.push_back(10);
	v.push_back(10);
	int num = count(v.begin(), v.end(), 10);
	cout << num << endl;
	vector<Person>v2;
	Person p1("Tom", 20);
	Person p2("Jerry", 20);
	Person p3("Trump", 80);
	Person p4("Biden", 70);
	Person p5("Tom", 20);
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);
	int num2 = count(v2.begin(), v2.end(), 20);
	cout << "the num of persons with same age to Tom is :" << num2 << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值