c++常用stl算法

1、头文件

        这些算法通常包含在头文件<algorithm> <functional> <numeric>中。

 2、常用遍历算法

for_each(v.begin(),v.end(), 元素处理函数/仿函数) 

注意:在使用transform转存时,目标容器需要提取开辟合适的空间。 

void printfunc(int val) {
	cout << val << " ";
}
class Printoperator {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};
int main() {

	vector<int> vi(10, 3);
	//输入一般函数完成for_each算法中的元素处理
	for_each(vi.begin(), vi.end(), printfunc);
	cout << endl;
	//输入仿函数完成for_each算法中的元素处理
	for_each(vi.begin(), vi.end(), Printoperator());
	cout << endl;
	system("pause");
	return 0;
}
class Printoperator {
public:
	int operator()(int val) {
		return val + 1;
	}
};
void print(vector<int> &v) {
	for (auto i : v) {
		cout << i << " ";
	}
	cout << endl;
}
int main() {

	vector<int> vi(10, 3);

	vector<int> vi2(4, 5);
	//需要提前给目标容器开辟空间,不然会报错
	vi2.resize(vi.size());
	transform(vi.begin(), vi.end(), vi2.begin(), Printoperator());
	print(vi2);
	system("pause");
	return 0;
}

3、常用查找算法

 find按值查找元素

函数原型:

         使用find查找自定义类型元素的时候,需要在对应类中重载==,以便find算法能知道该怎么比较。

class Person {
public:
	Person(int age, string name) {
		this->mAge = age;
		this->mName = name;
	}
	//帮助find查找元素
	bool operator==(const Person &p) {
		if (p.mAge == this->mAge && p.mName == this->mName) {
			return true;
		}
		else {
			return false;
		}
	}
	int mAge;
	string mName;
};
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(40, "ads");
	Person p4(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator iter = find(v.begin(), v.end(), p2);

	if (iter != v.end()) {
		cout << "逮到 !!!" << endl;
	}
	system("pause");
	return 0;
}

 find_if按值查找元素

         利用find_if按自定义谓词找到合适的自定义类型元素:

class Person {
public:
	Person(int age, string name) {
		this->mAge = age;
		this->mName = name;
	}
	int mAge;
	string mName;
};
class FindPerson {
public:
	bool operator()(const Person &p) {
		return p.mAge > 20;
	}
};
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(40, "ads");
	Person p4(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator iter = find_if(v.begin(), v.end(), FindPerson());


	if (iter != v.end()) {
		cout << (*iter).mName<<" 的年龄超过了20" << endl;
	}
	system("pause");
	return 0;
}

adjacent_find查找相邻重复元素 

         adjacen_find按自定义谓词查找相邻满足条件自定义元素:

class Person {
public:
	Person(int age, string name) {
		this->mAge = age;
		this->mName = name;
	}
	
	int mAge;
	string mName;
};
class FindPerson {
public:
	bool operator()(const Person &p1, const Person& p2) {
		if (p1.mAge == p2.mAge)
			return true;
		else
			return false;
	}
};
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	vector<Person>::iterator iter = adjacent_find(v.begin(), v.end(), FindPerson());

	if (iter != v.end()) {
		cout <<"有相邻年龄相同的人" << (*iter).mName <<"和" << (*(iter+1)).mName << endl;
	}
	else {
		cout << "没有相邻年龄相同的人" << endl;
	}
	system("pause");
	return 0;
}

binary_search查指定元素是否存在(不适用无序序列,必须有序且为升序序列) 

count统计元素出现次数

返回int

class Person {
public:
	Person(int age, string name) {
		this->mAge = age;
		this->mName = name;
	}
	//帮助count查找元素
	bool operator==(const Person &p) {
		if (p.mAge == this->mAge) {
			return true;
		}
		else {
			return false;
		}
	}
	int mAge;
	string mName;
};

int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count(v.begin(), v.end(), p2);
	cout << "20岁的人有 " << num << "个"  << endl;
	
	system("pause");
	return 0;
}

count_if 按条件统计元素个数 ,返回int

count_if的谓词使用参见find_if。 

4、常用排序算法

        

 sort排序

 对自定义类型排序:

class Person {
public:
	Person(int age, string name) {
		this->mAge = age;
		this->mName = name;
	}
	int mAge;
	string mName;
};
class SortPerson {
public:
	bool operator()(const Person& p1, const Person& p2) {
		if (p1.mAge > p2.mAge)
			return true;
		else
			return false;
	}
};
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	sort(v.begin(), v.end(), SortPerson());
	
	system("pause");
	return 0;
}

random_shuffle随机调序 

int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	random_shuffle(v.begin(), v.end());
	//cout << "20岁的人有 " << num << "个"  << endl;
	
	system("pause");
	return 0;
}

 

 merge合并容器元素(容器必须有序,提取个目标容器分配空间)

	vector<int> vi1{ 1,2,3,4,5 };
	vector<int> vi2{ 2,3,4,5,7 };

	vector<int> target;
	//提前个target开辟空间
	target.resize((vi1.size() + vi2.size()));

	merge(vi1.begin(), vi1.end(), vi2.begin(), vi2.end(),target.begin());

reverse反转容器中的元素 (不需要谓词就能实现自定义类型反转)

 

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	reverse(v.begin(), v.end());

5、常用拷贝替换生成算法

copy拷贝(需要预先开辟空间)

注:在拷贝自定义类时,需要有对应的空默认构造函数,如:Person(){}。 

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	vector<Person> target;
	target.resize(v.size());

	copy(v.begin(), v.end(), target.begin());

replace元素替换

	vector<int> vi1{ 1,2,3,4,5 };

	replace(vi1.begin(), vi1.end(), 1, 20);

replace_if满足条件元素替换

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

class ReplacePerson {
public:
	bool operator()(const Person& p) {
		if (p.mAge > 20)
			return true;
		else
			return false;
	}
};
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p6(10, "dsaf");

	replace_if(v.begin(), v.end(), ReplacePerson(), p6);

	system("pause");
	return 0;
}

swap交换容器元素

 6、常用算术生成算法

accumulate计算累加

 

自定义类型的某成员变量累加:(也可以采用仿函数形式)

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

int Age_acc(int pre,Person& p) {
	return pre + p.mAge;
}
int main() {

	vector<Person> v;

	Person p1(10, "aaa");
	Person p2(20, "sss");
	Person p3(20, "sad");
	Person p4(40, "ads");
	Person p5(10, "ccc");

	
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p6(10, "dsaf");
	int acu = accumulate(v.begin(), v.end(), 0, Age_acc);

	system("pause");
	return 0;
}

 fill向容器中填充元素

 7、常用集合算法

 set_intersection求两个容器交集(必须预先开辟目标容器空间)

set_union求两个容器并集

set_difference求两个容器差集

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值