1. 程式人生 > >STL基礎--算法(不修改數據的算法)

STL基礎--算法(不修改數據的算法)

詞典 find ems ear sam \n 第一個 turn heap

不修改數據的算法

  • count, min and max, compare, linear search, attribute
// 算法中Lambda函數很常用:
num = count_if(vec.begin(), vec.end(), [](int x){return x<10;});  

bool lessThan10(int x) {
   return x<10;
}

vector<int> vec = {9,60,90,8,45,87,90,69,69,55,7};
vector<int> vec2 = {9,60,70,8,45,87};
vector<int>::iterator itr, itr2;
pair<vector<int>::iterator, vector<int>::iterator> pair_of_itr;

// C++ 03: 一些算法可以在tr1或者boost中找到
vector<int> vec = {9,60,90,8,45,87,90,69,69,55,7};
  1. 計數 Counting

    int n = count(vec.begin()+2, vec.end()-1, 69);   // 2 元素值等於69的個數
    int m = count_if(vec.begin(), vec.end(), [](int x){return x==69;}); // 3  元素滿足謂詞的個數
    int m = count_if(vec.begin(), vec.end(), [](int x){return x<10;}); // 3  
  2. 最大小值 Min and Max

    itr = max_element(vec.begin()+2, vec.end());  // 90 返回第一個最大元素的叠代器
    
    itr = max_element(vec.begin(), vec.end(), 
                  [](int x, int y){ return (x%10)<(y%10);}); // 9 自定義比較函數
    
    // 大多數算法有一個簡單形式和一個更通用的形式
    
    itr = min_element(vec.begin(), vec.end());  // 7 
    // 通用形式的min_element()
    
    pair_of_itr = minmax_element(vec.begin(), vec.end(),  // {60, 69} 
                              [](int x, int y){ return (x%10)<(y%10);}); 
    // 返回一個pair, 包含第一個最小值和最後一個最大值
  3. 線性搜索(當數據未排序時使用)

    //    返回第一個匹配的
    itr = find(vec.begin(), vec.end(), 55);
    
    itr = find_if(vec.begin(), vec.end(), [](int x){ return x>80; });
    
    itr = find_if_not(vec.begin(), vec.end(), [](int x){ return x>80; });
    
    itr = search_n(vec.begin(), vec.end(), 2, 69);  // 連續2個69
    // 通用形式的search_n()
    
    // 搜索子串
    vector<int> sub = {45, 87, 90};
    itr = search( vec.begin(), vec.end(), sub.begin(), sub.end()); 
          // 搜索第一個匹配的子串 
    itr = find_end( vec.begin(), vec.end(), sub.begin(), sub.end());
          // 搜索最後一個匹配的子串
    // 通用形式: search(), find_end()
    
    
    
    // 搜索任意一個
    vector<int> items  = {87, 69};
    itr = find_first_of(vec.begin(), vec.end(), items.begin(), items.end()); 
          // 搜索任意一個在items中的元素
    itr = find_first_of(vec.begin(), vec.end(), items.begin(), items.end(),
                      [](int x, int y) { return x==y*4;}); 
          // 搜索任意一個在items中的元素,且滿足謂詞
    
    // 搜索相鄰
    itr = adjacent_find(vec.begin(), vec.end());  // 搜索相鄰兩個相同的元素
    itr = adjacent_find(vec.begin(), vec.end(), [](int x, int y){ return x==y*4;}); 
             // 通用版本,自定義謂詞
  4. 範圍比較

    if (equal(vec.begin(), vec.end(), vec2.begin())) {
      cout << "vec and vec2 are same.\n";   
    }
    
    if (is_permutation(vec.begin(), vec.end(), vec2.begin())) {
        cout << "vec and vec2 have same items, but in differenct order.\n"; 
    }
    
    pair_of_itr = mismatch(vec.begin(), vec.end(), vec2.begin());  
    // 找到第一個不同的元素
    // pair_of_itr.first是vec的叠代器
    // pair_of_itr.second是vec2的叠代器
    
    //詞典比較: 用"less than"逐元素比較
    lexicographical_compare(vec.begin(), vec.end(), vec2.begin(), vec2.end());
    // {1,2,3,5} < {1,2,4,5}
    // {1,2}     < {1,2,3}
    
    // 通用形式: 
    //   equal(), is_permutation(), mismatch(), lexicographical_compare()
  5. 檢查屬性

    is_sorted(vec.begin(), vec.end());  // 檢查vec是否排序
    
    itr = is_sorted_until(vec.begin(), vec.end()); 
    // itr指向第一個不滿足排序的元素
    // 通用形式: is_sorted(), is_sorted_until()
    
    is_partitioned(vec.begin(), vec.end(), [](int x){return x>80;} );
            // 檢查vec是否由謂詞的條件分成了兩個部分(x>80)
    
    is_heap(vec.begin(), vec.end());  // 檢查vec是否是一個堆,heap
    itr = is_heap_until(vec.begin(), vec.end());  // 找到第一個不是堆的位置
    
    // 通用形式: is_heap(), is_heap_until()
  6. All, any, none

    all_of(vec.begin(), vec.end(), [](int x) {return x>80} );  
    // 所有的元素都滿足
    
    any_of(vec.begin(), vec.end(), [](int x) {return x>80} );  
    // 任意一個元素滿足
    
    none_of(vec.begin(), vec.end(), [](int x) {return x>80} );  
    // 所有元素都不滿足

STL基礎--算法(不修改數據的算法)