C++ - 数据容器之 array(声明与初始化、元素访问、容量判断、元素遍历、其他操作)

一、声明与初始化

#include <iostream>
#include <array>

using namespace std;

int main() {
	
	// 声明,声明一个能存储 5 个整数的 array
	array<int, 5> arr1;

	// 初始化
	array<int, 5> arr2 = { 1, 2, 3, 4, 5 };
	array<int, 5> arr3{ 1, 2, 3, 4, 5 };

	return 0;
}

二、元素访问

  • 快速访问与安全访问
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	// 快速访问
	cout << arr[2] << endl;

	// 安全访问
	try {
		cout << arr.at(2) << endl;
		cout << arr.at(10) << endl;
	}
	catch (out_of_range& e) {
		cerr << e.what() << endl;
	}
	
	return 0;
}
# 输出结果

3
3
invalid array<T, N> subscript
  • 首尾元素访问
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	cout << arr.front() << endl;
	cout << arr.back() << endl;

	return 0;
}
# 输出结果

1
5
  • 底层数据指针访问
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	int* p = arr.data();
	cout << *p << endl;
	cout << *(p + 2) << endl;

	return 0;
}
# 输出结果

1
3

三、容量判断

#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr;
	array<int, 0> empty_arr;

	// 获取大小
	cout << "arr 获取大小:" << arr.size() << endl;
	cout << "empty_arr 获取大小:" << empty_arr.size() << endl;

	// 检查是否为空
	cout << "arr 检查是否为空:" << arr.empty() << endl;
	cout << "empty_arr 检查是否为空:" << empty_arr.empty() << endl;

	return 0;
}
# 输出结果

arr 获取大小:5
empty_arr 获取大小:0
arr 检查是否为空:0
empty_arr 检查是否为空:1

四、元素遍历

  1. 索引遍历
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	for (size_t i = 0; i < arr.size(); ++i) {
		cout << arr[i] << endl;
	}

	return 0;
}
  1. 迭代器遍历
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	for (auto it = arr.begin(); it != arr.end(); it++) {
		cout << *it << endl;
	}

	return 0;
}
  1. 范围的 for 循环遍历
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	for (int val : arr) {
		cout << val << endl;
	}

	return 0;
}

五、其他操作

  1. 填充数组
#include <iostream>
#include <array>

using namespace std;

int main() {

	array<int, 5> arr;
	arr.fill(10);

	for (size_t i = 0; i < arr.size(); ++i) {
		cout << arr[i] << endl;
	}

	return 0;
}
# 输出结果

10
10
10
10
10
  1. 交换内容
#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 (size_t i = 0; i < arr1.size(); ++i) {
		cout << arr1[i] << " ";
	}
	cout << endl;

	cout << "arr2: ";
	for (size_t i = 0; i < arr2.size(); ++i) {
		cout << arr2[i] << " ";
	}
	cout << endl;

	return 0;
}
# 输出结果

arr1: 6 7 8 9 10
arr2: 1 2 3 4 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值