adjacent_find binary_search cout/cout_if find/find_if/equal_range
在一个集合中寻找两个相邻的元素。如果相等,就返回这两个相等元素第一个元素的迭代器,不等的话,就返回v.end(). 函数原型:
template<class _FwdIt,
class _Pr> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find first satisfying _Pred with successor
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER_IF(_First != _Last && _STD next(_First) != _Last, _Pred);
return (_Rechecked(_First,
_Adjacent_find_unchecked(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
// TEMPLATE FUNCTION adjacent_find
template<class _FwdIt> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
{ // find first matching successor
return (_STD adjacent_find(_First, _Last, equal_to<>()));
}
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
using namespace std;
class Student{
private:
int number;
string name;
public:
Student(int number, string name) {
cout << "构造 " << number << " " << name.c_str() << endl;
this->number = number;
this->name = name;
}
Student(const Student & stu) {
//cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
this->number = stu.getNumber();
this->name = stu.getName();
}
~Student() {
//cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
}
void print() {
cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
}
int getNumber() const{
return this->number;
}
string getName()const {
return this->name;
}
};
struct StuFunc
{
bool operator()(const Student & stu1, const Student & stu2) const {
return stu1.getNumber() == stu2.getNumber();
}
};
void printStuV(vector<Student> v) {
cout << "开始遍历============" << endl;
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
it->print();
}
cout << "结束遍历============" << endl;
}
int main()
{
vector<Student> v;
v.push_back(Student(1, "one"));
Student stu2(2, "two");
v.push_back(stu2);
v.push_back(stu2);
v.push_back(Student(3, "three"));
v.push_back(Student(4, "four"));
v.push_back(Student(5, "five"));
printStuV(v);
vector<Student>::iterator res=adjacent_find(v.begin(), v.end(), StuFunc());
if (res != v.end()) {
cout << "adjacent_find result:"<<endl;
res++->print();
res++->print();
res->print();
}
else {
cout << "adjacent_find nothing!" << endl;
}
return 0;
}
结果:
adjacent_find.png
二分查找 集合必须是有序的
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
using namespace std;
class Student{
private:
int number;
string name;
public:
Student(int number, string name) {
cout << "构造 " << number << " " << name.c_str() << endl;
this->number = number;
this->name = name;
}
Student(const Student & stu) {
//cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
this->number = stu.getNumber();
this->name = stu.getName();
}
~Student() {
//cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
}
void print()const {
cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
}
int getNumber() const{
return this->number;
}
string getName()const {
return this->name;
}
};
struct StuFunc
{
bool operator()(const Student & stu1, const Student & stu2) const {
cout << "StuFunc》》"<<endl;
stu1.print();
stu2.print();
return stu1.getNumber() <stu2.getNumber();
}
};
void printStuV(vector<Student> v) {
cout << "开始遍历============" << endl;
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
it->print();
}
cout << "结束遍历============" << endl;
}
int main()
{
vector<Student> v;
v.push_back(Student(1, "one"));
Student stu2(2, "two");
v.push_back(stu2);
v.push_back(stu2);
v.push_back(Student(3, "three"));
v.push_back(Student(4, "four"));
v.push_back(Student(5, "five"));
printStuV(v);
bool res=binary_search(v.begin(), v.end(),Student(7,"keyword"), StuFunc());
cout << "binary_search 7: " << res << endl << endl;;
res = binary_search(v.begin(), v.end(), Student(2, "keyword"), StuFunc());
cout << "binary_search 2: " << res << endl;
return 0;
}
结果:
binary_search.png
前两个参数是iterator(迭代器),表示查找半闭合区间的前后两个位置。
这里的Student沿用前面的定义。
struct StuCount {
private:
int num ;
public:
StuCount(const int num) {
this->num = num;
}
bool operator()(const Student & stu) {
return stu.getNumber() == num;
}
};
int main()
{
vector<Student> v;
v.push_back(Student(1, "one"));
Student stu2(2, "two");
v.push_back(stu2);
v.push_back(stu2);
v.push_back(Student(3, "three"));
v.push_back(Student(4, "four"));
v.push_back(Student(5, "five"));
printStuV(v);
int sum=count_if(v.begin(), v.end(), StuCount(2));
cout << "vStu count_if 2: " << sum << endl;
vector<int> vNum;
vNum.push_back(1);
vNum.push_back(1);
vNum.push_back(1);
vNum.push_back(2);
cout << "vNum count 1: " << count(vNum.begin(),vNum.end(),1) << endl;
cout << "vNum count_if 1: " << count_if(vNum.begin(), vNum.end(),bind2nd( greater<int>(), 1)) << endl;
return 0;
}
结果:
count.png
struct StuCount {
private:
int num ;
public:
StuCount(const int num) {
this->num = num;
}
bool operator()(const Student & stu) {
return stu.getNumber() == num;
}
};
int main()
{
vector<Student> v;
v.push_back(Student(1, "one"));
Student stu2(2, "two");
v.push_back(stu2);
v.push_back(stu2);
v.push_back(Student(3, "three"));
v.push_back(Student(4, "four"));
v.push_back(Student(5, "five"));
printStuV(v);
vector<Student>::iterator itStuRes=find_if(v.begin(), v.end(), StuCount(2));
if (itStuRes != v.end()) {
itStuRes->print();
}
vector<int> vNum;
vNum.push_back(1);
vNum.push_back(1);
vNum.push_back(1);
vNum.push_back(2);
vector<int>::iterator itNumRes=find(vNum.begin(), vNum.end(), 2);
cout <<"num find " << *itNumRes << endl;
pair<vector<int>::iterator,vector<int>::iterator> pRes=equal_range(vNum.begin(), vNum.end(), 1);
cout << "equal_range left:" << *pRes.first << " right:" << *pRes.second << endl;
return 0;
}
结果:
find.png