关于C++常用库函数

*****sort函数

 sort包含于<algorithm>头文件

#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
  int a[1000];
  cin>>n;
   for(int i = 1;i<=n;i++)
  {
    cin>>a[i];
  }
   sort(a+1,a+1+n);//此时已经排好了,是升序
  //对于vector
   vector<int>arr={1,5,6,98,5,3}
   sort(v.begin(),v.end());//此时也排好了,是升序
return 0; 
}
//想要降序等操作 sort(arr.begin(),arr.end(),cmp);
//再写一个函数bool cmp(int &x,int &y)
 // {
 //      return x>y;
 // }

****关于max min的函数

单纯用max和min只能传入两个数,例如max(4,9)或者一个列表min({1,5,8,7,6,5});不能传入数组,传入数组可看如下操作

关于max_element和min_element的用法

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int>arr = { 1,5,8,6,4,3 };
	int mm = *min_element(arr.begin(), arr.end());//min_element返回的是目标值的地址,要解引用
	cout << *max_element(arr.begin(), arr.end())<<" " << mm;
	return 0;
}

*****binary_search的用法

是库函数中二分查找的函数,返回bool(布尔类型)下面分别展示找容器中的目标和找数组中的目标

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	vector<int>arr = { 1,5,8,6,4,3 ,8,5,6,89,0,88,88,88,11,2};
    int nums[] = {1,2,3,4,5,6,7,8,9};
	int target = 6;
 if (found1)
		cout << "666";
	else
		cout << "no found";
	int sz = sizeof(nums) / sizeof(nums[0]);
	cout << sz << "\n";
	bool found2 = binary_search(nums, nums+sz, 1);//要正确计算sz所以定义nums时候[]内不加数字,如果加了,sz就是成为那个加了的数字了。
	cout << found2;
	if (found2)
		cout << "6";
	else
		cout << "bu6";
	return 0;
}

****lower_bound 和 up_bound的用法

lower_bound:返回第一个大于等于x的地址,因为是地址,所以需要减去首元素地址

upper_bound:返回第一个大于x的地址,也要减去首元素地址

#include<iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
	vector<int> v = { 1,4,8,6,6,9,8,8,8,4 };
	sort(v.begin(), v.end());
	for (auto i : v)cout << i << " ";
	int aa = lower_bound(v.begin(), v.end(), 8) - v.begin();//lower_bound返回第一个大于等于x的下标
	int bb = upper_bound(v.begin(), v.end(), 8) - v.begin();//upper_bound返回第一个大于x的下标,在这里,x代表的就是8
	cout << bb - aa;//此时,bb-aa代表的数字就是x这个数字的区间长度
	printf("[%d,%d]", aa, bb-1);//对于x的区间
	return 0;
}

上面是对于容器的解法,下面是对定义一个数组的示例

#include <iostream>
using namespace std;
int main()
{
	int data[200];
	int target;
	for (int i = 0; i < 200; i++)data[i] = 4 * i + 6;
	cin >> target;
	cout << (lower_bound(data, datr_bound+ 200, target)-data)lower_bound返回是目标数字的地址,还得减去第一个元素的地址
	return 0;
}

*****memset

常用的数组初始化函数

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int arr[10] = { 0 };//第一种方法初始化arr中数字全部为0
	for (auto i : arr) { 
		cout << i << " ";
	}
	cout << "\n";
	memset(arr, 0,sizeof(arr));//第二个方法,将arr数组全部归零,括号内为(首元素地址,每8个bit位的参数,字节大小)这里sizeof(arr)是4*10=40
	for (auto o : arr)//传入参数是根据每8个bit位来传的,如果传入1,就是01 01 01 01可以在调试中看
	{
		cout << o << " ";
	}
	return 0;
}

*****swap函数

就是传入两个值,然后会交换,如swap(a,b)太简单了不写了

*****reverse函数

****reverse函数(使数组或容器中数字倒序)
包含于头文件#include <algorithm>中
设定一个容器或者数组示例:
vector<int> v ={1,5,8,6,4,9};
reverse(v.begin(),v.end());
此时再打印v中的数字,就是倒序了


****unique函数

unique函数是将一个有序的数组去除重复的元素,将去除的数字排在数组后面

#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
	int arr[] = {1,6,8,4,2,2,3,5,8,8};
	int sz = sizeof(arr) / sizeof(arr[0]);
	sort(arr, arr +sz);
	int n = unique(arr, arr + sz)-arr;//unique返回的是地址,地址-地址的二者直接的元素数量,即n
	for (int i = 0; i < n; i++)cout << arr[i];//前n个数是去重后的数字,如果还要打印后面的则调整n变大即可
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值