1. 程式人生 > >c++ array模板類使用

c++ array模板類使用

目錄

本章是對c++ array模板類的知識歸納,講述了c++中array模板類的使用,不涉及原理方面的內容。

c++中的陣列型別是繼承了c語言的特性,在使用陣列的時候要注意陣列越界操作問題。為了更安全的對陣列進行操作,c++提出了陣列模板類array。

1、array模板類的定義

(1)array模板類的宣告

template <class T,size_t N> class array;

陣列類是固定大小的序列容器,它們包含以嚴格線性序列排序的特定數量的元素。陣列類具有固定大小,並且不通過分配器管理其元素的分配,它們是封裝固定大小元素陣列的聚合型別。

(2)容器屬性

  • 序列容器中的元素按嚴格的線性順序排序。各個元素按其順序訪問它們的位置。
  • 元素儲存在連續的儲存器位置,允許對元素進行恆定時間隨機訪問。可以偏移元素的指標以訪問其他元素。
  • 容器使用隱式建構函式和解構函式靜態分配所需的空間。它的大小是編譯時常量。沒有記憶體或時間開銷。

(3)array模板類的說明

array模板類中T為包含元素的型別(std::array::value_type),N為元素個數。

(4)array模板類標頭檔案

使用array模板類之前需要包含#include <array>標頭檔案!

2、array模板類的使用

(1)Iterators

Iterators迭代器的作用是遍歷array陣列類中的元素。可以通過begin/end()rbegin/rend()cbegin/cend()crbegin/crend()等函式進行訪問。

begin Return iterator to beginning
end Return iterator to end
rbegin Return reverse iterator to reverse beginning
rend Return reverse iterator to reverse end
cbegin Return const_iterator to beginning
cend Return const_iterator to end
crbegin Return const_reverse_iterator to reverse beginning
crend Return const_reverse_iterator to reverse end

參考程式碼如下所示:

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>

int main(void) {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    std::cout << "array values: ";
    for (auto it = arr.begin(); it != arr.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

執行結果如下所示:

array values: 1 2 3 4 5

(2)Capacity

array陣列容器的大小是固定的。可以通過sizeof()size()max_size()empty()等函式進行檢測。

size Return size
max_size Return maximum size
empty Test whether list is empty

測試array陣列容器大小的參考程式碼如下所示:

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>

int main(void) {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    std::cout << "sizeof(array) = " << sizeof(arr) << std::endl;
    std::cout << "size of array = " << arr.size() << std::endl;
    std::cout << "max_size of array = " << arr.max_size() << std::endl;

    if (arr.empty()) {
        std::cout << "array is empty!" << std::endl;
    } else {
        std::cout << "array is not empty!" << std::endl;
    }

    return 0;
}

執行結果如下所示:

sizeof(array) = 20
size of array = 5
max_size of array = 5
array is not empty!

(3)Element access

可以通過下標[ ]at()front()back()data()等函式訪問array容器內的元素。

operator[ ] Access element
at Access element 
front Access first element
back Access last element
data Get pointer to first data

參考程式碼如下:

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>

int main(void) {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    std::cout << "array[0] = " << arr[0] << std::endl;
    std::cout << "array.at(4) = " << arr.at(4) << std::endl;
    std::cout << "array.front() = " << arr.front() << std::endl;
    std::cout << "array.back() = " << arr.back() << std::endl;
    std::cout << "&array: " << arr.data() << " = " << &arr << std::endl;

    return 0;
}

執行結果如下所示:

array[0] = 1
array.at(4) = 5
array.front() = 1
array.back() = 5
&array: 0x7ffd22df6e50 = 0x7ffd22df6e50

(4)Modifiers

可以使用fill()swap()等函式對array容器整體進行操作。

fill Fill array with value
swap Swap content

參考程式碼如下所示:

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>

int main(void) {
    std::array<int, 5> arr;

    arr.fill(5);  // fill

    std::cout << "array values: ";
    for (auto i : arr) {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    std::array<int, 3> first = {1, 2, 3};
    std::array<int, 3> second = {6, 5, 4};

    std::cout << "first  array values: ";
    for (auto it = first.begin(); it != first.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "second array values: ";
    for (auto it = second.begin(); it != second.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    first.swap(second);  // swap

    std::cout << "swap array success!" << std::endl;

    std::cout << "first  array values: ";
    for (auto it = first.begin(); it != first.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "second array values: ";
    for (auto it = second.begin(); it != second.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

執行結果如下所示:

array values: 5 5 5 5 5 
first  array values: 1 2 3 
second array values: 6 5 4 
swap array success!
first  array values: 6 5 4 
second array values: 1 2 3 

(5)Compare

還可以使用>  <  ==等符號對兩個array陣列容器進行比較。

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>

int main(void) {
    std::array<int,5> a = {10, 20, 30, 40, 50};
    std::array<int,5> b = {10, 20, 30, 40, 50};
    std::array<int,5> c = {50, 40, 30, 20, 10};

    if (a == b) {
        std::cout << "a == b" << std::endl;
    } else {
        std::cout << "a != b" << std::endl;
    }

    if (a == c) {
        std::cout << "a == c" << std::endl;
    } else {
        std::cout << "a != c" << std::endl;
    }

    if (a < c) {
        std::cout << "a < c" << std::endl;
    } else {
        std::cout << "a >= c" << std::endl;
    }

    return 0;
}

執行結果如下所示:

a == b
a != c
a < c

(6)Other

c++過載了get()函式來訪問陣列容器中的元素,為了和元組相似,還過載了tuple_sizetuple_element型別。

get( array) Get element (tuple interface)
tuple_element<array> Tuple element type for array
tuple_size<array> Tuple size traits for array

參考程式碼如下所示:

/*****************************************************
Copyright (C) 2018. All rights reserved.
File name     : array.cpp
Version       : v1.0
Author        : zhengqijun
Date          : 2018-08-10
Function List :
Description   : array container.
******************************************************/

#include <iostream>
#include <array>
#include <tuple>

int main(void) {
    std::array<int,3> myarray = {10, 20, 30};
    std::tuple<int, int, int> mytuple (10, 20, 30);

    std::tuple_element<0, decltype(myarray)>::type myelement;  // int myelement

    myelement = std::get<2>(myarray);
    std::get<2>(myarray) = std::get<0>(myarray);
    std::get<0>(myarray) = myelement;

    std::cout << "first element in myarray: " << std::get<0>(myarray) << std::endl;
    std::cout << "first element in mytuple: " << std::get<0>(mytuple) << std::endl;

    return 0;
}

執行結果如下所示:

first element in myarray: 30
first element in mytuple: 10

建議:多使用array陣列容器代替c型別陣列,使運算元組元素更加安全!