多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 值查找 ~~~ bool Myprint(int v) { return v > 30; } void test01() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); vector<int>::iterator it=find(v.begin(), v.end(), 20); if (it == v.end()) { cout << "查找失败" << endl; } else { cout << "查找成功=" << *it << endl; } it=find_if(v.begin(), v.end(), Myprint); if (it == v.end()) { cout << "查找失败" << endl; } else { cout << "查找成功=" << *it << endl; } } ~~~ # 对象查找 ~~~ //查找对象 class Maker { public: Maker(string name, int age) { this->name = name; this->age = age; } //重载== bool operator==(const Maker& m) { return this->name == m.name && this->age == m.age; } public: string name; int age; }; struct MyFind:public binary_function<Maker,Maker,bool> { bool operator()(Maker m,Maker m2)const { return m.name == m2.name && m.age == m2.age; } }; void test02() { vector<Maker> v; v.push_back(Maker("aaa1", 18)); v.push_back(Maker("aaa2", 20)); v.push_back(Maker("aaa3", 21)); v.push_back(Maker("aaa4", 22)); v.push_back(Maker("aaa5", 23)); vector<Maker>::iterator it = find(v.begin(), v.end(), Maker("aaa2", 20)); if (it == v.end()) { cout << "查找失败" << endl; } else { cout << "查找成功=" << it->name<<" "<<it->age<< endl; } it = find_if(v.begin(), v.end(), bind2nd(MyFind(),Maker("aaa3",21))); if (it == v.end()) { cout << "查找失败" << endl; } else { cout << "查找成功=" << it->name << " " << it->age << endl; } } ~~~ # 查找相邻重复元素 adjacent_find算法 ~~~ @param beg 容器开始迭代器 @param end 容器结束迭代器 @param _callback 回调函数或者谓词(返回bool类型的函数对象) @return 返回相邻元素的第一个位置的迭代器 ~~~ ~~~ void test03() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); vector<int>::iterator it = adjacent_find(v.begin(), v.end()); if (it == v.end()) { cout << "查找相邻重复元素失败" << endl; } else { cout << "查找成功: " << *it << endl; } } ~~~ # 自定义查找重复相邻元素 ~~~ bool myadjacent_find(Maker2 &m1, Maker2 &m2) { return m1.name == m2.name && m1.age == m2.age; } void test03() { vector<Maker2> v2; v2.push_back(Maker2("aaa1", 10)); v2.push_back(Maker2("aaa4", 40)); v2.push_back(Maker2("aaa4", 40)); v2.push_back(Maker2("aaa4", 40)); v2.push_back(Maker2("aaa5", 50)); vector<Maker2>::iterator it2 = adjacent_find(v2.begin(), v2.end(), myadjacent_find); if (it2 == v2.end()) { cout << "查找相邻的重复元素失败" << endl; } else { cout << "查找成功:" << it2->name << " " << it2->age << endl; } } ~~~ # 二分查找法 binary_search算法 ~~~ 注意: 在无序序列中不可用 @param beg 容器开始迭代器 @param end 容器结束迭代器 @param value 查找的元素 @return bool 查找返回true 否则false ~~~ ~~~ class Student { public: string name; int age; public: Student(string name, int age) { this->name = name; this->age = age; } bool operator>(const Student &stu) const { return this->age > stu.age; } bool operator<(const Student &stu) const { return this->age < stu.age; } }; void test02() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); v.push_back(60); bool flg = binary_search(v.begin(), v.end(), 30); if (flg) { cout << "找到" << endl; } else { cout << "没有找到" << endl; } } ~~~ # 自定义二分查找 ~~~ vector<Student> vs; vs.push_back(Student("a1", 1)); vs.push_back(Student("a2", 2)); vs.push_back(Student("a3", 3)); vs.push_back(Student("a4", 4)); vs.push_back(Student("a5", 5)); /* bool binary_search(_FwdIt _First, _FwdIt _Last,const _Ty& _Val, _Pr _Pred) { _First = _STD lower_bound(_First, _Last, _Val, _Pred); return (_First != _Last && !_Pred(_Val, *_First)); } */ //存储对象,如果使用less,那么数据要是升序,并且要重载<,greater这需要数据是降序,并且要重载> bool flg2 = binary_search(vs.begin(), vs.end(), Student("a4", 4), greater<Student>()); if (flg2){ cout << "找到" << endl; } else { cout << "没有找到" << endl; } ~~~ # 统计 ~~~ vector<int> v; v.push_back(1); v.push_back(1); v.push_back(2); v.push_back(1); v.push_back(3); v.push_back(4); v.push_back(4); //查询1有多少个元素 int n=count(v.begin(), v.end(), 1); cout << n << endl; //大于2的元素有多少个 n = count_if(v.begin(), v.end(), [](int val)->bool{return val > 2; }); cout << n << endl; ~~~