容器库(1)-std::array

std::array是封装固定大小数组的容器,其大小在编译期就决定了,在运行时不可再更改。

本文章的代码库:

https://gitee.com/gamestorm577/CppStd

构造、赋值和析构

构造函数

可以使用初始化列表的方式来初始化一个array。代码示例:

struct MyStruct
{
    MyStruct(int i)
    {
        std::cout << "construct " << i << std::endl;
    }
};

std::array arr{1, 2, 3, 4, 5};

输出结果:

construct 1
construct 2
construct 3
construct 4
construct 5

析构函数

array销毁时,会从右到左销毁其包含的没个元素。代码示例:

struct MyStruct
{
    MyStruct(int i)
        : Index(i)
    {
    }

    ~MyStruct()
    {
        std::cout << "desctuct " << Index << std::endl;
    }

    int Index = 0;
};

std::array arr{1, 2, 3, 4, 5};

输出结果:

desctuct 5
desctuct 4
desctuct 3
desctuct 2
desctuct 1

赋值函数

拷贝另一个array的对应元素。代码示例:

struct MyStruct
{
    MyStruct(int i)
        : Index(i)
    {
        std::cout << "MyStruct i = " << i << std::endl;
    }

    MyStruct& operator=(const MyStruct& other)
    {
        std::cout << "operator, other = " << other.Index << std::endl;
        Index = other.Index;
        return *this;
    }

    int Index = 0;
};

std::array arr1{1, 2, 3};
std::array arr2{4, 5, 6};
arr2 = arr1;

输出结果:

MyStruct i = 1
MyStruct i = 2
MyStruct i = 3
MyStruct i = 4
MyStruct i = 5
MyStruct i = 6
operator, other = 1
operator, other = 2
operator, other = 3

元素访问

at

返回指定位置的元素的引用,如果超出边界,会抛出异常。代码示例:

std::array arr = {1, 2, 3, 4, 5};
int& num = arr.at(3);
num = 20;
std::cout << "arr at 3 is: " << arr.at(3) << std::endl;

try
{
    arr.at(15) = 20;
}
catch (std::out_of_range& e)
{
    std::cout << e.what() << std::endl;
}

输出结果:

arr at 3 is: 20
array::at

operator[]

返回指定位置的元素的引用,没有边界检查。代码示例:

std::array arr = {1, 2, 3, 4, 5};
int& num = arr[3];
num = 50;
std::cout << "arr at 3 is: " << arr[3] << std::endl;

输出结果:

arr at 3 is: 50

front

返回首个元素的引用,要求array不为空。代码示例:

std::array arr = {1, 2, 3, 4, 5};
int& num = arr.front();
num = 20;
std::cout << "arr front = " << arr.front() << std::endl;

输出结果为:

arr front = 20

back

返回最后一个元素的引用,要求array不为空。代码示例:

std::array arr = {1, 2, 3, 4, 5};
int& num = arr.back();
num = 50;
std::cout << "arr back = " << arr.back() << std::endl;

输出结果:

arr back = 50

data

返回保存的元素数组的地址。代码示例:

std::array arr = {1, 2, 3, 4, 5};
int* data = arr.data();
*(data + 3) = 20;
std::cout << "arr at 3 is: " << arr.at(3) << std::endl;

输出结果:

arr at 3 is: 20

迭代器

接口begin、cbegin指向array起始的迭代器,end、cend指向末尾的迭代器。rbegin、crbegin指向起始的逆向迭代器,rend、crend指向末尾的逆向迭代器。代码示例:

std::array arr = {1, 2, 3, 4, 5};
for (auto iter = arr.rbegin(); iter != arr.rend(); ++iter)
{
    *iter += 10;
}

for (auto iter = arr.crbegin(); iter != arr.crend(); ++iter)
{
    std::cout << "num = " << *iter << std::endl;
}

输出结果:

num = 15
num = 14
num = 13
num = 12
num = 11

容量

empty

检查array是否为空。代码示例:

