Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: slhmy/full-stack-docusaurus
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3f1a0ed276178b0732eef121fc35037ccb28ef1f
Choose a base ref
..
head repository: slhmy/full-stack-docusaurus
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e6094c4d5950d4ad9e72dce22ade752c8b5cd185
Choose a head ref
Showing with 6 additions and 3 deletions.
  1. +6 −3 i18n/zh-CN/docusaurus-plugin-content-docs/current/algorithms/cpp-basics/vector-and-array.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# vector 和 array

:::caution
:::warning

在 C++ 中,你应该尽量使用 `std::vector``std::array`,而不是 C 风格的数组。原因是 C 风格的数组没有提供任何安全保障,没有越界检查,尽管在算法题中我们经常会用下标访问数组元素(`std::vector``std::array` 中正规的做法是使用 `at(idx)` 方法)。
在 C++ 中,你应该尽量使用 `std::vector``std::array`,而不是 C 风格的数组。
原因是 C 风格的数组没有提供任何安全保障,没有越界检查,尽管在算法题中我们经常会用下标访问数组元素
`std::vector``std::array` 中正规的做法是使用 `at(idx)` 方法)。

:::

## vector 和 array 的区别

`std::vector` 存储在堆上,`std::array` 存储在栈上。
因此,当你需要一个动态大小的数组时,你应该使用 `std::vector`,而当你只需要一个固定大小的数组时,使用 `std::array` 会更好,它拥有更好的性能。
因此,当你需要一个动态大小的数组时,你应该使用 `std::vector`
而当你只需要一个固定大小的数组时,使用 `std::array` 会更好,它拥有更好的性能。

## vector 和 array 的初始化