> 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/data-structures/array-1d.md).

# 一维数组

### 定义与使用

数组是一组**相同类型**变量的集合，用下标访问。

```cpp
int a[10];              // 10 个元素，下标 0~9
int b[5] = {1, 2, 3};   // 前 3 个赋值，其余为 0
int c[] = {1, 2, 3};    // 自动确定长度 3

for (int i = 0; i < 5; i++) cin >> b[i];   // 输入
for (int i = 0; i < 5; i++) cout << b[i];  // 输出
```

### 重要性质

* 下标从 **0** 开始，`a[n]` 会越界，属于未定义行为。
* 数组长度一旦定义不可改变（变长数组不在考纲内）。
* 全局数组默认全 0；局部数组不会自动清零，建议手动初始化。

### 常用操作

```cpp
int a[100], n = 5;
int sum = 0, mx = a[0];
for (int i = 0; i < n; i++) { sum += a[i]; if (a[i] > mx) mx = a[i]; }

// 逆序
for (int i = 0, j = n - 1; i < j; i++, j--) swap(a[i], a[j]);

// 查找最大值下标
int pos = 0;
for (int i = 1; i < n; i++) if (a[i] > a[pos]) pos = i;
```

### 数组开多大

一般按题目数据范围 +5 开，例如 `n ≤ 100` 就开 `a[105]`。注意不要开得过小导致越界。


---

# 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/data-structures/array-1d.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.
