> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/functions/common-functions.md).

# 常用函数

> StCoder @qx1063<https://stcoder.cn/discussion-detail/1991>

#### 数学函数

| 函数          | 说明                              |
| ----------- | ------------------------------- |
| `ceil()`    | 向上取整函数——将小数变为与它相邻且比它大的整数        |
| `floor()`   | 向下取整函数——将小数变为与它相邻且比它小的整数        |
| `round()`   | 四舍五入取整函数——将小数的小数点后小于4的舍去，大于5的进位 |
| `abs()`     | 绝对值函数——将负数变为正数，正数保持不变           |
| `pow(a, b)` | 指数函数——a为底数，b为指数，计算a的b次方         |
| `sqrt(a)`   | 平方根函数——为平方的逆运算（开平方）             |
| `max(a, b)` | 取最大值                            |
| `min(a, b)` | 取最小值                            |
| `rand()`    | 生成随机数                           |

#### 字符串函数

| 函数               | 说明                   |
| ---------------- | -------------------- |
| `strlen(a)`      | 长度函数——求字符串的长度        |
| `strcpy(a, b)`   | 复制函数——把 b 复制到 a      |
| `strcat(a, b)`   | 连接函数——a 连接 b         |
| `strcmp(a, b)`   | 字符串比较函数——比较 a 和 b    |
| `strstr(a, b)`   | 子串函数——判断 b 是否为 a 的子串 |
| `a.substr(始, 终)` | 在字符串中取出固定的范围         |

#### strstr 函数详解

* 如果 b 是 a 的子串，则返回第一个出现的左边的字符
* 如果 b 不是 a 的子串，则返回 `nullptr`（空指针）

**判断子串示例：**

```cpp
char a[100] = "123456789";
char b[100] = "456";
if (strstr(a, b) == nullptr) {
    cout << "b不是a的子串";
} else {
    cout << "b是a的子串";
}
```

#### 输入输出函数

| 函数                  | 说明                       |
| ------------------- | ------------------------ |
| `cin.getline(a, n)` | 带空格的输入——接收一个字符串并输入（可含空格） |
| `setw(n)`           | 设定输出宽度                   |

#### 排序函数

| 函数                               | 说明     |
| -------------------------------- | ------ |
| `sort(a, a + n)`                 | 从小到大排序 |
| `sort(a, a + n, greater<int>())` | 从大到小排序 |

#### 其他常用

| 函数                    | 说明              |
| --------------------- | --------------- |
| `swap(a, b)`          | 交换 a 和 b 变量的值   |
| `map<下标类型, 内容类型> 数组名` | 映射容器，可用任意类型作为下标 |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/functions/common-functions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
