今天在 上面测试了几道题,我发现用C++比用C 方便多了。因为如果用C++,我们可以使用STL 的结构及算法,但是使用C的话就要自己实现了。
毕业之后 就没有专门做过算法题了,今天使用起来有些生硬,费事不少。现在将一些常用结构整理一下,作为自己的知识库储存在大脑里,以后可以信手拈来。
一下提供一些C转C++的结构,以及C++常用算法。
1. 常用函数
get max integer
// numeric_limits example#include // std::cout#include // std::numeric_limitsint main () { std::cout << std::boolalpha; std::cout << "Minimum value for int: " << std::numeric_limits ::min() << '\n'; std::cout << "Maximum value for int: " << std::numeric_limits ::max() << '\n'; std::cout << "int is signed: " << std::numeric_limits ::is_signed << '\n'; std::cout << "Non-sign bits in int: " << std::numeric_limits ::digits << '\n'; std::cout << "int has infinity: " << std::numeric_limits ::has_infinity << '\n'; return 0;}
min, max
// min example#include // std::cout#include // std::minint 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 // std::cout#include // std::sort#include // std::vectorbool myfunction (int i,int j) { return (i
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
::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}
// count algorithm example#include // std::cout#include // std::count#include // std::vectorint 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 myvector (myints, myints+8); mycount = std::count (myvector.begin(), myvector.end(), 20); std::cout << "20 appears " << mycount << " times.\n"; return 0;}
// for_each example#include // std::cout#include // std::for_each#include // std::vectorvoid myfunction (int i) { // function: std::cout << ' ' << i;}struct myclass { // function object type: void operator() (int i) {std::cout << ' ' << i;}} myobject;int main () { std::vector 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
// constructors used in the same order as described above: std::vector first; // empty vector of ints std::vector second (4,100); // four ints with value 100 std::vector third (second.begin(),second.end()); // iterating through second std::vector 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 fifth (myints, myints + sizeof(myints) / sizeof(int) )
3. 常用算法