什么是容器?
容器是用来存放对象的,就是容器类,是用做的容器的内存的管理方法
内核:数据结构 + 算法
C语言的数组和结构体,是语言源生支持的容器
C++ 的容器通过类库提供,容器类库被模板技术泛华后,就是STL容器
STL容器有哪些?
序列容器:位置与元素值无关;包括array、vector、deque、list、forward_list等几个。
排序容器:按照元素值排序好;包括set、multiset、map、mutilmap等。
哈希容器:未排序,元素位置由哈希函数决定;包括unordered_set、unordered_map、hash_set、hash_multiset、hash_map、hash_multimap等。
array的初步使用
array特点:定长、同类型元素、内存连续排布
本质是数组
1、构造、初始化
聚合初始化:从初始化器列表初始化聚合体。此为列表初始化的一种形式
#include <array>
#include <string>
void test070803(void) {
std::array<int, 3> a0;
std::array<int, 3> a1{4, 5, 6};
std::array<int, 3> a2 = {1, 2, 3}; //聚合初始化
std::array<int, 3> a3 = a2; //拷贝初始化
}
2、元素访问
#include <array>
#include <string>
void test070803(void) {
std::array<int, 3> a1{4, 5, 6};
cout << "a1[0] = " << a1[0] << endl;
cout << "a1[1] = " << a1[1] << endl;
cout << "a1[2] = " << a1.operator[](2) << endl;
a1[2] = 99;
cout << "a1[2] = " << a1[2] << endl;
cout << "a1.at(0) = " << a1.at(0) << endl;
a1.at(0) = 888;
cout << "a1.at(0) = " << a1.at(0) << endl;
// front和back方法返回第1个和最后1个元素
cout << "a1.front() = " << a1.front() << endl;
cout << "a1.back() = " << a1.back() << endl;
// data返回真实存储内存中首元素首地址的值
cout << "a1.data() = " << a1.data() << endl;
cout << "*(a1.data()) = " << *(a1.data()) << endl;
cout << "*(a1.data()+1)= " << *(a1.data() + 1) << endl;
cout << "*(a1.data()+2)= " << *(a1.data() + 2) << endl;
int* p = a1.data();
cout << " p= " << p << endl;
cout << "*(a1.data()++)= " << *(p++) << endl;
cout << " p= " << p << endl;
cout << "*(a1.data()++)= " << *(p++) << endl;
// empty
cout << "a1.empty()= " << a1.empty() << endl;
// size
cout << "a1.size()= " << a1.size() << endl;
// max_size
cout << "a1.max_size() = " << a1.max_size() << endl;
操作
fill
swap
非成员函数
operator重载函数
get
swap
to_array
#include <algorithm> // for std::swap
#include <array> // for std::array
#include <stdexcept> // for std::out_of_range
// Helper function to print array contents
template <typename T, std::size_t N>
void printArray(const std::array<T, N>& arr) {
for (std::size_t i = 0; i < N; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int test070804() {
std::array<int, 5> arr1;
std::array<int, 5> arr2;
// Fill arr1 with value 10 and arr2 with value 20
arr1.fill(10);
arr2.fill(20);
// Print arrays before swapping
std::cout << "Array 1 before swap: ";
printArray(arr1);
std::cout << "Array 2 before swap: ";
printArray(arr2);
// Swap the contents of arr1 and arr2
swap(arr1, arr2); //数据交换 内存没有改变
// Print arrays after swapping
std::cout << "Array 1 after swap: ";
printArray(arr1);
std::cout << "Array 2 after swap: ";
printArray(arr2);
// Use operator[] to access and modify elements
arr1[2] = 50;
std::cout << "Array 1 after modifying index 2: ";
printArray(arr1);
// Compare arrays using operator==
if (arr1 == arr2) { //比较长度和数据内容
std::cout << "Array 1 is equal to Array 2" << std::endl;
} else {
std::cout << "Array 1 is not equal to Array 2" << std::endl;
}
// Compare arrays using operator<=
if (arr1 <= arr2) {
std::cout << "Array 1 is less than or equal to Array 2" << std::endl;
} else {
std::cout << "Array 1 is greater than Array 2" << std::endl;
}
std::array<int, 3> a{4, 5, 6};
// 获取值:
std::cout << '(' << get<0>(a) << ',' << get<1>(a) << ',' << get<2>(a)
<< ")\n";
// 设置值:
get<0, int, 3>(a) = 1;
get<1>(a) = 2;
get<2>(a) = 3;
// 获取值:
std::cout << '(' << get<0>(a) << ',' << get<1>(a) << ',' << get<2>(a)
<< ")\n";
return 0;
}
学习记录,侵权联系删除。
来源:朱老师物联网大课堂