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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 查找算法(线性查找) 线性查找又叫顺序查找,是一种基础的查找算法。顾名思义,就是按照数据的顺序依次进行查找,直到找到目标数据或者遍历完整个数据集为止。 C++代码实现如下: ```c++ #include <iostream> using namespace std; int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { if (arr[i] == x) { return i; } } return -1; // 未找到目标数据 } int main() { int arr[] = { 2, 5, 3, 7, 9, 1 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 7; int index = search(arr, n, x); if (index == -1) { cout << "未找到目标数据!" << endl; } else { cout << "目标数据在数组中的下标为:" << index << endl; } return 0; } ``` 2. 折半查找算法 折半查找又叫二分查找,是一种高效的查找算法。前提条件是数据已经排好序。折半查找的思路是将数据集分成两部分,判断目标数据在哪一部分,然后在该部分中继续进行查找,直到找到目标数据或者确定目标数据不存在为止。 C++代码实现如下: ```c++ #include <iostream> using namespace std; int binarySearch(int arr[], int low, int high, int x) { if (low > high) { return -1; // 未找到目标数据 } int mid = (low + high) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] > x) { return binarySearch(arr, low, mid - 1, x); } else { return binarySearch(arr, mid + 1, high, x); } } int main() { int arr[] = { 1, 2, 3, 5, 7, 9 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 7; int index = binarySearch(arr, 0, n - 1, x); if (index == -1) { cout << "未找到目标数据!" << endl; } else { cout << "目标数据在数组中的下标为:" << index << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值