[关闭]
@2017libin 2019-06-17T07:54:12.000000Z 字数 3548 阅读 58

c++ 中常用的内置函数

c++


1. algorithm中处理数组/vect的函数

  1.可以处理两种数据结构的函数只要将参数vect.begin()和array以及vect.end()和array+n(n为元素个数)相互转换就可以处理不同的结构了,下面是只给出了vector的参数形式。(注:下面的函数都是返回一个指向目标元素的iterator,并且可以用*iterator来输出目标元素,所以接下来当做返回的是一个地址)

函数头 函数功能
sort(vect.begin(),vect.end(),function) 无function默认为升序排序
reverse(vect.begin(),vect.end()) 逆置数组,在原有的数组上面修改数组
max_element(vect.begin(),vect.end()) 返回数组中最大元素的地址
min_element(vect.begin(),vect.end()) 返回数组中最小元素的地址
count(vect.begin(),vect.end(),x) 返回数组中x出现的次数
find(vect.begin(),vect.end(),x) 返回数组中第一次出现x的地址,不存在否则返回最后一个元素的地址(注:find函数操作数组时第二个参数为array+n-1,n为数组的大小)
next_permutation(vect.begin(),vect.end()) 使用数组元素生成一种新的排列
prev_permutation(vect.begin(),vect.end()) 使用数组元素生成上一种排列
binary _search(vect.begin(),vect.end(),x) 基于有序数组进行二分查找x,存在返回1否则返回0
lower_bound(vect.begin(),vect.end(),x) 返回数组中第一个不小于x的数的地址
upper_bound(vect.begin(),vect.end(),x) 返回数组中第一个大于x的数的地址

 (注:以上3个函数应是基于升序排序的数据进行操作)

2. math.h库中的一些方法

方法
log10(double x)
log(double x)
pow(double x, double y)
sqrt(double x)

3. iomanip库中的格式控制函数

函数原型 函数功能
setw(int i) 预设宽度,不够宽度前面空格补齐
setfill(char c) 预设宽度中没使用完的宽度用c填充
setbase(int i) 将数字设置为i进制
setprecision(int i) 设置有效数字位数
setiosflags(ios::right) 输出右对齐
setiosflags(ios::left) 输出左对齐
setiosflags(ios::fixed) 与setprecision合用输出定点小数

4. 其他一些常用的内置函数

头文件 函数原型 函数功能
algorithm max,min(value1, value2) 比较返回较大/较少值
algorithm swap(value1,value2) 交换value1和value2的值
numeric accumulate(array.first, array.last, init) 返回值为init + 数组元素求和。
cstring memset(void* str, int ch, size_t n) 用 ch 初始化首地址为str,内存大小为n的空间

5. 一些简单的技巧

6. 关于algorithm函数的一些实例代码

  1. //自定义升序函数
  2. bool down_function(int i, int j) { return i > j; }
  3. int main() {
  4. int arr[] = { 10, 12, 23, 10, 5, 2, 22, 20, 20, 11 };
  5. vector<int> vect(arr, arr + 10);
  6. //sort函数默认排序
  7. sort(vect.begin(), vect.end());
  8. sort(arr, arr + 10);
  9. //vector的新的一种访问方式
  10. cout << "sort vect: ";
  11. for (int i = 0; i < vect.size(); ++i)
  12. cout << vect.at(i) << " ";
  13. cout << endl << "sort array: ";
  14. for (int i = 0; i < 10; ++i)
  15. cout << arr[i] << " ";
  16. //自定义排序,up_function()不需要传入参数
  17. sort(vect.begin(), vect.end(), down_function);
  18. cout << endl << "sort vect by down_function: ";
  19. for (int i = 0; i < vect.size(); ++i)
  20. cout << vect[i] << " ";
  21. //reverse函数使用
  22. reverse(arr, arr + 10);
  23. cout << endl << "reverse array: ";
  24. for (int i = 0; i < 10; ++i)
  25. cout << arr[i] << " ";
  26. cout << endl;
  27. //max_element函数的使用
  28. cout << "max_element: " << *max_element(arr, arr + 10) << endl;
  29. //min_element函数的使用
  30. cout << "min_element: " << *min_element(arr, arr + 10) << endl;
  31. //count函数的使用
  32. cout << "count 1: " << count(arr, arr + 10, 1) << endl << "count 10: " << count(arr, arr + 10, 10) << endl;
  33. //find函数的使用
  34. cout << "find 10: " << *find(arr, arr + 9, 10) << endl << "find 15: " << *find(arr, arr + 9, 15) << endl;
  35. //binary_search函数的使用
  36. sort(arr, arr + 10);
  37. cout << "search 10: " << binary_search(arr, arr + 10, 11) << endl << "search 15: " << binary_search(arr, arr + 10, 15) << endl;
  38. //lower_bound和upper_bound函数的使用
  39. sort(vect.begin(), vect.end());
  40. auto p = lower_bound(vect.begin(), vect.end(), 10);
  41. auto q = upper_bound(vect.begin(), vect.end(), 10);
  42. cout << "第一个不小于10的元素位置为:" << p - vect.begin() << endl;
  43. cout << "第一个大于10的元素位置为:" << q - vect.begin() << endl;
  44. //next_permutation和prev_permutation函数的使用
  45. for (int i = 0; i < 10; ++i)
  46. cout << arr[i] << " ";
  47. next_permutation(arr, arr + 10);
  48. cout << endl << "next_permutation: ";
  49. for (int i = 0; i < 10; ++i)
  50. cout << arr[i] << " ";
  51. prev_permutation(arr, arr + 10);
  52. cout << endl << "prev_permutation: ";
  53. for (int i = 0; i < 10; ++i)
  54. cout << arr[i] << " ";
  55. system("pause");
  56. return 0;
  57. }
  58. }

输出结果

  1. sort vect: 2 5 10 10 11 12 20 20 22 23
  2. sort array: 2 5 10 10 11 12 20 20 22 23
  3. sort vect by down_function: 23 22 20 20 12 11 10 10 5 2
  4. reverse array: 23 22 20 20 12 11 10 10 5 2
  5. max_element: 23
  6. min_element: 2
  7. count 1: 0
  8. count 10: 2
  9. find 10: 10
  10. find 15: 2
  11. search 10: 1
  12. search 15: 0
  13. 第一个不小于10的元素位置为:2
  14. 第一个大于10的元素位置为:4
  15. 2 5 10 10 11 12 20 20 22 23
  16. next_permutation: 2 5 10 10 11 12 20 20 23 22
  17. prev_permutation: 2 5 10 10 11 12 20 20 22 23
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注