9. 包括——includes
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //includes 6 #include<algorithm> 7 #include<numeric> 8 9 using namespace std; 10 11 int main() 12 { 13 int all[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 14 int all2[4] = {4, 5, 8}; 15 16 bool y = includes(all, all + 10, all2 , all2 + 3);//判断all的前10个数中是否包含all2的三个数 17 18 if(y) 19 printf_s("yes"); 20 else 21 printf_s("no"); 22 23 return 0; 24 }
10. 最低边界——lower_bound
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //lower_bound 6 #include<algorithm> 7 #include<numeric> 8 9 using namespace std; 10 11 int main() 12 { 13 int all[11] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; 14 int nee = 9; 15 16 int *a = lower_bound(all, all + 10, nee);//找出nee可在序列all插入的第一个位置 17 printf("%d", a - all); 18 19 return 0; 20 }
11. 最大最小值——max, max_element, min, min_element
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //max, max_element, min, min_element 6 #include<algorithm> 7 #include<numeric> 8 using namespace std; 9 10 int main() 11 { 12 //printf_s("%d", max('a', 'b')); 13 //printf_s("%d", min('a', 'b')); 14 15 int all[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 16 //int *max = max_element(all, all + 11); 17 //printf_s("%d", *max); 18 int *min = min_element(all, all + 11); 19 printf_s("%d", *min); 20 21 return 0; 22 }
12. 合并——merge
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 //merge 6 #include<algorithm> 7 #include<numeric> 8 9 using namespace std; 10 11 int main() 12 { 13 int all[11]={0,1,2,3,4,5,6,7,8,10}; 14 int all2[11]={0,2,6,11,13}; 15 int all3[20]; 16 17 int *a = merge(all, all + 10, all2, all2 + 5, all3);//合并all的前10个元素以及all2的5个元素到all3中,注意自动进行了排序 18 int len = a - all3; 19 for(int i = 0 ; i < len ; i ++) 20 printf_s("%d ", all3[i]); 21 22 return 0; 23 }
13. 排列—— next_permutation
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int main() 6 { 7 int myints[] = {1, 2, 3, 4}; 8 cout << "The 3! possible permutations with 3 elements: "; 9 10 sort(myints, myints + 4); 11 do { 12 cout<<myints[0]<<" "<<myints[1]<<" "<<myints[2]<<" "<<myints[3]<<endl; 13 }while(next_permutation(myints, myints + 4));//排列 14 15 return 0; 16 }
15. 部分排序——partial_sort
#include<iostream> #include<algorithm> using namespace std; int main() { int all[11] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; partial_sort(all, all + 4 , all + 11);//1 2 3 4 11 10 9 8 7 6 5 for(int i = 0 ; i < 11 ; i ++) printf("%d ", all[i]); return 0; }
16. 快速排序——sort,qsort
1 //////=========First Sort——qsort===============//// 2 ///int 3 int num[100]; 4 int cmp(const void *a, const void *b) 5 { 6 return *(int *)a - *(int *)b; 7 } 8 qsort(num, 100, sizeof(num[0]), cmp); 9 10 ///char 11 char word[100]; 12 int cmp(const void *a, const void *b) 13 { 14 return *(char *)a - *(char *)b; 15 } 16 qsort(word, 100, sizeof(word[0]), cmp); 17 18 //double 19 couble in[100]; 20 int cmp(const void *a, const void *b) 21 { 22 return *(double *)a > *(double *)b ? 1 : -1; 23 } 24 qsort(word, 100, sizeof(word[0]), cmp); 25 26 //struct 27 struct In 28 { 29 double data; 30 int other; 31 }s[100]; 32 33 int cmp(const void *a, const void *b) 34 { 35 return (*(In *)a).data > (*(In *)b).data ? 1 : -1; 36 } 37 qsort(s, 100, sizeof(s[0]), cmp); 38 39 //string 40 char str[100][100]; 41 int cmp(const void *a, const void *b) 42 { 43 return strcmp((char *)a, (char *)b); 44 } 45 qsort(str, n , sizoof(str[0]), cmp); //n是要排序的字符串的个数 46 47 //////=========Second Sort——sort===============//// 48 #include<iostream> 49 #include<algorithm> 50 using namespace std; 51 52 int main() 53 { 54 int a[20]; 55 for(int i = 0 ; i < 20 ; i ++) 56 cin>>a[i]; 57 58 sort(a, a + 20); 59 for(int i = 0 ; i < 20 ; i ++) 60 cout<<a[i]<<endl; 61 62 return 0; 63 } 64 //////=========Third——qsort与sort的区别===============//// 65 sort 是 qsort 的升级版,如果能用 sort 尽量用 sort,使用也比较简单,不像 qsort 还得自己去写 cmp 函数,只要注明 使用的库函数就可以使用,参数只有两个(如果是普通用法)头指针和尾指针;默认 sort 排序后是升序,如果想让他降序排列,可以使用自己编的 cmp 函数: 66 bool compare(int a, int b) 67 { 68 return a > b; 69 } 70 sort(*a, *b, cmp);
17. 堆排序—— sort_heap
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 using namespace std; 5 6 int main() 7 { 8 int myints[] = {10, 20, 30, 5, 15}; 9 vector<int> v(myints, myints + 5); 10 vector<int>::iterator it; 11 12 make_heap(v.begin(), v.end());//已经调整了堆 13 cout<<"Initial max heap :"<<v.front()<<endl; 14 15 pop_heap(v.begin(), v.end()); 16 v.pop_back(); 17 cout<<"Max heap after pop: "<<v.front()<<endl; 18 19 v.push_back(99); 20 push_heap(v.begin(), v.end()); 21 cout<<"Max heap after push: "<<v.front() <<endl; 22 23 sort_heap(v.begin(), v.end()); 24 25 cout<<"Final sorted range: "; 26 for(int i = 0 ; i < v.size() ; i ++) 27 cout<<" "<<v[i]; 28 29 cout<<endl; 30 31 return 0; 32 }