C++ std::find 搜尋用法與範例

本篇介紹 C++ 的 std::find 搜尋/尋找的用法教學,C++ find 常用於容器裡搜尋,例如:傳統陣列 array 或 vector 等其他容器,
在使用 C++ STL 容器時需要搜尋/尋找就可以使 std::find,學習後可以說是方便許多,C++ 最常用到的就是在 vector 裡 find 搜尋,這邊把常用到的用法與範例紀錄一下。

以下內容將分為這幾部分,

  • 範例1. 在傳統陣列尋找
  • 範例2. 在 C++ STL vector 容器尋找
  • 範例3. 使用 std::find_if 函式來尋找

需要引入的標頭檔<algorithm>

範例1. 在傳統陣列尋找

這邊示範 c++ 使用 std::find 在傳統陣列裡搜尋目標數值,例如我要搜尋整數 3 有沒有在這個陣列裡,

std-find1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// g++ std-find1.cpp -o a.out
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
int arr[] = {2, 3, 4, 5, 6};

int *p = std::find(arr, arr+4, 3); // find 3
if (p == arr+4) {
cout << "not find\n";
} else {
cout << "found " << *p << "\n";
}

return 0;
}

結果輸出如下,

1
found 3

範例2. 在 C++ STL vector 容器尋找

這邊示範 c++ 使用 std::find 在 std::vector 容器裡搜尋目標數值,使用 std::find 傳入 vector 後,在 std::find 的第一個參數與第二個參數指定搜尋範圍後,第三個參數為欲搜尋的數值,

最後判斷一下,如果迭代到 vector 最後一個元素的話代表沒有搜尋找到該數值,

例如我要搜尋整數 10 有沒有在這個 vector 裡,

std-find2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// g++ std-find2.cpp -o a.out -std=c++11
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
vector<int> v = {2, 4, 6, 8, 10, 12, 14, 16, 18};

vector<int>::iterator it = std::find(v.begin(), v.end(), 10); // find 10
if (it != v.end())
cout << "found " << *it << ", index: " << std::distance(v.begin(), it) << "\n";
else
cout << "not find\n";

return 0;
}

結果輸出如下,

1
found 10, index: 4

範例3. 使用 std::find_if 函式來尋找

這個範例是使用 std::find_if 在 std::vector 容器搜尋目標數值,功能跟 std::find 很像所以這邊也介紹一下,差別在 std::find_if 的第三的參數是放入謂詞(pred),

例如我要搜尋整數 5 有沒有在這個 vector 裡,這裡我們示範自己寫的 equal_5 謂詞,

std-find3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// g++ std-find3.cpp -o a.out -std=c++11
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template<typename T>
bool smallthan_5(T value) {
return value < 5;
}

template<typename T>
bool equal_5(T value) {
return value == 5;
}

int main() {
vector<int> v;
v.push_back(2);
v.push_back(4);
v.push_back(3);
v.push_back(5);
v.push_back(6);

vector<int>::iterator it = find_if(v.begin(), v.end(), equal_5<int>);
if (it != v.end())
cout << "found " << *it << endl;
else
cout << "not find\n";

return 0;
}

結果輸出如下,

1
found 5

不想自己寫謂詞,你也可以使用內建的 equal_to<type>() 謂詞像這樣寫,

1
2
3
#include <functional>
using std::placeholders::_1;
vector<int>::iterator it = find_if(v.begin(), v.end(), bind(equal_to<>(), _1, 5));

或這樣寫,

1
vector<int>::iterator it = find_if(v.begin(), v.end(), bind2nd(equal_to<int>(), 5));

你還可以使用 lambda 表達式像這樣寫,

1
vector<int>::iterator it = find_if(v.begin(), v.end(), [](int value) { return value == 5; });

以上就是 C++ std::find 搜尋用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其他參考
[1] std::find, std::find_if, std::find_if_not - cppreference.com
https://en.cppreference.com/w/cpp/algorithm/find
[2] find - C++ Reference
http://www.cplusplus.com/reference/algorithm/find/
[3] std::find in C++ - GeeksforGeeks
https://www.geeksforgeeks.org/std-find-in-cpp/
[4] C++ find()函数用法(一般用于vector的查找)
https://blog.csdn.net/zhangweijiqn/article/details/9107571

其它相關文章推薦
C/C++ 新手入門教學懶人包
std::thread 用法與範例
std::deque 用法與範例
std::mutex 用法與範例
std::unordered_map 用法與範例
std::sort 用法與範例
std::random_shuffle 產生不重複的隨機亂數
std::shared_ptr 用法與範例
std::async 用法與範例