> 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/basics/input.md).

# 输入

### cin 输入

```cpp
int a, b;
cin >> a >> b;            // 用空格或换行分隔
string s;
cin >> s;                 // 读一个单词，遇空格/换行停止
```

> 特性：`cin >>` 会自动跳过空白字符，所以读不了带空格的整行。

### 读入整行

```cpp
string line;
getline(cin, line);       // 读入一整行（含空格），string 用

char buf[100];
cin.getline(buf, 100);    // 读入一整行，字符数组用
```

混用 `cin >>` 和 `getline` 时，`cin >>` 会残留换行符，需要先吃掉：

```cpp
int n;
cin >> n;
cin.ignore();             // 吃掉换行符
string s;
getline(cin, s);
```

### scanf 输入（C 风格）

```cpp
#include <cstdio>
int a; double x; char ch;
scanf("%d%lf %c", &a, &x, &ch);   // 注意取地址符 &
char s[100];
scanf("%s", s);                    // 字符串不用 &
```

### 字符级输入

```cpp
char c = getchar();       // 读一个字符（含空格、换行）
```

### 小结

| 需求           | 写法                  |
| ------------ | ------------------- |
| 读数字/单词       | `cin >> x`          |
| 读整行（string）  | `getline(cin, s)`   |
| 读整行（char\[]） | `cin.getline(s, n)` |
| 格式化输入        | `scanf`             |


---

# 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/basics/input.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.
