0
点赞
收藏
分享

微信扫一扫

C++常用遍历和查找算法

卿卿如梦 2022-03-11 阅读 40

for_each

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*for_each遍历
for_each(v.begin(), v.end(), MyPrint());
//参数列表:1:起始迭代器,2:结束迭代器,3:函数对象或函数名
*/

class MyPrint {
public:
	void operator()(int& a) {
		cout << a << " ";
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

int main() {
	test();
	return 0;
}

transform

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*transform遍历
transform(v.begin(), v.end(), vTarget.begin(), Transform());
//参数列表:1:原起始迭代器,2:原结束迭代器,3:目标起始迭代器,4:函数对象或函数名
*/

class MyPrint {
public:
	void operator()(int& a) {
		cout << a << " ";
	}
};

class Transform {
public:
	int operator()(int& a) {
		return a;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	vector<int> vTarget;
	vTarget.resize(v.size());//需要先开辟空间,否者transform会越界

	transform(v.begin(), v.end(), vTarget.begin(), Transform());

	for_each(vTarget.begin(), vTarget.end(), MyPrint());
	cout << endl;
}

int main() {
	test();
	return 0;
}

find

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*find
vector<int>::iterator it = find(v.begin(), v.end(), 5);
//参数列表:1:起始迭代器,2:结束迭代器,3:查找目标元素,
返回值:目标对应的迭代器
自定义数据类型vector<Person>要重载operator==()
*/

void test() {
	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 << "not" << endl;
	}
	else {
		cout << "it: " << *it << endl;
	}

}

int main() {
	test();
	return 0;
}

find_if

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*find_if
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
//参数列表:1:起始迭代器,2:结束迭代器,3:谓词或函数名,
返回值:第一个满足条件的元素对应的迭代器
*/
class GreaterFive {
public:
	bool operator()(int& a) {
		return a > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "not" << endl;
	}
	else {
		cout << "it: " << *it << endl;
	}

}

int main() {
	test();
	return 0;
}

adjacent_find

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
//参数列表:1:起始迭代器,2:结束迭代器,,
返回值:相邻重复的元素中第一个元素对应的迭代器
*/
class GreaterFive {
public:
	bool operator()(int& a) {
		return a > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(9);
	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end()) {
		cout << "not" << endl;
	}
	else {
		cout << "it: " << *it << endl;
	}

}

int main() {
	test();
	return 0;
}

二分查找 binary_search 查找序列必须是有序序列,否则结果未知

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*
bool it = binary_search(v.begin(), v.end(),10);必须是有序序列,否则结果未知
//参数列表:1:起始迭代器,2:结束迭代器,3:查找的元素,
返回值bool:区间内有该元素则返回true,否则返回false
*/
class GreaterFive {
public:
	bool operator()(int& a) {
		return a > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(10);
	bool it = binary_search(v.begin(), v.end(),9);
	if (it == false) {
		cout << "not" << endl;
	}
	else {
		cout << "it: " << it << endl;
	}

}

int main() {
	test();
	return 0;
}

count 自定义数据类型要重载operator==

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*
int it = count(v.begin(), v.end(),9);
//参数列表:1:起始迭代器,2:结束迭代器,3:查找的元素,
返回值int:区间内该元素的个数
*/
class GreaterFive {
public:
	bool operator()(int& a) {
		return a > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(5);
	int it = count(v.begin(), v.end(),5);
	if (it == 0) {
		cout << "not" << endl;
	}
	else {
		cout << "it: " << it << endl;
	}

}

int main() {
	test();
	return 0;
}

count_if

#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>

/*count_if
int it = count_if(v.begin(), v.end(),GreaterFive());
//参数列表:1:起始迭代器,2:结束迭代器,3:谓词,
返回值int:区间内满足条件元素的个数
*/
class GreaterFive {
public:
	bool operator()(int& a) {
		return a > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(6);
	int it = count_if(v.begin(), v.end(),GreaterFive());
	if (it == 0) {
		cout << "not" << endl;
	}
	else {
		cout << "it: " << it << endl;
	}

}

int main() {
	test();
	return 0;
}
举报

相关推荐

0 条评论