当前位置: 首页 > news >正文

实用指南:C++STL---静态数组array

1. std::array 定义

std::array 是 C++11 标准模板库(STL)提供的一个固定大小数组容器,定义在 <array> 头文件中。
它把 C 风格的静态数组(如 int arr[5])封装成一个类模板,并提供 STL 容器的接口,使其能和 STL 算法无缝配合。

定义格式:

#include <array>std::array<T, N> arr;
  • T:元素类型
  • N:数组大小,必须是编译期常量(constexpr)

例子:

std::array<int, 5> a1;         // 5个int,值未初始化std::array<int, 5> a2 = {1,2,3}; // 前3个初始化,剩余为0std::array<int, 5> a3{};        // 所有元素值初始化(为0)

2. std::array优势

C 风格数组有几个明显的缺点:

  1. 容易退化(decay)成指针,丢失尺寸信息
    void func(int arr[]) {
    // 这里 arr 是 int*,sizeof(arr) 不是数组大小
    }
  2. 不支持迭代器,不能直接用 STL 算法
  3. 没有成员函数,需要依赖全局函数(std::begin, std::end 等)

std::array 保留了 C 风格数组固定大小、栈上分配的优点,同时解决了上述缺点,提供:

  • 随机访问迭代器
  • 成员函数接口size(), empty(), front(), back(), data() 等)
  • 支持 STL 算法
  • 值语义(可以整体赋值、按值传递)

3. 特性总结

特性说明
固定大小大小在编译期确定,不能动态增加或减少
连续内存底层是连续内存布局,支持指针运算
栈上存储元素存储在栈上(如果 std::array 本身在栈上)
随机访问支持下标 []at(),时间复杂度 O(1)
可复制支持直接赋值、拷贝构造
可比较支持 ==, !=, <, >, <=, >= 运算符(按字典序)
STL 兼容提供迭代器、begin()/end()rbegin()/rend()

4. 成员类型

std::array 支持的常见类型别名:

std::array<int, 5>::value_type;      // intstd::array<int, 5>::size_type;       // size_tstd::array<int, 5>::reference;       // int&std::array<int, 5>::const_reference; // const int&std::array<int, 5>::iterator;        // 随机访问迭代器std::array<int, 5>::reverse_iterator;// 反向迭代器

5. 成员函数与常用操作

5.1 构造与赋值

std::array<int, 3> a1{1, 2, 3};std::array<int, 3> a2 = a1;      // 拷贝构造std::array<int, 3> a3;a3 = a1;                         // 赋值

5.2 元素访问

std::array<int, 5> arr = {1,2,3,4,5};int x = arr[2];      // 下标访问,无越界检查int y = arr.at(2);   // 带越界检查,抛出 std::out_of_rangeint f = arr.front(); // 第一个元素int b = arr.back();  // 最后一个元素int* p = arr.data(); // 返回指向底层数组的指针

5.3 迭代器

for (auto it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << " ";
}
// C++11 range-based for
for (int x : arr) {
std::cout << x << " ";
}
// 反向迭代
for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
std::cout << *it << " ";
}

5.4 容量相关

std::array<int, 5> arr;std::cout << arr.size();    // 5std::cout << arr.max_size();// 5(固定大小)std::cout << arr.empty();   // false(大小为0才返回true)

5.5 修改操作

arr.fill(42); // 所有元素设为42
std::array<int, 5> arr2{1,2,3,4,5};arr.swap(arr2); // 交换两个array的内容(必须同类型同大小)

6. 与 STL 算法配合

std::array 是 STL 容器,可直接用 <algorithm> 中的算法:

#include <algorithm>#include <iostream>#include <array>int main() {std::array<int, 5> arr = {3,1,4,1,5};// 排序std::sort(arr.begin(), arr.end());for (int x : arr) std::cout << x << " "; // 1 1 3 4 5std::cout << "\n";// 查找auto it = std::find(arr.begin(), arr.end(), 3);if (it != arr.end()) {std::cout << "找到3在位置:" << (it - arr.begin()) << "\n";}}

7. 与其他数组类型对比

特性C 风格数组std::arraystd::vector
大小编译期固定编译期固定运行期可变
存储位置栈或静态区栈(若对象在栈上)
赋值不能直接整体赋值可以整体赋值可以整体赋值
退化会退化成指针不会退化不会退化
STL 兼容否(需借助 std::begin
内存开销零额外开销零额外开销有控制结构开销

8. 优缺点

优点

  • 零额外开销:和原生数组一样高效
  • 类型安全:不会隐式退化成指针
  • STL 兼容:支持迭代器和 STL 算法
  • 值语义:可直接赋值、按值传递
  • 支持结构化绑定(C++17):
    std::array<int, 3> arr{1,2,3};auto [x, y, z] = arr; // x=1, y=2, z=3

缺点

  • 大小固定:不能动态扩容
  • 大小必须是编译期常量
  • 存储在栈上:如果过大,可能导致栈溢出

数组选择建议

http://www.rkmt.cn/news/44564.html

相关文章:

  • MCP神器!一键部署连接任何MCP服务器
  • [ docker del imags containers ]
  • Flask的核心知识点如下
  • 学习流程
  • 2025年评价高的MC减速机厂家最新推荐排行榜
  • 2025年口碑好的压榨机设备行业内知名厂家排行榜
  • 2025年质量好的圆管犁厂家最新权威推荐排行榜
  • 2025年口碑好的消防转子泵实力厂家TOP推荐榜
  • 2025年评价高的圆盘耙TOP品牌厂家排行榜
  • python--手势识别 - 详解
  • 2025年比较好的木浆竹浆挤浆机厂家推荐及采购参考
  • 2025年评价高的动画制作2025优质品牌榜
  • 2025年热门的RAYCEE精密过滤器厂家最新推荐权威榜
  • Rachoon:基于 TypeScript 和 PostgreSQL 的自部署发票管理系统
  • 我的博客
  • 2025年质量好的南京工程发电机用户好评厂家排行
  • 应用安全 --- 如何知道可执行文件的虚拟地址对应的实际文件地址偏移
  • 领码方案|微服务与SOA的世纪对话(5):未来已来——AI 驱动下的智能架构哲学 - 详解
  • flask:访问redis
  • 2025年评价高的冰箱重型滑轨厂家推荐及采购参考
  • 2025年仓储货架厂家十大品牌综合评测:大连名商仓储货架荣登榜首
  • 2025年质量好的防尘四方袋厂家最新权威实力榜
  • 2025年评价高的木门液压合页行业内口碑厂家排行榜
  • 远程连接mysql8.0时报错:1130, Host *.*.*.* is not allowed to connect to this MySQL server
  • 2025年质量好的冷凝式衣物烘干机TOP实力厂家推荐榜
  • GitHub 快速入门指南,新手必备的高效使用手册!
  • 2025年靠谱的工地铺路钢板租赁行业内口碑厂家排行榜
  • MySQL索引(四):深入剖析索引失效的原因与优化方案
  • 实用指南:Node.js模块化开发实训案例
  • AI元人文:从“真理宫殿”到“可能性土壤”的哲学升华