在 C++ 中,std::array
是一个封装了固定大小数组的容器类。它与 C 风格数组相似,因为它存储了多个相同类型的数值。
C++ std::array 声明
std::array
定义在 <array>
头文件中,因此在使用 std::array
之前必须包含此头文件。
语法
#include <array>
// declaration of std::array
std::array<T, N> array_name;
其中,
- T - 要存储的元素类型
- N - 数组中的元素数量
下面是如何声明一个大小为 5,存储数字列表的 std::array
std::array<int, 5> numbers;
std::array 的初始化
我们可以用两种方式初始化 std::array
方法 1
// initializer list
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
方法 2
// uniform initialization
std::array<int, 5> marks {10, 20, 30, 40, 50};
在这里,我们初始化了两个名为 numbers 和 marks 的数组,它们的大小均为 5,存储的元素类型为 int
。
示例:C++ std::array
// C++ program to demonstrate the usage of std::arrays
#include <iostream>
#include <array>
using namespace std;
int main(){
// uniform initialization
array <int , 5> numbers {1, 2, 3, 4, 5};
cout << "The elements are: " << endl;
// use a ranged for loop print the elements
for(const int num: numbers){
cout << num << " ";
}
}
输出
The elements are: 1 2 3 4 5
在上面的示例中,我们使用统一初始化声明并初始化了一个名为 numbers 的 std::array
。
array <int , 5> numbers {1, 2, 3, 4, 5};
然后,我们使用 范围 for 循环 显示了数组的内容。
访问 std::array 的元素
我们可以使用 []
运算符和数组元素的索引来访问数组元素。
// array of 5 integers
std::array<int, 5> n = {1, 2, 3, 4, 5};
// access first array element
std::cout << n[0] // returns 1
// access second array element
std::cout << n[1] // returns 2
由于索引从 0 开始,n[0]
返回数组的第一个元素,n[1]
返回第二个元素,依此类推。
注意:使用 []
运算符访问元素不会检查越界错误。
越界错误发生在程序尝试访问超出允许范围的数据时。例如,我们有一个包含 5 个元素的数组,而我们尝试访问第 10 个元素,这就是越界错误。
访问数组元素的另一种方法是使用 at
方法,它会检查越界错误。
// array of 5 integers
std::array<int, 5> n = {1, 2, 3, 4, 5};
std::cout << n.at(0) // returns 1
std::cout << n.at(1) // returns 2
std::cout << n.at(10) // throws std::out_of_range exception
修改 std::array 的元素
要修改特定索引处的元素,我们可以再次使用 []
和 at
。
std::array<int, 5> marks = {50, 67, 88, 98, 34};
// modify the 3rd element using []
marks[2] = 76;
// modify the first element using at
n.at(0)= 1;
示例:修改和访问数组元素
#include <iostream>
#include <array>
using namespace std;
int main(){
array <int, 5> numbers = {1, 2, 3, 4, 5};
// accessing using the [] operator
cout << "First element: " << numbers[0] << '\n';
// accessing using the at method
cout << "Second element: " << numbers.at(1) << '\n';
// modify the element at index 0
numbers[0] = 8;
cout << "Modifying first element: " << numbers[0] << '\n';
// modify the element at index 1
numbers.at(1) = 90;
cout << "Modifying second element: " << numbers[1] << '\n';
}
输出
First element: 1 Second element: 2 Modifying first element: 8 Modifying second element: 90
上面的示例演示了使用 []
运算符和 at
方法来访问和修改 std::array
的元素。
检查数组是否为空
要检查数组是否为空,我们可以使用 empty()
方法,如下所示:
// let n be a std::array
n.empty()
当数组为空时,empty
方法返回 true,否则返回 false。
获取数组中的元素数量
我们可以使用 size
方法来获取数组中的元素数量。
// let n be a std::array
n.size() // returns the size of array
示例:将 empty 和 size 方法与 std::array 一起使用
#include <iostream>
#include <array>
using namespace std;
int main(){
array <int, 5> numbers = {1, 2, 3, 4, 5};
cout << "The size of array is: " << numbers.size() << '\n';
if(numbers.empty()){
cout << "The array is empty";
}
else{
cout << "The array is not empty";
}
}
输出
The size of array is: 5 The array is not empty
在上面的示例中,我们使用 empty
方法检查数组是否为空,并使用 size
方法获取数组中的元素数量。
用相同的值填充 std::array
我们可以使用 fill 方法将整个数组 fill
同一个值。
让我们看一个例子。
#include <iostream>
#include <array>
using namespace std;
int main(){
array<int, 5> a;
// fill the array with zeros
a.fill(0);
cout << "The elements are: ";
for(const int& elt: a){
cout << elt << " ";
}
}
输出
The elements are: 0 0 0 0 0
在这里,我们使用 a.fill(0)
将整个数组填充为 0。
示例:将 std::array 与 STL 算法一起使用
在 C++ 中,我们可以使用 标准模板库 来实现一些常用的算法。这些算法对 std::array
支持得很好。例如,
#include <iostream>
#include <algorithm>
#include <numeric>
#include <array>
using namespace std;
int main(){
array<int, 5> age = {45, 23, 66, 87, 21};
// sorting
sort(age.begin(), age.end());
// print the sorted array
cout << "The sorted array is: ";
for(const int elt: age){
cout << elt << " ";
}
cout << endl;
// searching
// checking whether the number 5 exists or not
auto found = find(age.begin(), age.end(), 5);
if (found != age.end()) cout << "5 was Found" << endl;
else cout << "5 was Not Found" << endl;
// summing
int sum = accumulate(age.begin(), age.end(), 0);
cout << "The sum of the element of array is : " << sum;
}
输出
The sorted array is: 21 23 45 66 87 5 was Not Found The sum of the element of array is : 242
在这里,我们对数组执行了 3 个不同的操作:排序、查找和求和。
要对数组进行排序,我们使用了 sort()
函数。
sort(age.begin(), age.end());
这将从第一个元素到最后一个元素对数组进行排序。
要查找数组中的元素,我们使用了 find()
函数。
// check if the number 5 is present or not
auto found = find(age.begin(), age.end(), 5);
这里,find 函数搜索数组 age 中是否存在数字 5。结果存储在变量 found 中。如果 found 等于 age.end()
,则表示该数字不存在;否则,表示存在。
要查找数组元素的总和,我们使用 std::accumulate
函数。
int sum = std::accumulate(age.begin(), age.end(), 0);
这将返回整个数组中数字的总和。
std::array
和 C 风格数组之间有什么区别?std::array
和 C 风格数组之间的区别在于:
- C 风格数组
T[n]
可以自动转换为T*
,而std::array
也可以。 - C 风格数组不支持赋值,而
std::array<T, N>
支持。
int a[5] = {1, 2, 3, 4, 5};
// Error: invalid array assignment
int b[5] = a[5];
std::array<int, 5> a = {1, 2, 3, 4, 5};
// Correct: std::array supports assignment
std::array<int, 5> b = a;
此外,std::array
提供了 STL 容器相对于 C 风格数组的所有优势。因此,在 C++ 中,建议使用 std::array
而不是 C 风格数组。