#include<assert.h>#include<iostream>#include<algorithm>#include<functional>#include<vector>#include<cstdlib>#include<ctime>#include<iterator>// compare two numberstemplate<typenameT>boolcompare(T a, T b){return a < b;}template<typenameT>// absolute right because we use STLvoidrightMethod(std::vector<T>& vec){sort(vec.begin(), vec.end(), compare<T>);// sort(vec.begin(), vec.end(), std::less<T>()); // official}
std::default_random_engine e;
std::vector<int>generateRandom(int maxLen,int minValue,int maxValue){
std::uniform_int_distribution<int>distS(1, maxLen);
std::uniform_int_distribution<int>distV(minValue, maxValue);
std::vector<int> arr;int size =distS(e);
arr.resize(size);for(int i =0; i < size;++i){
arr[i]=distV(e);}return arr;}intmain(){
e.seed(time(NULL));inttestCount(10);// epochintmaxSize(10);// max size of the vectorintminValue(-100);// min valueintmaxValue(100);// maxvaluefor(int i =0; i < testCount;++i){
std::vector<int> vec1 =generateRandom(maxSize, minValue, maxValue);rightMethod(vec1);
std::copy(vec1.begin(), vec1.end(), std::ostream_iterator<int>(std::cout," "));
std::cout <<"\n";}return0;}
两个数组是否相等
template<typenameT>boolisEqual(std::vector<T>& vec1, std::vector<T>& vec2){if(vec1.size()!= vec2.size()){returnfalse;}else{returnequal(vec1.begin(), vec1.end(), vec2.begin());// STL equal}}intmain(){
std::vector<int> arr1 {1,2,3,4,4};
std::vector<int> arr2 {1,2,3,4,4};if(isEqual(arr1, arr2)){printf("equal ok\n");}else{printf("arr1, arr2 should equal , but not equal\n");}
arr1 ={1,2,3,4,4};
arr2 ={1,2,3,4,5};if(isEqual(arr1, arr2)){printf("arr1, arr2 should not equal , but equal\n");}else{printf("not equal ok\n");}
std::vector<std::string> str1 {"1","2","3","4","4"};
std::vector<std::string> str2 {"1","2","3","4","4"};if(isEqual(str1, str2)){printf("equal ok\n");}else{printf("str1, str2 should equal , but not equal\n");}
str1 ={"1","2","3","4","4"};
str2 ={"1","2","3","4","5"};if(isEqual(str1, str2)){printf("str1, str2 should not equal , but equal\n");}else{printf("not equal ok\n");}return0;}
拷贝数组
template<typenameT>voidcopyVector(std::vector<T>& vec1, std::vector<T>& vec2){if(vec2.empty()){// copy the vec2 to the vec1return;}
vec1 = vec2;}template<typenameT>boolisEqual(std::vector<T>& vec1, std::vector<T>& vec2){if(vec1.size()!= vec2.size()){returnfalse;}else{returnequal(vec1.begin(), vec1.end(), vec2.begin());// STL equal}}intmain(){
std::vector<int> arr1 {1,2,3,4,4};
std::vector<int> arr2;copyVector(arr2, arr1);if(isEqual(arr1, arr2)){printf("equal ok\n");}else{printf("arr1, arr2 should equal , but not equal\n");}
std::vector<std::string> str1 {"1","2","3","4","4"};
std::vector<std::string> str2;copyVector(str2, str1);if(isEqual(str1, str2)){printf("equal ok\n");}else{printf("str1, str2 should equal , but not equal\n");}return0;}
清空数组
std::vector<int>().swap(vec3);
对数器
#include<assert.h>#include<iostream>#include<algorithm>#include<functional>#include<vector>#include<cstdlib>#include<ctime>template<typenameT>// here we use different functional pointervoidtestAlgorithm(void(*sortFunc)(std::vector<T>&)){inttestCount(100000);// epochintmaxSize(10);// max size of the vectorintminValue(-100);// min valueintmaxValue(100);// maxvalueboolsuccessFlag(true);// flag
std::clock_t startTime, endTime;
startTime = std::clock();// start clockfor(int i =0; i < testCount;++i){
std::vector<int> vec1, vec2, vec3;generateRandomVector(vec1, maxSize, minValue,maxValue);// generate random vectorcopyVector(vec2, vec1);copyVector(vec3, vec1);// help to showrightMethod(vec1);// absolute right algorithmsortFunc(vec2);// we want to test the algorithmif(!isEqual(vec1, vec2)){
successFlag =false;// something wrongdisplay(vec3);break;}// clean the usage of the vector, std::vector<T> ().swap()
std::vector<int>().swap(vec1);
std::vector<int>().swap(vec2);
std::vector<int>().swap(vec3);}if(successFlag){
std::cout <<"Nice!"<< std::endl;}else{
std::cout <<"There may be something wrong!"<< std::endl;}
endTime = std::clock();
std::cout <<"Total usage is: "<<(endTime - startTime)/double(CLOCKS_PER_SEC)<<'s'<< std::endl;}//std::vector<>intmain(){testAlgorithm(insertSort<int>);// insert sortreturn0;}