@2017libin
2019-06-17T07:54:12.000000Z
字数 3548
阅读 57
c++
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个函数应是基于升序排序的数据进行操作)
| 方法 |
|---|
| log10(double x) |
| log(double x) |
| pow(double x, double y) |
| sqrt(double x) |
| 函数原型 | 函数功能 |
|---|---|
| setw(int i) | 预设宽度,不够宽度前面空格补齐 |
| setfill(char c) | 预设宽度中没使用完的宽度用c填充 |
| setbase(int i) | 将数字设置为i进制 |
| setprecision(int i) | 设置有效数字位数 |
| setiosflags(ios::right) | 输出右对齐 |
| setiosflags(ios::left) | 输出左对齐 |
| setiosflags(ios::fixed) | 与setprecision合用输出定点小数 |
| 头文件 | 函数原型 | 函数功能 |
|---|---|---|
| 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的空间 |
//自定义升序函数bool down_function(int i, int j) { return i > j; }int main() {int arr[] = { 10, 12, 23, 10, 5, 2, 22, 20, 20, 11 };vector<int> vect(arr, arr + 10);//sort函数默认排序sort(vect.begin(), vect.end());sort(arr, arr + 10);//vector的新的一种访问方式cout << "sort vect: ";for (int i = 0; i < vect.size(); ++i)cout << vect.at(i) << " ";cout << endl << "sort array: ";for (int i = 0; i < 10; ++i)cout << arr[i] << " ";//自定义排序,up_function()不需要传入参数sort(vect.begin(), vect.end(), down_function);cout << endl << "sort vect by down_function: ";for (int i = 0; i < vect.size(); ++i)cout << vect[i] << " ";//reverse函数使用reverse(arr, arr + 10);cout << endl << "reverse array: ";for (int i = 0; i < 10; ++i)cout << arr[i] << " ";cout << endl;//max_element函数的使用cout << "max_element: " << *max_element(arr, arr + 10) << endl;//min_element函数的使用cout << "min_element: " << *min_element(arr, arr + 10) << endl;//count函数的使用cout << "count 1: " << count(arr, arr + 10, 1) << endl << "count 10: " << count(arr, arr + 10, 10) << endl;//find函数的使用cout << "find 10: " << *find(arr, arr + 9, 10) << endl << "find 15: " << *find(arr, arr + 9, 15) << endl;//binary_search函数的使用sort(arr, arr + 10);cout << "search 10: " << binary_search(arr, arr + 10, 11) << endl << "search 15: " << binary_search(arr, arr + 10, 15) << endl;//lower_bound和upper_bound函数的使用sort(vect.begin(), vect.end());auto p = lower_bound(vect.begin(), vect.end(), 10);auto q = upper_bound(vect.begin(), vect.end(), 10);cout << "第一个不小于10的元素位置为:" << p - vect.begin() << endl;cout << "第一个大于10的元素位置为:" << q - vect.begin() << endl;//next_permutation和prev_permutation函数的使用for (int i = 0; i < 10; ++i)cout << arr[i] << " ";next_permutation(arr, arr + 10);cout << endl << "next_permutation: ";for (int i = 0; i < 10; ++i)cout << arr[i] << " ";prev_permutation(arr, arr + 10);cout << endl << "prev_permutation: ";for (int i = 0; i < 10; ++i)cout << arr[i] << " ";system("pause");return 0;}}
输出结果
sort vect: 2 5 10 10 11 12 20 20 22 23sort array: 2 5 10 10 11 12 20 20 22 23sort vect by down_function: 23 22 20 20 12 11 10 10 5 2reverse array: 23 22 20 20 12 11 10 10 5 2max_element: 23min_element: 2count 1: 0count 10: 2find 10: 10find 15: 2search 10: 1search 15: 0第一个不小于10的元素位置为:2第一个大于10的元素位置为:42 5 10 10 11 12 20 20 22 23next_permutation: 2 5 10 10 11 12 20 20 23 22prev_permutation: 2 5 10 10 11 12 20 20 22 23