(原創) 如何使用find() algorithm? (C/C++) (STL)
find()能在container中,尋找單一的值,若找的到,傳回該iterator,若找不到,則傳回container.end()。
1/*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : GenericAlgo_find.cpp
5Compiler : Visual C++ 8.0 / ISO C++
6Description : Demo how to use find() algorithm
7Release : 11/15/2006 1.0
8 12/14/2006 2.0
9*/
10
11#include <iostream>
12#include <vector>
13#include <algorithm>
14
15using namespace std;
16
17int main() {
18 int ia[] = {1,2,3};
19 vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
20
21 vector<int>::iterator iter = find(ivec.begin(), ivec.end(),3);
22
23 if (iter != ivec.end()) {
24 cout << *iter << endl;
25 }
26 else {
27 cout << "not find!!" << endl;
28 }
29}
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : GenericAlgo_find.cpp
5Compiler : Visual C++ 8.0 / ISO C++
6Description : Demo how to use find() algorithm
7Release : 11/15/2006 1.0
8 12/14/2006 2.0
9*/
10
11#include <iostream>
12#include <vector>
13#include <algorithm>
14
15using namespace std;
16
17int main() {
18 int ia[] = {1,2,3};
19 vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
20
21 vector<int>::iterator iter = find(ivec.begin(), ivec.end(),3);
22
23 if (iter != ivec.end()) {
24 cout << *iter << endl;
25 }
26 else {
27 cout << "not find!!" << endl;
28 }
29}
執行結果
13
2請按任意鍵繼續 . . .
2請按任意鍵繼續 . . .