C++中所有开发者都应了解的实用内置函数
C++内置函数是C++标准库的一部分,这些函数旨在提供编程中经常需要的通用和基本功能。本文将介绍C++中一些最常用的内置函数,以便您可以在代码中使用它们。
我们将涵盖的内容:
- sqrt()函数
- pow()函数
- sort()函数
- find()函数
- binary_search()函数
- max()函数
- min()函数
- swap()函数
- toupper()函数
- tolower()函数
sqrt()函数
sqrt()函数用于计算double类型值的平方根,定义在头文件中。
语法:
参数:该函数只接受一个double类型的参数,即要计算平方根的数字。
返回类型:double类型的平方根值。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// C++程序演示sqrt()函数的使用
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double x = 100;
double answer;
// 使用sqrt()函数计算数字的平方根
answer = sqrt(x);
// 打印结果
cout << answer << endl;
return 0;
}
|
输出:
pow()函数
pow()函数用于计算给定数字的幂次方,也定义在头文件中。
语法:
1
|
double pow(double x, double y);
|
参数:
返回类型:x的y次方的值。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// C++程序演示pow()函数的使用
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
// 声明整数变量'base'
int base = 5;
// 声明整数变量'exponent'
int exponent = 3;
// pow(5, 3)表示5^3,即5*5*5 = 125
// 使用pow()函数计算base的exponent次方
int answer = pow(base, exponent);
// 输出结果
cout << answer << endl;
}
|
输出:
sort()函数
sort()函数是STL头文件的一部分,是一个函数模板,可用于排序随机访问容器,如向量、数组等。
语法:
1
|
sort(arr, arr + n, comparator)
|
参数:
- arr:指向数组第一个元素的指针或迭代器
- arr + n:指向数组最后一个元素之后虚拟元素的指针
- comparator:一元谓词函数,用于按特定顺序排序值。默认值按升序排序数组
返回值:此函数不返回任何值。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream>
#include <algorithm> // 包含sort()函数的头文件
using namespace std;
int main()
{
// 声明并初始化一个包含未排序元素的整数数组
int arr[] = { 13, 15, 12, 14, 11, 16, 18, 17 };
// 计算数组中的元素数量
int n = sizeof(arr) / sizeof(arr[0]);
// 使用算法库中的内置sort()函数
sort(arr, arr + n);
// 使用循环打印排序后的数组
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
|
输出:
1
|
11 12 13 14 15 16 17 18
|
find()函数
find()函数也是STL库的一部分,用于在给定范围内查找值。它可以用于排序和未排序的数据集,因为它实现了线性搜索算法。
语法:
1
|
find(startIterator, endIterator, key)
|
参数:
- startIterator:迭代到范围的开始
- endIterator:迭代到范围的结束
- key:要搜索的值
返回值:如果找到元素,则迭代器设置为该元素。否则,迭代到结束。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// C++程序演示find()函数的使用
#include <algorithm> // 需要find()函数
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 初始化一个向量
vector<int> dataset{ 12, 28, 16, 7, 33, 43 };
// 使用find()函数搜索值7
auto index = find(dataset.begin(), dataset.end(), 7);
// 检查是否找到元素
if (index != dataset.end()) {
// 如果找到,通过减去起始迭代器打印位置(索引)
cout << "The element is found at the "
<< index - dataset.begin() << "nd index";
}
else {
// 如果未找到
cout << "Element not found";
}
return 0;
}
|
输出:
1
|
The element is found at the 3rd index
|
binary_search()函数
binary_search()函数也用于在范围内查找元素,但与find()函数相比,它实现了二分搜索而不是线性搜索。它比find()函数更快,但只能用于具有随机访问的排序数据集。定义在头文件中。
语法:
1
|
binary_search(starting_pointer, ending_pointer, target);
|
参数:
- starting_pointer:指向范围开始的指针
- ending_pointer:指向范围结束之后元素的指针
- target:要在数据集中搜索的值
返回值:
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// C++程序演示binary_search()函数
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 初始化一个排序的整数向量
vector<int> arr = { 56, 57, 58, 59, 60, 61, 62 };
// binary_search()仅适用于排序容器
if (binary_search(arr.begin(), arr.end(), 62)) {
// 如果找到,打印元素存在
cout << 62 << " is present in the vector.";
}
else {
// 如果未找到,打印元素不存在
cout << 16 << " is not present in the vector";
}
cout << endl;
}
|
输出:
1
|
62 is present in the vector.
|
max()函数
std::max()函数用于比较两个数字并找出较大的那个,也定义在头文件中。
语法:
参数:
返回值:
- 此函数返回两个数字a和b中较大的数字
- 如果两个数字相等,则返回第一个数字
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// max()函数
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
// 声明两个整数变量
int a = 8;
int b = 10;
// 使用max()函数找出a和b中较大的数字
int maximum = max(a, b);
// 用有意义的消息显示结果
cout << "The maximum of " << a << " and " << b << " is: " << maximum << endl;
return 0;
}
|
输出:
1
|
The maximum of 8 and 10 is: 10
|
min()函数
std::min()函数用于比较两个数字并找出较小的那个,也定义在头文件中。
语法:
参数:
返回值:
- 此函数返回两个数字a和b中较小的数字
- 如果两个数字相等,则返回第一个数字
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// min()函数的使用
#include <algorithm> // 用于内置min()函数
#include <iostream>
using namespace std;
int main()
{
// 声明两个整数变量存储用户输入
int a = 4;
int b = 8;
// 使用min()函数找出较小的数字
int smallest = min(a, b);
// 显示结果
cout << "The smaller number between " << a << " and " << b << " is: " << smallest << endl;
return 0;
}
|
输出:
1
|
The smaller number between 4 and 8 is: 4
|
swap()函数
std::swap()函数允许您交换两个值,定义在头文件中。
语法:
参数:
返回值:此函数不返回任何值。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// swap()函数的使用
#include <algorithm> // 用于内置swap()函数
#include <iostream>
using namespace std;
int main()
{
int firstNumber = 8;
int secondNumber = 9;
// 使用内置swap()函数交换值
swap(firstNumber, secondNumber);
// 显示交换后的值
cout << "After the swap:" << endl;
cout << firstNumber << " " << secondNumber << endl;
return 0;
}
|
输出:
tolower()函数
tolower()函数用于将给定的字母字符转换为小写,定义在头文件中。
语法:
参数:
返回值:
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// C++程序
// tolower()函数的使用
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
// 声明并初始化一个包含大写字符的字符串
string str = "FRECODECAMP";
for (auto& a : str) {
a = tolower(a);
}
// 打印修改后的字符串
cout << str;
return 0;
}
|
输出:
toupper()函数
toupper()函数用于将给定的字母字符转换为大写,定义在头文件中。
语法:
参数:
返回值:
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// toupper()函数的使用
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
// 声明并初始化一个字符串
string str = "freecodecamp";
for (auto& a : str) {
a = toupper(a);
}
// 输出转换后的大写字符串
cout << str;
return 0;
}
|
输出:
结论
内置函数在竞争性编程和常见编程任务中是有用的工具。这些函数有助于提高代码可读性并增强代码效率。在上面的文章中,我们讨论了一些非常有用的常见内置函数。一些常见的内置函数包括max()、min()、sort()和sqrt()等。通过使用这些内置库,我们可以减少样板代码并加速软件开发过程。这些函数有助于编写更简洁、可靠和可维护的C++程序。