C++ STL 查找算法 簡述

查找算法(13個):判斷容器中是否包含某個值

  1. ①count(first,last,value);
    返回 [first,last) 範圍內等於 value 的元素個數。
    代碼示例
#include <iostream>     // std::cout
#include <algorithm>    // std::count
#include <vector>       // std::vector
using namespace std;
int main() {
	int myints[] = { 10,20,30,30,20,10,10,20 };   // 8 elements
	int mycount = count(myints, myints + 8, 10);
	cout << "10 appears " << mycount << " times."<<endl;

	vector<int> myvector(myints, myints + 8);
	mycount = count(myvector.begin(), myvector.end(), 20);
	std::cout << "20 appears " << mycount << " times."<<endl;

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
②count_if(first,last,value,IsOdd);
返回 [first,last) 範圍內使 IsOdd爲 true 的元素個數
③count_if(first,last,value,IsOdd);
返回 [first,last) 範圍內使 IsOdd爲 true 並且等於 value 的元素個數
例題:統計 1-10 中是 3 的倍數的數的個數:

#include <iostream>     // std::cout
#include <algorithm>    // std::count_if
#include <vector>       // std::vector
using namespace std;
bool IsOdd(int i) { 
	return ((i % 3) == 0); 
}
int main() {
	vector<int> myvector;
	for (int i = 1; i < 10; i++) 
		myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9
	int mycount = count_if(myvector.begin(), myvector.end(), IsOdd);// 3 6 9
	cout << "myvector contains " << mycount << " odd values."<<endl;

	system("pause");
	return 0;
}

輸出
在這裏插入圖片描述
2. binary_search(first,last,value);
[first,last) 範圍中如果找到 value ,返回布爾值 true,否則返回 false。
binary_search() 實現了一個二分查找算法。
序列中的元素必須被排成升序序列(從小到大排序)。
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::binary_search, std::sort
#include <vector>       // std::vector
using namespace std;
bool myfunction(int i, int j) { 
	return (i < j); 
}
int main() {
	int myints[] = { 1,2,3,4,5,4,3,2,1 };
	vector<int> v(myints, myints + 9); // 1 2 3 4 5 4 3 2 1

	sort(v.begin(), v.end());// 1 1 2 2 3 3 4 4 5

	cout << "looking for a 3... ";
	if (binary_search(v.begin(), v.end(), 3))
		cout << "found!"<<endl; 
	else 
		cout << "not found."<<endl;

	sort(v.begin(), v.end(), myfunction);// 5 4 4 3 3 2 2 1 1

	cout << "looking for a 6... ";
	if (binary_search(v.begin(), v.end(), 6))
		cout << "found!"<<endl; 
	else 
		cout << "not found."<<endl;

	system("pause");
	return 0;
}

輸出
在這裏插入圖片描述
3. ①adjacent_find (first,last);
在迭代器區間 [first , last) 上每次檢查兩個連續元素,若兩元素相等,返回元素對中第一個元素的迭代器位置,未找到則返回 last 。
②adjacent_find (first,last,myfunction);
在迭代器區間 [first , last) 上每次檢查兩個連續元素,返回使 myfunction 爲 true 的元素對中第一個元素的迭代器位置,未找到則返回 last 。
代碼示例:

#include <iostream>     // std::cout
#include <algorithm>    // std::adjacent_find
#include <vector>       // std::vector
using namespace std;
bool myfunction(int i, int j) {
	return (i == j);
}
int main() {
	int myints[] = { 5,20,5,30,30,20,10,10,20,20 };
	vector<int> myvector(myints, myints + 10); 
	vector<int>::iterator it; 

	it = adjacent_find(myvector.begin(), myvector.end());

	if (it != myvector.end())
		cout << "the first pair of repeated elements are: " << *it << '\n';

	it = adjacent_find(++it, myvector.end(), myfunction);

	if (it != myvector.end())
		cout << "the second pair of repeated elements are: " << *it << '\n';

	it = adjacent_find(++it, myvector.end(), myfunction);

	if (it != myvector.end())
		cout << "the third pair of repeated elements are: " << *it << '\n';

	system("pause");
	return 0;
}

輸出
在這裏插入圖片描述
4. ①equal_range (first,last,value);
代碼示例
在已排序的迭代器區間 [first,last) 中尋找 value,返回一對迭代器 i 和 j ,其中 i 是在不破壞次序的前提下,value 可插入的第一個位置(亦即lower_bound),j 則是在不破壞次序的前提下,value 可插入的最後一個位置(亦即upper_bound),因此,[i,j) 內的每個元素都等同於 value,而且 [i,j) 是 [first,last) 之中符合此一性質的最大子區間

#include<iostream>     // std::cout
#include<algorithm>    // std::equal_range, std::sort
#include<vector>       // std::vector
using namespace std;
bool mygreater(int i, int j) { 
	return (i > j); 
}
int main() {
	int myints[] = { 10,20,30,30,20,10,10,20 };
	vector<int> v (myints, myints + 8); // 10 20 30 30 20 10 10 20
	pair<vector<int>::iterator, vector<int>::iterator> bounds;

	sort(v.begin(), v.end()); // 10 10 10 20 20 20 30 30
	bounds = equal_range(v.begin(), v.end(), 20); 
	cout << "bounds at positions " << (bounds.first - v.begin());
	cout << " and " << (bounds.second - v.begin()) << endl;

	sort(v.begin(), v.end(), mygreater);  // 30 30 20 20 20 10 10 10
	bounds = equal_range(v.begin(), v.end(), 20, mygreater); 

	cout << "bounds at positions " << (bounds.first - v.begin());
	cout << " and " << (bounds.second - v.begin()) << endl;

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
5. ①find (first,last,value);
返回 [first,last) 範圍中第一個等於 value 的元素的迭代器或指針,並非索引下標。
如果沒有元素匹配,則返回 last 。
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector
using namespace std;
int main() {
	int myints[] = { 10, 20, 30, 40 };
	int * p;

	p = find(myints, myints + 4, 30);
	if (p != myints + 4)
		cout << "Element found in myints: " << *p << endl;
	else
		cout << "Element not found in myints" << endl;

	vector<int> myvector(myints, myints + 4);
	vector<int>::iterator it;

	it = find(myvector.begin(), myvector.end(), 30);
	if (it != myvector.end())
		cout << "Element found in myvector: " << *it << endl;
	else
		cout << "Element not found in myvector" << endl;

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
②find_first_of(first1,last1,first2,last2);
返回序列 [first1,last1) 中查找到的子序列 [first2,last2) 的第一個匹配項的第一個元素
如果未找到序列,則函數返回 last1。
注意:找的是一個元素, 只要這個元素是後面一個列表中的任意一個就行了
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolower
using namespace std;
bool comp_case_insensitive(char c1, char c2) {
	return (tolower(c1) == tolower(c2));//把字符串都轉化爲小寫字母
}
int main() {
	int mychars[] = { 'a','b','c','A','B','C' };
	vector<char> haystack(mychars, mychars + 6);
	vector<char>::iterator it;

	int needle[] = { 'A','B','C' };

	it = find_first_of(haystack.begin(), haystack.end(), needle, needle + 3);

	if (it != haystack.end())
		cout << "The first match is: " << *it << '\n';

	it = find_first_of(haystack.begin(), haystack.end(),needle, needle + 3, comp_case_insensitive);

	if (it != haystack.end())
		cout << "The first match is: " << *it << '\n';

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
③find_end (first1,last1,first2,last2);
返回序列 [first1,last1) 中查找到的子序列 [first2,last2) 的最後一個匹配項的第一個元素
如果未找到序列,則函數返回 last1。
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vector
using namespace std;
bool myfunction(int i, int j) {
	return (i == j);
}
int main() {
	int myints[] = { 1,2,3,4,5,1,2,3,4,5 };
	vector<int> haystack(myints, myints + 10);
	int needle1[] = { 1,2,3 };

	vector<int>::iterator it;
	it = find_end(haystack.begin(), haystack.end(), needle1, needle1 + 3);

	if (it != haystack.end())
		cout << "needle1 last found at position " << (it - haystack.begin()) << '\n';

	int needle2[] = { 4,5,1 };

	it = find_end(haystack.begin(), haystack.end(), needle2, needle2 + 3, myfunction);

	if (it != haystack.end())
		cout << "needle2 last found at position " << (it - haystack.begin()) << '\n';

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
④find_if(first,last,IsOdd);
區間 [first,last) 中搜尋使一元判斷式 IsOdd 爲true的第一個元素。
如果沒找到,返回 last 。
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector
using namespace std;
bool IsOdd(int i) {
	return ((i % 2) == 1);
}
int main() {
	vector<int> myvector;

	myvector.push_back(10);
	myvector.push_back(25);
	myvector.push_back(40);
	myvector.push_back(55);

	vector<int>::iterator it = find_if(myvector.begin(), myvector.end(), IsOdd);
	cout << "The first odd value is " << *it << '\n';

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
6. ①lower_bound (first,last,value);
返回有序序列 [first,last) 中最後一個比 value 小的迭代器。
②upper_bound (first,last,value);
返回有序序列 [first,last) 中第一個比 value 大的迭代器。
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector
using namespace std;
int main() {
	int myints[] = { 10,20,30,30,20,10,10,20 };
	std::vector<int> v(myints, myints + 8);           // 10 20 30 30 20 10 10 20

	std::sort(v.begin(), v.end());                // 10 10 10 20 20 20 30 30

	std::vector<int>::iterator low, up;
	low = std::lower_bound(v.begin(), v.end(), 20); //          ^
	up = std::upper_bound(v.begin(), v.end(), 20); //                   ^

	std::cout << "lower_bound at position " << (low - v.begin()) << '\n';
	std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
7. ①search (first1,last1,first2,last2);
返回序列 [first1,last1) 中查找到的子序列 [first2,last2) 的第一個匹配項的第一個元素位置
如果未找到序列,則函數返回 last1 位置。
注意:找的是一塊相同的區域
代碼示例

#include <iostream>     // std::cout
#include <algorithm>    // std::search
#include <vector>       // std::vector
using namespace std;
bool mypredicate(int i, int j) {
	return (i == j);
}
int main() {
	vector<int> haystack;

	for (int i = 1; i < 10; i++) 
		haystack.push_back(i * 10);

	int needle1[] = { 40,50,60,70 };
	vector<int>::iterator it;
	it = search(haystack.begin(), haystack.end(), needle1, needle1 + 4);

	if (it != haystack.end())
		cout << "needle1 found at position " << (it - haystack.begin()) << '\n';
	else
		cout << "needle1 not found\n";

	int needle2[] = { 20,30,50 };
	it = search(haystack.begin(), haystack.end(), needle2, needle2 + 3, mypredicate);

	if (it != haystack.end())
		cout << "needle2 found at position " << (it - haystack.begin()) << '\n';
	else
		cout << "needle2 not found\n";

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述
②search_n (first,last, times, value);
返回首次出現 times 個連續 value 的第一個元素的位置
或者返回出現 times 個 value 的最後一個元素的位置
代碼實現

#include <iostream>     // std::cout
#include <algorithm>    // std::search_n
#include <vector>       // std::vector
using namespace std;
bool mypredicate(int i, int j) {
	return (i == j);
}
int main() {
	int myints[] = { 10,20,30,30,20,10,10,20 };
	vector<int> myvector(myints, myints + 8);

	vector<int>::iterator it;

	it = search_n(myvector.begin(), myvector.end(), 2, 30);

	if (it != myvector.end())
		cout << "two 30s found at position " << (it - myvector.begin()) << '\n';
	else
		cout << "match not found\n";

	it = search_n(myvector.begin(), myvector.end(), 2, 10, mypredicate);

	if (it != myvector.end())
		cout << "two 10s found at position " << int(it - myvector.begin()) << '\n';
	else
		cout << "match not found\n";

	system("pause");
	return 0;
}

結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章