蓝桥杯——常用库函数

本文介绍了C++编程中的各种实用库函数,如取消数据流处理、浮点数精度控制、大小写转换、排序函数(如sort和二分查找)、以及全排列、最值查找和nth_element等。通过实例展示了如何在实际编程中应用这些功能。
摘要由CSDN通过智能技术生成

万能头文件 

目录

万能头文件 

取消数据流

浮点数

 大小写转换

排序函数

二分查找

binary_search(,,)

lower_bound(大于等于)和upper_bound(大于)

其他库函数

全排列 

 最值查找

max min min_element  max_element

nth_element


#include <bits/stdc++.h>

取消数据流

数据量较大时

ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

浮点数

精确到小数点后三位 

float x = 3.1415926;
cout << fixed << setprecision(3) << x;

 大小写转换

islower(ch1);
isupper(ch2);
char ch3=tolower(ch2);
char ch5=ch2-'A'+'a';
char ch4=toupper(ch1);
​
#include <bits/stdc++.h>
using namespace std;

char converted(char ch) {
	if (islower(ch)) ch = toupper(ch);
	else if (isupper(ch)) ch = tolower(ch);
	return ch;
}
int main() {
	string s;
	getline(cin, s);
	for (auto& i : s) {
		i = converted(i);
	}
	cout << s << "\n";
	return 0;
}

​

排序函数

1.范围是[st,end)

2.第三个参数默认是升序

sort(st,end)

二分查找

binary_search(,,)

1.可以用向量代替数组

2.binary_search()  只能用于单调的数据,返回值是布尔类型

3.三个参数

#include <bits/stdc++.h>
using namespace std;
int main() {
	vector<int> numbers = { 1,3,5,7,9 };
	int target = 5;
	bool found = binary_search(numbers.begin(), numbers.end(), target);
	if (found) cout << "found";
	else cout << "not found";
	return 0;
}

lower_bound(大于等于)和upper_bound(大于)

1.不一定要先排序 只要是非降序的就行

2.数组的变量名直接代表数组的首地址,而容器则提供 data()begin() 函数来获取首元素的地址。

3.lower_bound(st,end,x)        范围是[st,end)        不存在则返回的是end

#include <bits/stdc++.h>
using namespace std;
int main() {
	vector<int> v= {5, 1, 7, 3, 10, 18, 9};
	cout << v[1];
	sort(v.begin(), v.end());

	for (auto& i : v) {
		cout << i << ' ';
	}
	cout << "\n";

	//找到数组中第一个大于等于8的位置 下标
	cout << (lower_bound(v.begin(), v.end(), 8) - v.begin()) << "\n";
}

其他库函数

两个数据的交换  swap()

int a = 10;
	int b = 9;
	swap(a, b);

数据的反转reverse()  前闭后开

#include <bits/stdc++.h>
using namespace std;

int main() {
	vector<int> vec = { 1,2,3,4,5 };
	reverse(vec.begin(), vec.end());

	for (int num : vec) {
		cout << num;
	}
}

unique

#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[] = { 1,1,2,3,4 };
	int n = unique(a, a + 5) - a;
	for (int i = 0; i < n; ++i) cout << a[i] << " ";
	return 0;
}

全排列 

 要枚举数组的所有情况

#include <bits/stdc++.h>
using namespace std;
int a[10];
int main() {
	for (int i = 1; i <= 4; i++) a[i] = i;

	bool tag = true;
	while (tag)
	{
		for (int j = 1; j <= 4; j++) cout << a[j] << " ";
		cout << "\n";
		tag = next_permutation(a + 1, a + 1 + 4);
	}

	return 0;
}

 最值查找

max min min_element  max_element

#include <bits/stdc++.h>
using namespace std;
int main() {
	int a[2] = { 1,2 };
	vector<int> vec = { 1,2 };
	min(1, 2);
	min({ 1,2,3,4 });
	//min(a10);
	int minel = *min_element(a, a + 1 +1);
	int maxel = *max_element(a, a + 1+1);

	int minelvec = *min_element(vec.begin(), vec.end());
	int maxelvec = *max_element(vec.begin(), vec.end());

	cout << minel << maxel << minelvec << maxelvec;
}

nth_element

  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值