STL模板类array使用详解
C++中的STL模板类array是一种固定大小的数组容器,它的大小在编译时就已经被确定了,并且不能进行动态扩展和收缩。在本篇文章中,我们将详细讲解关于C++中的STL模板类array的各项操作方法,并附上相应的代码范例、注释及输出结果。
- 元素访问操作
1.1 at函数
at函数用于访问指定位置的元素,并且在访问时进行越界检查,如果越界则会引发异常。
下面是使用at函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
try {
int x = arr.at(3);
int y = arr.at(6); // 越界访问
} catch (const exception& e) {
cout << "发生异常: " << e.what() << endl;
}
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用at函数访问了第三个元素和第七个元素(越界),在第二次访问时抛出了异常。
输出结果为:
发生异常: array::at: __n (which is 6) >= _Nm (which is 5)
1.2 operator[]函数
operator[]函数用于访问指定位置的元素,不进行越界检查。
下面是使用operator[]函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
int x = arr[3];
int y = arr[6]; // 不进行越界检查
cout << "x = " << x << endl;
cout << "y = " << y << endl; // 输出不确定的结果
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并分别使用operator[]函数访问了第三个元素和第七个元素(越界),在访问越界时输出了不确定的结果。
输出结果为:
x = 4
y = -572662307
1.3 front函数
front函数用于访问数组的第一个元素。
下面是使用front函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
int x = arr.front();
cout << "数组的第一个元素为: " << x << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用front函数访问了数组的第一个元素。
输出结果为:
数组的第一个元素为: 1
1.4 back函数
back函数用于访问数组的最后一个元素。
下面是使用back函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
int x = arr.back();
cout << "数组的最后一个元素为: " << x << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用back函数访问了数组的最后一个元素。
输出结果为:
数组的最后一个元素为: 5
1.5 data函数
data函数用于直接访问数组底层的指针,以便进行数组指针的操作。
下面是使用data函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
int* p = arr.data();
for (int i = 0; i < arr.size(); i++) {
cout << p[i] << " ";
}
cout << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用data函数直接访问了数组底层的指针,以遍历数组中的所有元素并输出。
输出结果为:
1 2 3 4 5
- 迭代器操作
2.1 begin函数和cbegin函数
begin函数和cbegin函数用于返回一个指向数组第一个元素的迭代器。
下面是使用begin函数和cbegin函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
auto it1 = arr.begin(); // auto关键字自动推导出迭代器类型为int*
auto it2 = arr.cbegin(); // auto关键字自动推导出迭代器类型为const int*
cout << "*it1 = " << *it1 << endl;
cout << "*it2 = " << *it2 << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用begin函数和cbegin函数返回了指向数组第一个元素的迭代器,接着输出了迭代器指向的元素。
输出结果为:
*it1 = 1
*it2 = 1
2.2 end函数和cend函数
end函数和cend函数用于返回一个指向数组尾元素的下一个元素的迭代器。
下面是使用end函数和cend函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
auto it1 = arr.end(); // auto关键字自动推导出迭代器类型为int*
auto it2 = arr.cend(); // auto关键字自动推导出迭代器类型为const int*
cout << "*--it1 = " << *--it1 << endl;
cout << "*--it2 = " << *--it2 << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用end函数和cend函数返回了指向数组尾元素下一个元素的迭代器,接着将迭代器倒退一位并输出了所指向的元素。
输出结果为:
*--it1 = 5
*--it2 = 5
2.3 rbegin函数和crbegin函数
rbegin函数和crbegin函数用于返回一个指向数组最后一个元素的逆向迭代器。
下面是使用rbegin函数和crbegin函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
auto rit1 = arr.rbegin(); // auto关键字自动推导出逆向迭代器类型为reverse_iterator<int*>
auto rit2 = arr.crbegin(); // auto关键字自动推导出逆向迭代器类型为const_reverse_iterator<int*>
cout << "*rit1 = " << *rit1 << endl;
cout << "*rit2 = " << *rit2 << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用rbegin函数和crbegin函数返回了指向数组最后一个元素的逆向迭代器,接着输出了迭代器指向的元素。
输出结果为:
*rit1 = 5
*rit2 = 5
2.4 rend函数和crend函数
rend函数和crend函数用于返回一个指向数组头元素的逆向迭代器。
下面是使用rend函数和crend函数访问数组元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
auto rit1 = arr.rend(); // auto关键字自动推导出迭代器类型为reverse_iterator<int*>
auto rit2 = arr.crend(); // auto关键字自动推导出迭代器类型为const_reverse_iterator<int*>
cout << "*--rit1 = " << *--rit1 << endl;
cout << "*--rit2 = " << *--rit2 << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用rend函数和crend函数返回了指向数组头元素的逆向迭代器,接着将迭代器倒退一位并输出了所指向的元素。
输出结果为:
*--rit1 = 1
*--rit2 = 1
- 容量操作
3.1 empty函数
empty函数用于判断数组是否为空。
下面是使用empty函数判断数组是否为空的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1 = {1, 2, 3, 4, 5};
array<int, 0> arr2 = {};
if (!arr1.empty()) {
cout << "数组arr1不为空" << endl;
}
if (arr2.empty()) {
cout << "数组arr2为空" << endl;
}
return 0;
}
在该范例中,我们使用array定义了两个整型数组arr1和arr2,并使用empty函数检查了这两个数组是否为空。
输出结果为:
数组arr1不为空
数组arr2为空
3.2 size函数
size函数用于返回数组中元素的个数。
下面是使用size函数获取数组元素个数的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
int n = arr.size();
cout << "数组中元素个数为: " << n << endl;
return 0;
}
在该范例中,我们使用array定义了一个整型数组,并使用size函数获取了数组中元素的个数,并将其输出。
输出结果为:
数组中元素个数为: 5
3.3 max_size函数
max_size函数用于返回数组可容纳的最大元素数。
下面是使用max_size函数获取数组可容纳的最大元素数的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
, 5> arr;
cout << "数组最大容量为: " << arr.max_size() << endl;
return 0;
}
在该范例中,我们使用array定义了一个空的整型数组,并使用max_size函数获取了该数组可容纳的最大元素数,并将其输出。
输出结果为:
数组最大容量为: 5
- 操作操作
4.1 fill函数
fill函数用于将数组中所有的元素都赋值为指定的值。
下面是使用fill函数将数组中所有元素都赋值为指定值的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr;
arr.fill(5);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
在该范例中,我们使用array定义了一个空的整型数组,并使用fill函数将数组中所有的元素都赋值为5,并遍历数组中所有元素并打印出来。
输出结果为:
5 5 5 5 5
4.2 swap函数
swap函数用于交换两个数组的元素。
下面是使用swap函数交换两个数组的元素的范例:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1 = {1, 2, 3, 4, 5};
array<int, 5> arr2 = {6, 7, 8, 9, 10};
arr1.swap(arr2);
cout << "交换后的arr1: ";
for (int i = 0; i < arr1.size(); i++) {
cout << arr1[i] << " ";
}
cout << endl << "交换后的arr2: ";
for (int i = 0; i < arr2.size(); i++) {
cout << arr2[i] << " ";
}
cout << endl;
return 0;
}
在该范例中,我们使用array定义了两个整型数组arr1和arr2,并使用swap函数交换了这两个数组的元素,接着分别遍历两个数组并将元素打印出来。
输出结果为:
交换后的arr1: 6 7 8 9 10
交换后的arr2: 1 2 3 4 5
至此,我们就详细讲解了C++中的STL模板类array的各项操作,包括元素访问、迭代器、容量和操作等你有所帮助。