码迷,mamicode.com
首页 > 其他好文 > 详细

cpp algorithm

时间:2015-06-01 06:08:53      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

今天在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() << \n;
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << \n;
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << \n;
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << \n;
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << \n;
  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) << \n;
  std::cout << "min(2,1)==" << std::min(2,1) << \n;
  std::cout << "min(‘a‘,‘z‘)==" << std::min(a,z) << \n;
  std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << \n;
  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 << \n;

  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.\n";

  // 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.\n";

  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 << \n;

  // or:
  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);
  std::cout << \n;

  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. 后面我将花一段时间学习和整理一下 子列相关算法。

cpp algorithm

标签:

原文地址:http://www.cnblogs.com/harrysun/p/4543081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!