美文网首页
C++ 常用泛型算法

C++ 常用泛型算法

作者: wayyyy | 来源:发表于2018-07-12 16:34 被阅读0次
查找算法
  • all_of
    template< class InputIt, class UnaryPredicate >
    bool all_of(InputIt first, InputIt last, UnaryPredicate p );
    检查一元谓词 p 是否对范围 [first, last) 中所有元素返回 true
    
    // 应用:判断一个字符串是不是全部由数字组成
    string st = "01234";
    all_of(st.begin(), st.end(), isdigit());
    
  • any_of none_of
template< class InputIt, class UnaryPredicate >
bool all_of(InputIt first, InputIt last, UnaryPredicate p );
检查一元谓词 p 是否对范围 [first, last) 中所有元素返回 true 

template< class InputIt, class UnaryPredicate >
bool any_of(InputIt first, InputIt last, UnaryPredicate p );
检查一元谓词 p 是否对范围 [first, last) 中至少一个元素返回 true 

template< class InputIt, class UnaryPredicate >
bool none_of(InputIt first, InputIt last, UnaryPredicate p );
检查一元谓词 p 是否不对范围 [first, last) 中任何元素返回 true

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;

int main() 
{
    vector<int> v(2, 4, 6, 8, 9, 10. 12, 14);

    if (std::all_of(v.cbegin(), v.cend(), [](int i){    return i % 2 == 0;  }))  
        std::cout << "All numbers are even\n";
    
    if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(), std::placeholders::_1, 2))) 
        std::cout << "None of them are odd\n";
    
    class DivisibleBy 
    {
        public:
            DivisibleBy(int n) : d(n)   {   }
            bool operator()(int n) const    {   return n % d == 0;  }       
        private:
            const int d;
    };

    if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) 
        std::cout << "At least one number is divisible by 7\n";
}
  • partition
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <forward_list>
 
template <class ForwardIt>
 void quicksort(ForwardIt first, ForwardIt last)
 {
    if(first == last) 
        return;
    auto pivot = *std::next(first, std::distance(first,last)/2);

    ForwardIt middle1 = std::partition(first, last, 
                         [pivot](const auto& em){ return em < pivot; });
    
    ForwardIt middle2 = std::partition(middle1, last, 
                         [pivot](const auto& em){ return !(pivot < em); });

    quicksort(first, middle1);
    quicksort(middle2, last);
 }
 
int main()
{
    std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
    std::cout << "\nUnsorted list:\n    ";
    for (int n : fl)
        std::cout << n << ' ';
    std::cout << '\n';  
 
    quicksort(std::begin(fl), std::end(fl));
    std::cout << "Sorted using quicksort:\n    ";
    for(int fi : fl) std::cout << fi << ' ';
    std::cout << '\n';
}
排序
  • is_sorted
template< class ForwardIt >
bool is_sorted( ForwardIt first, ForwardIt last );

template< class ForwardIt, class Compare >
bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );

ForwardIt 必须满足 ForwardIterator 的要求。
  • is_sorted_until
template< class ForwardIt >
ForwardIt is_sorted_until( ForwardIt first, ForwardIt last );

template< class ForwardIt, class Compare >
ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, Compare comp );

检验范围 [first, last) ,并寻找始于 first 且其中元素已以升序排序的最大范围。
ForwardIt 必须满足 ForwardIterator 的要求。
  • sort
template< class RandomIt >
void sort( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

RandomIt 必须满足 ValueSwappable 和 RandomAccessIterator 的要求。
最小最大操作
  • max
template< class T > 
const T& max( const T& a, const T& b );

template< class T, class Compare >
const T& max( const T& a, const T& b, Compare comp );

template< class T >
T max(std::initializer_list]initializer_list<T> ilist );

template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp );
  • max_element
template< class ForwardIt > 
ForwardIt max_element(ForwardIt first, ForwardIt last );

template< class ForwardIt, class Compare >
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp );
二分搜索操作(在已排序范围上)
template< class ForwardIt, class T >
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value);

template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp);

ForwardIt 必须满足 ForwardIterator 的要求。
指向首个不小于 value 的元素的迭代器,或若找不到这种元素则为 last 
集合操作

修改序列
  • transfrom
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,
                    UnaryOperation unary_op );

template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, 
                    OutputIt d_first, BinaryOperation binary_op );

-InputIt, InputIt1, InputIt2 必须满足 InputIterator 的要求。
-OutputIt 必须满足 OutputIterator 的要求。
-ForwardIt1, ForwardIt2, ForwardIt3 必须满足 ForwardIterator 的要求。

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
 
int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c) -> unsigned char { return std::toupper(c); });
}
  • generate
template< class ForwardIt, class Generator >
void generate( ForwardIt first, ForwardIt last, Generator g );

-ForwardIt 必须满足 ForwardIterator 的要求。

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v(5);
    std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
 
    std::cout << "v: ";
    for (auto iv: v)
        std::cout << iv << " ";
    std::cout << "\n";
}
  • std::remove 和 std::remove_if
template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );

template< class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );

-ForwardIt 必须满足 ForwardIterator 的要求。
-解引用 ForwardIt 结果的类型必须满足 MoveAssignable 的要求。
-UnaryPredicate 必须满足 Predicate 的要求。

从 string 移除所有空格,通过迁移所有非空格字符到左侧,再擦除其他内容。这是擦除移除手法的样例。

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
 
int main()
{
    std::string str1 = "Text with some   spaces";
    str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());
    std::cout << str1 << '\n';
 
    std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
    str2.erase(std::remove_if(str2.begin(), 
                              str2.end(),
                              [](unsigned char x){return std::isspace(x);}),
               str2.end());
    std::cout << str2 << '\n';
}

相关文章

  • C++ 常用泛型算法

    查找算法 all_oftemplate< class InputIt, class UnaryPredicate ...

  • Golang泛型编程初体验

    序言 众所周知,Golang中不支持类似C++/Java中的标记式泛型,所以对于常用算法,比如冒泡排序算法,有些同...

  • Geekband-third week of part3

    1.泛型算法之变易算法 2.泛型算法之排序 3.泛型算法之泛型数值算法 4.内存分配器

  • C++模板

    C++模板和泛型程序设计 泛型程序设计(generic programming)是一种算法在实现时不指定具体要操作...

  • 迭代器

    在C++中,泛型编程时一个重要的特性。与面向对象不同的是,泛型编程关注的是算法。泛型编程旨在编写独立于数据类型的代...

  • 10泛型算法

    10泛型算法 10.1概述 泛型算法不能改变容器的大小,依赖于元素类型的操作。 10.2初识泛型算法 10.2.1...

  • 从c到c++快速入门2019

    C++语言·C++语言是对C语言的扩展和增强:面向对象、通用算法(泛型编程)·1979年,贝尔实验室Bjarne ...

  • 理顺iOS(一)泛型

    1.1 泛型介绍 泛型的概念最早出自C++,Swift的泛型与其设计思路相同,与Java不同。 优缺点 C++与S...

  • c++primer笔记----泛型算法

    大多数算法在 。泛型算法运行在迭代器之上 数组利用指针实现泛型算法 只读算法:find、count、accumul...

  • Android 学习(一):Java 泛型

    Java泛型学习 1.0 泛型常用案例 2.0 泛型方法 3.0 泛型类 4.0 通配符 ? 通配符,占位符,标识...

网友评论

      本文标题:C++ 常用泛型算法

      本文链接:https://www.haomeiwen.com/subject/mihuuftx.html