文章目录
一、常用查找算法
find、find_if、adijacent_find、binary_search
1.find
find(first, end, value);
返回区间[first,end)中第一个值等于value的元素位置;
若未找到,返回end。
函数返回的是迭代器或指针,即位置信息。
void test02(){
vector<int> v1;
for (int i = 0; i < 10; i++){
v1.push_back(i);
}
vector<int>::iterator ret = find(v1.begin(), v1.end(), 5);
if (ret == v1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到了:" << *ret << endl;
}
}
从对象中查找
class Person {
public:
Person(int age, int id) :age(age), id(id) {}
bool operator==(const Person& p) const {
return p.id == this->id && p.age == this->age;
}
public:
int id;
int age;
};
void test03() {
vector<Person> v1;
Person p1(10, 20), p2(20, 30);
v1.push_back(p1);
v1.push_back(p2);
vector<Person>::iterator ret = find(v1.begin(), v1.end(), p1);
if (ret == v1.end()) {
cout << "Person 么有找到!" << (*ret).id << endl;
}
else {
cout << "Person 找到了: " << (*ret).id << endl;
}
}
}
2.adjacent_find 查找相邻重复元素
/*adjacent_find 查找相邻重复元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param callback 回调函数或则谓词(返回bool型的函数对象)
@return 返回相邻元素的第一个位置迭代器
*/
adjacent_find(iterator beg, iterator end, _callback);
3.find_if 查找满足条件的
find_f 会根据我们的条件(函数) ,返回第一个满足条件的元素的迭代器
/*find_if 查找相邻重复元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param callback 回调函数或则谓词(返回bool型的函数对象)
@return 到返回true,否则false
*/
find_if(iterator beg, iterator end, _callback);
4.binary_search 二分查找
/*binary_search算法 二分查找法
注意:在无序容器中不可用
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value 查找的元素
@return 找到返回true,否则false
*/
binary_search(iterator beg, iterator end, value);
程序示例
bool MySearch(int val) {
return val > 5;
}
bool MySearch2(int val) {
return val > 5;
}
bool MySearch01(int val, int v) {
return val > v;
}
void test04() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
v1.push_back(9);
//binary_search 二分查找法
bool ret = binary_search(v1.begin(), v1.end(), 5);
if (ret) {
cout << "binary_search 找到!" << endl;
}
else {
cout << "binary_search 没有找到!" << endl;
}
/*adjacent_find 查找相邻重复元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param callback 回调函数或则谓词(返回bool型的函数对象)
@return 返回相邻元素的第一个位置迭代器
*/
vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
if (it != v1.end()) {
cout << "adjacent_find 找到相邻重复元素:" << *it << endl;
}
else {
cout << "adjacent_find 没有找打相邻重复元素" << endl;
}
//find_f 会根据我们的条件(函数) ,返回第一个满足条件的元素的迭代器
//it = find_if(v1.begin(), v1.end(), not1(bind2nd(ptr_fun(MySearch01),4)));
it = find_if(v1.begin(), v1.end(), MySearch);
if (it != v1.end()) {
cout << "find_if 找到:" << *it << endl;
}
else {
cout << "find_if 没有找到" << endl;
}
//count count_if
int num = count(v1.begin(), v1.end(), 9);
cout << "9出现的次数:" << num << endl;
num = count_if(v1.begin(), v1.end(), MySearch2);
cout << "大于5的个数:" << num << endl;
}
}
程序执行结果:
二、常用遍历算法
transform 、for_each、count、count_if
1. transform
transform 将一个容器的元素 搬运 到另一个容器中,并执行特定操作
transform(数据起始,结束,写入目标的起始,执行的操作)
//transform 将一个容器的元素 搬运 到另一个容器中,并执行特定操作
struct MyPlus {
int operator()(int val) {
return val + 100;
}
};
void MyPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
v2.resize(v1.size()); //开辟空间
cout << "tranform Test..." << endl;
transform(数据起始,结束,写入目标的起始,执行的操作)
//遍历一个容器里面元素 然后执行一个操作
transform(v1.begin(), v1.end(), v2.begin(), MyPlus());
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
}
2.for_each
参照上述使用示例