查找算法(13个):判断容器中是否包含某个值
- ①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;
}
结果: