内置函数c++
时间: 2025-05-11 12:27:26 浏览: 32
### C++ 内置函数概述
C++ 中的内置函数是指由编译器提供并可以直接使用的函数。它们通常被用来完成一些常见的任务,比如数学运算、字符串处理和类型转换等。虽然 C++ 标准并未明确定义“内置函数”,但我们可以通过广义的方式理解这些功能[^2]。
以下是部分常用的 C++ 内置函数及其用法:
---
### 1. **数学相关内置函数**
C++ 提供了许多用于数学计算的标准库函数,位于 `<cmath>` 头文件中。以下是一些常见函数的例子:
#### 绝对值
- `abs(int)` 和 `fabs(double)`
```cpp
#include <iostream>
#include <cmath>
int main() {
std::cout << abs(-5) << std::endl; // 输出: 5
std::cout << fabs(-3.14) << std::endl; // 输出: 3.14
return 0;
}
```
#### 幂运算
- `pow(base, exponent)`
```cpp
#include <iostream>
#include <cmath>
int main() {
std::cout << pow(2, 3) << std::endl; // 输出: 8
return 0;
}
```
#### 开方
- `sqrt(x)`
```cpp
#include <iostream>
#include <cmath>
int main() {
std::cout << sqrt(16) << std::endl; // 输出: 4
return 0;
}
```
#### 取整
- `ceil()` 向上取整;`floor()` 向下取整。
```cpp
#include <iostream>
#include <cmath>
int main() {
std::cout << ceil(3.14) << std::endl; // 输出: 4
std::cout << floor(3.14) << std::endl; // 输出: 3
return 0;
}
```
---
### 2. **字符串操作相关内置函数**
C++ 的字符串操作主要依赖于标准库 `<string>` 或者字符数组的操作方法。下面列举了一些常用的功能:
#### 字符串长度
- 使用 `std::strlen(const char*)` 对字符数组求长度。
```cpp
#include <iostream>
#include <cstring>
int main() {
const char* str = "hello";
std::cout << strlen(str) << std::endl; // 输出: 5
return 0;
}
```
#### 子串查找
- 使用 `std::strstr(char*, const char*)` 查找子串位置。
```cpp
#include <iostream>
#include <cstring>
int main() {
const char* haystack = "hello world";
const char* needle = "world";
if (strstr(haystack, needle)) {
std::cout << "Substring found!" << std::endl;
} else {
std::cout << "Substring not found!" << std::endl;
}
return 0;
}
```
---
### 3. **容器算法相关内置函数**
C++ STL 提供了一系列强大的算法来操作容器数据结构。例如,在 `<algorithm>` 中提供了许多实用工具。
#### 排序
- 使用 `std::sort(first_iterator, last_iterator)` 进行排序。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {5, 3, 8, 6};
std::sort(vec.begin(), vec.end());
for(auto num : vec){
std::cout << num << ' '; // 输出: 3 5 6 8
}
return 0;
}
```
#### 查找
- 使用 `std::find(first_iterator, last_iterator, value)` 来定位某个值的位置。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = std::find(vec.begin(), vec.end(), 3);
if(it != vec.end()){
std::cout << "Found at position: " << std::distance(vec.begin(), it) << std::endl; // 输出: Found at position: 2
}
return 0;
}
```
#### 计数
- 使用 `std::count(first_iterator, last_iterator, value)` 统计某值出现次数。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 2, 4, 2};
int count_val = std::count(vec.begin(), vec.end(), 2);
std::cout << "Count of 2 is: " << count_val << std::endl; // 输出: Count of 2 is: 3
return 0;
}
```
---
### 4. **其他常用内置函数**
#### 类型转换
- 动态类型转换:`dynamic_cast<type>(expression)`。
- 静态类型转换:`static_cast<type>(expression)`。
```cpp
double d = 3.14;
int i = static_cast<int>(d); // 转换为整数
```
#### 输入输出流控制
- 设置精度:`std::setprecision(n)`。
```cpp
#include <iomanip>
std::cout << std::fixed << std::setprecision(2) << 3.14159 << std::endl; // 输出: 3.14
```
---
### 总结
上述列出了 C++ 常见的一些内置函数类别及其实现方式。值得注意的是,某些所谓的“内置函数”实际上是通过编译器优化实现的内联函数或者宏定义形式存在。
阅读全文
相关推荐















