# STL常用查找算法介绍

540人阅读 评论(1)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);

if (it == v1.end())
{
cout << "没有找到 重复的元素" << endl;
}
else
{
cout << *it << endl;
}
// 2
int index = distance(v1.begin(), it);
cout << index << endl;
// 1

}

int main()
{

return 0;
}

binary_search()

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

// binary_search是对排序好的进行查找
void play_binary_search()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(9);

bool b = binary_search(v1.begin(), v1.end(), 7);
if (b) {
cout << "find success\n";
}
else {
cout << "find fail\n";
}
// find success

}

int main()
{
play_binary_search();

return 0;
}

count()

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_count()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(3);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);

int cnt = count(v1.begin(), v1.end(), 3);
cout << "count of 3: " << cnt << endl;
// count of 3: 3

}

int main()
{
play_count();

return 0;
}

count_if()
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
return a > 3;
}

void play_count_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);

int cnt = count_if(v1.begin(), v1.end(), GreaterThree);
cout << "count of greater 3: " << cnt << endl;
// count of greater 3: 3

}

int main()
{
play_count_if();

return 0;
}

#### find()

find:  利用底层元素的等于操作符，对指定范围内的元素与输入值进行比较。当匹配时，结束搜索，返回该元素的迭代器。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);

vector<int>::iterator it = find(v1.begin(), v1.end(), 4);
if (it == v1.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
}
// find success

}

int main()
{
play_find();

return 0;
}

find_if()
find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
return a > 3;
}

void play_find_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(4);
v1.push_back(7);
v1.push_back(9);
v1.push_back(3);

vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterThree);
if (it == v1.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
cout << "value: " << *it << endl;
}
// find success
// value: 4

}

int main()
{
play_find_if();

return 0;
}

0
0

个人资料
• 访问：423898次
• 积分：8210
• 等级：
• 排名：第2964名
• 原创：401篇
• 转载：30篇
• 译文：11篇
• 评论：87条
博客专栏
 iOS Developer Library 文章：12篇 阅读：22386
阅读排行