<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [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; ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看