C++STL总结——常用算法——查找算法
包含头文件:algorithm
整合
//find:查找元素是否存在
//find(begin, end, value)
//找到返回迭代器,未找到返回end()
void find_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
}
//find_if(begin, end, function):按照条件查找
//最后一个参数是函数或者仿函数
//返回迭代器
class GreaterFive {
public:
bool operator() (int n) {
return n>5;
}
};
void find_if_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
}
//adjacent_find(begin, end):查找相邻重复元素
//返回相邻元素的第一个位置的迭代器
void adjacent_find_test() {
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(3);
v.push_back(0);
//adjacent_find()查找会返回这个位置的迭代器,对迭代器解引用解出来的是值
v.push_back(7);
v.push_back(7);
v.push_back(0);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
cout << *pos << endl;
}
//binary_search(begin, end, value):二分查找
//必须用在有序序列中
//返回值为bool
void binary_search_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
binary_search(v.begin(), v.end(), 5);
}
//count(begin(), end(), value):统计元素个数
//返回值为int
void count_test() {
vector<int> v;
v.push_back(3);
v.push_back(3);
v.push_back(5);
v.push_back(2);
v.push_back(1);
cout << count(v.begin(), v.end(), 3) << endl;
}
//count_if(begin(), end(), function):按照条件查找
//最后一个参数是谓词(仿函数)
//返回值类型为int
class GreaterFour {
public:
bool operator() (int n) {
return n>4;
}
};
void count_if_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
cout << count_if(v.begin(), v.end(), GreaterFour()) << endl;
}
find
//find:查找元素是否存在
//find(begin, end, value)
//找到返回迭代器,未找到返回end()
void find_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
}
find_if
//find_if(begin, end, function):按照条件查找
//最后一个参数是函数或者仿函数
//返回迭代器
class GreaterFive {
public:
bool operator() (int n) {
return n>5;
}
};
void find_if_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
}
adjacent_find
//adjacent_find(begin, end):查找相邻重复元素
//返回相邻元素的第一个位置的迭代器
void adjacent_find_test() {
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(3);
v.push_back(0);
//adjacent_find()查找会返回这个位置的迭代器,对迭代器解引用解出来的是值
v.push_back(7);
v.push_back(7);
v.push_back(0);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
cout << *pos << endl;
}
binary_search
//binary_search(begin, end, value):二分查找
//必须用在有序序列中
//返回值为bool
void binary_search_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
binary_search(v.begin(), v.end(), 5);
}
count
//count(begin(), end(), value):统计元素个数
//返回值为int
void count_test() {
vector<int> v;
v.push_back(3);
v.push_back(3);
v.push_back(5);
v.push_back(2);
v.push_back(1);
cout << count(v.begin(), v.end(), 3) << endl;
}
count_if
//count_if(begin(), end(), function):按照条件查找
//最后一个参数是谓词(仿函数)
//返回值类型为int
class GreaterFour {
public:
bool operator() (int n) {
return n>4;
}
};
void count_if_test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
cout << count_if(v.begin(), v.end(), GreaterFour()) << endl;
}