STL基础--算法(不修改数据的算法)

不修改数据的算法

  • 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} );  
    // 所有元素都不满足

猜你喜欢

转载自www.cnblogs.com/logchen/p/10202267.html
今日推荐