std::array arr1;
std::array arr2;
std::cout << std::boolalpha;
std::cout << "arr1 empty: " << arr1.empty() << std::endl;
std::cout << "arr2 empty: " << arr2.empty() << std::endl;

输出结果:

arr1 empty: true
arr2 empty: false

size

返回元素的个数。代码示例:

std::array arr1;
std::array arr2;
std::cout << "arr1 size = " << arr1.size() << std::endl;
std::cout << "arr2 size = " << arr2.size() << std::endl;

输出结果:

arr1 size = 0
arr2 size = 5

max_size

返回可容纳的最大元素个数,和size接口返回的结果一样。代码示例:

std::array arr1;
std::array arr2;
std::cout << "max size of arr1 is: " << arr1.max_size() << std::endl;
std::cout << "max size of arr2 is: " << arr2.max_size() << std::endl;

输出结果:

max size of arr1 is: 0
max size of arr2 is: 5

其他

fill

以指定的值填充array。代码示例:

std::array arr = {1, 2, 3, 4, 5};
arr.fill(5);
for (auto num : arr)
{
    std::cout << "num = " << num << std::endl;
}

输出结果:

num = 5
num = 5
num = 5
num = 5
num = 5

swap

和另一个array交换内容。代码示例:

std::array arr1 = {1, 2, 3, 4, 5};
std::array arr2 = {11, 12, 13, 14, 15};
arr1.swap(arr2);
for (auto num : arr1)
{
    std::cout << "arr1 num = " << num << std::endl;
}

输出结果:

arr1 num = 11
arr1 num = 12
arr1 num = 13
arr1 num = 14
arr1 num = 15

std::swap

交换两个array的内容。代码示例:

std::array arr1 = {1, 2, 3, 4, 5};
std::array arr2 = {11, 12, 13, 14, 15};
std::swap(arr1, arr2);
for (auto num : arr2)
{
    std::cout << "arr2 num = " << num << std::endl;
}

输出结果:

arr2 num = 1
arr2 num = 2
arr2 num = 3
arr2 num = 4
arr2 num = 5

比较运算符

operator==,!=,<,<=,>,>=用于比较两个array。代码示例:

std::array arr1 = {1, 2, 3};
std::array arr2 = {11, 12, 13};

std::cout << std::boolalpha;
std::cout << "arr1 == arr2: " << (arr1 == arr2) << std::endl;
std::cout << "arr1 != arr2: " << (arr1 != arr2) << std::endl;
std::cout << "arr1 >  arr2: " << (arr1 > arr2) << std::endl;
std::cout << "arr1 >= arr2: " << (arr1 >= arr2) << std::endl;
std::cout << "arr1 <  arr2: " << (arr1 < arr2) << std::endl;
std::cout << "arr1 <= arr2: " << (arr1 <= arr2) << std::endl;

输出结果:

arr1 == arr2: false
arr1 != arr2: true
arr1 >  arr2: false
arr1 >= arr2: false
arr1 <  arr2: true
arr1 <= arr2: true

std::get

访问指定位置的元素。代码示例:

std::array arr = {1, 2, 3};
int& num = std::get<1>(arr);
num = 20;
for (auto i : arr)
{
    std::cout << "i = " << i << std::endl;
}

输出结果:

i = 1
i = 20
i = 3

std::to_array

用原生数组来创建array。代码示例:

int nums[5] = {1, 2, 3, 4, 5};
std::array arr = std::to_array(nums);
for (auto i : arr)
{
    std::cout << "i = " << i << std::endl;
}

输出结果:

i = 1
i = 2
i = 3
i = 4
i = 5

tuple_size

获取array的大小。代码示例:

auto size = std::tuple_size>::value;
std::cout << "size = " << size << std::endl;

输出结果:

size = 3

tuple_element

获取array的元素类型。代码示例:

struct MyStruct
{
};

using T = std::tuple_element<0, std::array>::type;

std::cout << std::boolalpha;
std::cout << std::is_same::value << std::endl;

输出结果:

true

你可能感兴趣的:(c++标准库基础,c++,开发语言)