Ескертпе
Бұл бетке кіру үшін қатынас шегін айқындау қажет. Жүйеге кіруді немесе каталогтарды өзгертуді байқап көруге болады.
Бұл бетке кіру үшін қатынас шегін айқындау қажет. Каталогтарды өзгертуді байқап көруге болады.
Заголовок <array> включает три функции, не являющиеся членами, getswapи to_array которые работают с объектами массива.
get
Возвращает ссылку на указанный элемент массива.
template <std::size_t Index, class Type, std::size_t Size>
constexpr Type& get(std::array<Type, Size>& arr) noexcept;
template <std::size_t Index, class Type, std::size_t Size>
constexpr const Type& get(const std::array<Type, Size>& arr) noexcept;
template <std::size_t Index, class Type, std::size_t Size>
constexpr Type&& get(std::array<Type, Size>&& arr) noexcept;
template <std::size_t Index, class Type, std::size_t Size>
constexpr const Type&& get(const std::array<Type, Size>&& arr) noexcept;
Параметры шаблона
Index
Смещение элемента.
Type
Тип элемента.
Size
Количество элементов в массиве.
Параметры
arr
Массив для выбора элемента.
Пример
#include <array>
#include <iostream>
using namespace std;
typedef array<int, 4> MyArray;
int main()
{
MyArray c0 { 0, 1, 2, 3 };
// display contents " 0 1 2 3"
for (const auto& e : c0)
{
cout << " " << e;
}
cout << endl;
// display odd elements " 1 3"
cout << " " << get<1>(c0);
cout << " " << get<3>(c0) << endl;
}
0 1 2 3
1 3
swap
Специализация шаблона, не являющегося членом, std::swap заменяет два объекта массива .
template <class Type, std::size_t Size>
void swap(std::array<Type, Size>& left, std::array<Type, Size>& right);
Параметры шаблона
Type
Тип элемента.
Size
Размер массива.
Параметры
left
Первый массив для обмена.
right
Второй массив для обмена.
Замечания
Функция шаблона выполняет метод left.swap(right).
Пример
// std__array__swap.cpp
// compile with: /EHsc
#include <array>
#include <iostream>
typedef std::array<int, 4> Myarray;
int main()
{
Myarray c0 = { 0, 1, 2, 3 };
// display contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
Myarray c1 = { 4, 5, 6, 7 };
c0.swap(c1);
// display swapped contents " 4 5 6 7"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
swap(c0, c1);
// display swapped contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
return (0);
}
0 1 2 3
4 5 6 7
0 1 2 3
to_array
Преобразует встроенный массив в std::array объект.
// C++20
template <class Type, std::size_t Size>
constexpr std::array<std::remove_cv_t<Type>, Size> to_array(Type (&arr)[Size]);
// C++20
template <class Type, std::size_t Size>
constexpr std::array<std::remove_cv_t<Type>, Size> to_array(Type (&&arr)[Size]);
Параметры шаблона
Type
Тип элемента.
Size
Размер входного массива.
Параметры
arr
Входной массив, используемый для преобразования.
Пример
// std_to_array.cpp
// Requires /std:c++20 or later
#include <array>
#include <iostream>
int main()
{
int arr1[]{ 1, 2, 3 };
std::array<int, 3> arr2 = std::to_array(arr1);
std::cout << "std::to_array(arr1):\n";
for (const auto& i : arr2)
{
std::cout << i << " ";
}
std::cout << std::endl;
// The size is 7 as it includes the null terminator
std::array<char, 7> arr3 = std::to_array("string");
std::cout << "\nstd::to_array(\"string\"):\n";
for (const auto& i : arr3)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
std::to_array(arr1):
1 2 3
std::to_array("string"):
s t r i n g