<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 對排序向量中的二分搜索 > 原文: [https://www.geeksforgeeks.org/binary-search-sorted-vector-pairs/](https://www.geeksforgeeks.org/binary-search-sorted-vector-pairs/) 給定向量按其第一個值(關鍵字)排序的情況下,如何將 [STL binary_search](https://www.geeksforgeeks.org/binary-search-algorithms-the-c-standard-template-library-stl/) 應用于對(關鍵字,值)的[向量](https://www.geeksforgeeks.org/vector-in-cpp-stl/) 代碼中的**結構比較**包含兩個函數,將鍵(搜索元素)與向量中的第一個元素進行比較 ``` /* C++ code to demonstrate how Binary Search ???can be applied on a vector of pairs */ #include <bits/stdc++.h> using namespace std; struct compare { ????bool operator()(const pair<int, int>& value,? ????????????????????????????????const int& key) ????{ ????????return (value.first < key); ????} ????bool operator()(const int& key,? ????????????????????const pair<int, int>& value) ????{ ????????return (key < value.first); ????} }; int main() { ????// initializing the vector of pairs ????vector<pair<int, int> > vect; ????// insertion of pairs (key, value) in vector vect ????vect.push_back(make_pair(1, 20)); ????vect.push_back(make_pair(3, 42)); ????vect.push_back(make_pair(4, 36)); ????vect.push_back(make_pair(2, 80)); ????vect.push_back(make_pair(7, 50)); ????vect.push_back(make_pair(9, 20)); ????vect.push_back(make_pair(3, 29)); ????// sorting the vector according to key ????sort(vect.begin(), vect.end()); ????// printing the sorted vector ????cout << "KEY" << '\t' << "ELEMENT" << endl; ????for (pair<int, int>& x : vect) ????????cout << x.first << '\t' << x.second << endl; ????// searching for the key element 3 ????cout << "search for key 3 in vector" << endl; ????if (binary_search(vect.begin(), vect.end(), ??????????????????????????????????3, compare())) ????????cout << "Element found"; ????else ????????cout << "Element not found"; ????return 0; } ``` **輸出**: ``` KEY ELEMENT 1 20 2 80 3 29 3 42 4 36 7 50 9 20 search for key 3 in vector Element found ``` 上面的 binary_search 操作具有時間復雜度 **O(lg n)** * * * * * *
                  <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>

                              哎呀哎呀视频在线观看