zoukankan      html  css  js  c++  java
  • cpp algorithm

    今天在https://codility.com/ 上面测试了几道题,我发现用C++比用C 方便多了。因为如果用C++,我们可以使用STL 的结构及算法,但是使用C的话就要自己实现了。

    毕业之后 就没有专门做过算法题了,今天使用起来有些生硬,费事不少。现在将一些常用结构整理一下,作为自己的知识库储存在大脑里,以后可以信手拈来。

    一下提供一些C转C++的结构,以及C++常用算法。

    1. 常用函数

      get max integer

    // numeric_limits example
    #include <iostream>     // std::cout
    #include <limits>       // std::numeric_limits
    
    int main () {
      std::cout << std::boolalpha;
      std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '
    ';
      std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '
    ';
      std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '
    ';
      std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '
    ';
      std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '
    ';
      return 0;
    }

    min, max

    // min example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::min
    
    int main () {
      std::cout << "min(1,2)==" << std::min(1,2) << '
    ';
      std::cout << "min(2,1)==" << std::min(2,1) << '
    ';
      std::cout << "min('a','z')==" << std::min('a','z') << '
    ';
      std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '
    ';
      return 0;
    }

    sort

    // sort algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::sort
    #include <vector>       // std::vector
    
    bool myfunction (int i,int j) { return (i<j); }
    
    struct myclass {
      bool operator() (int i,int j) { return (i<j);}
    } myobject;
    
    int main () {
      int myints[] = {32,71,12,45,26,80,53,33};
      std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
    
      // using default comparison (operator <):
      std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33
    
      // using function as comp
      std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
    
      // using object as comp
      std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)
    
      // print out content:
      std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '
    ';
    
      return 0;
    }

    std::count

    // count algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::count
    #include <vector>       // std::vector
    
    int main () {
      // counting elements in array:
      int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
      int mycount = std::count (myints, myints+8, 10);
      std::cout << "10 appears " << mycount << " times.
    ";
    
      // counting elements in container:
      std::vector<int> myvector (myints, myints+8);
      mycount = std::count (myvector.begin(), myvector.end(), 20);
      std::cout << "20 appears " << mycount  << " times.
    ";
    
      return 0;
    }

    std::for_each

    // for_each example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::for_each
    #include <vector>       // std::vector
    
    void myfunction (int i) {  // function:
      std::cout << ' ' << i;
    }
    
    struct myclass {           // function object type:
      void operator() (int i) {std::cout << ' ' << i;}
    } myobject;
    
    int main () {
      std::vector<int> myvector;
      myvector.push_back(10);
      myvector.push_back(20);
      myvector.push_back(30);
    
      std::cout << "myvector contains:";
      for_each (myvector.begin(), myvector.end(), myfunction);
      std::cout << '
    ';
    
      // or:
      std::cout << "myvector contains:";
      for_each (myvector.begin(), myvector.end(), myobject);
      std::cout << '
    ';
    
      return 0;
    }

    2. C 的array转 C++  vector

    vector 的构造函数如下所示,

     // constructors used in the same order as described above:
      std::vector<int> first;                                // empty vector of ints
      std::vector<int> second (4,100);                       // four ints with value 100
      std::vector<int> third (second.begin(),second.end());  // iterating through second
      std::vector<int> fourth (third);                       // a copy of third
    
      // the iterator constructor can also be used to construct from arrays:
      int myints[] = {16,2,77,29};
      std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) )

    int A[] = {1, 2, 3, 4, 5}

    std::vector<int>  vec(A, A+sizeouf(A)/sizeof(int))   //

    3. 常用算法

    1)动态规划例子

    最长公共子序列:给定两个数组A和B,求其最长公共子序列。

    最长递增子序列:给定数组A, 求其最长递增子序列。

  • 相关阅读:
    正则里的.*?
    无边框缩放
    平台 测试笔记
    eclipse快捷键
    linux笔记
    笔记
    wamp、wordpress
    java-selenium
    html/css笔记
    selenium2——ruby
  • 原文地址:https://www.cnblogs.com/harrysun/p/4543081.html
Copyright © 2011-2022 走看看