c++创建数组的方式有哪些

在C++中,创建数组的方式有以下几种:

静态数组:通过指定数组大小来定义数组,例如:

int arr[5]; // 声明一个包含5个整数的数组

动态数组:使用new关键字在堆上创建动态数组,例如:

int* arr = new int[5]; // 创建一个包含5个整数的动态数组

使用std::array容器:C++11引入了std::array容器,可以方便地创建数组并提供一些便利的方法,例如:

#include <array>
std::array<int, 5> arr; // 创建一个包含5个整数的std::array

使用std::vector容器:std::vector提供了动态数组的功能,并且可以动态调整大小,例如:

#include <vector>
std::vector<int> arr(5); // 创建一个包含5个整数的std::vector

使用指针:可以使用指针来模拟数组,例如:

int* arr = new int[5]; // 创建一个包含5个整数的动态数组
阅读剩余
THE END