Пространства имён
Варианты
Действия

cpp/utility/integer sequence — различия между версиями

Материал из cppreference.com
< cpp‎ | utility
(Helper templates)
м
 
(не показаны 6 промежуточных версий 1 участника)
Строка 11: Строка 11:
 
{{par begin}}
 
{{par begin}}
 
{{par | T | целочисленный тип, используемый для элементов последовательности}}
 
{{par | T | целочисленный тип, используемый для элементов последовательности}}
{{par | ...Ints | пакет параметров не тип, представляющий последовательность}}
+
{{par | ...Ints | пакет параметров не , представляющий последовательность}}
 
{{par end}}
 
{{par end}}
  
Строка 33: Строка 33:
  
 
===Параметры===
 
===Параметры===
(none)
+
()
  
 
===Возвращаемое значение===
 
===Возвращаемое значение===
Строка 42: Строка 42:
 
===Вспомогательные шаблоны===
 
===Вспомогательные шаблоны===
  
Псевдоним вспомогательного шаблона {{ttb|std::index_sequence}} определён для общего случая, когда {{tt|T}} равен {{lc|std::size_t}}:
+
шаблона {{ttb|std::index_sequence}} определён для общего случая, когда {{tt|T}} равен {{lc|std::size_t}}:
  
 
{{ddcl | 1=
 
{{ddcl | 1=
Строка 49: Строка 49:
 
}}
 
}}
  
Псевдонимы вспомогательных шаблонов {{ttb|std::make_integer_sequence}} и {{ttb|std::make_index_sequence}} определены для упрощения создания типов {{tt|std::integer_sequence}} и {{tt|std::index_sequence}} соответственно, с {{tt|Ints}} равным 0, 1, 2, ..., {{tt|N-1}}:
+
шаблонов {{ttb|std::make_integer_sequence}} и {{ttb|std::make_index_sequence}} определены для упрощения создания типов {{tt|std::integer_sequence}} и {{tt|std::index_sequence}} соответственно, с {{tt|Ints}} равным 0, 1, 2, ..., {{tt|N-1}}:
  
 
{{dcl begin}}
 
{{dcl begin}}
Строка 64: Строка 64:
 
Программа некорректна, если {{tt|N}} отрицательное. Если {{tt|N}} равно нулю, указанный тип это {{tt|integer_sequence<T>}}.
 
Программа некорректна, если {{tt|N}} отрицательное. Если {{tt|N}} равно нулю, указанный тип это {{tt|integer_sequence<T>}}.
  
Псевдоним вспомогательного шаблона {{ttb|std::index_sequence_for}} определён для преобразования любого пакета параметров типа в индексную последовательность той же длины:
+
шаблона {{ttb|std::index_sequence_for}} определён для преобразования любого пакета параметров типа в индексную последовательность той же длины:
  
 
{{dcl begin}}
 
{{dcl begin}}
Строка 73: Строка 73:
 
{{dcl end}}
 
{{dcl end}}
  
===Notes===
+
======
{{feature test macro|__cpp_lib_integer_sequence}}
+
{{feature test macro|__cpp_lib_integer_sequence}}
  
===Example===
+
======
{{example|Note: see ''Possible Implementation'' in {{rlpt|apply#Possible implementation|std::apply}} for another example.
+
{{example|: '''' {{rlpt|apply#|std::apply}}.
 
|code=
 
|code=
 
#include <tuple>
 
#include <tuple>
Строка 84: Строка 84:
 
#include <utility>
 
#include <utility>
  
// debugging aid
+
//
 
template<typename T, T... ints>
 
template<typename T, T... ints>
 
void print_sequence(std::integer_sequence<T, ints...> int_seq)
 
void print_sequence(std::integer_sequence<T, ints...> int_seq)
 
{
 
{
     std::cout << "The sequence of size " << int_seq.size() << ": ";
+
     std::cout << "" << int_seq.size() << ": ";
 
     ((std::cout << ints << ' '), ...);
 
     ((std::cout << ints << ' '), ...);
 
     std::cout << '\n';
 
     std::cout << '\n';
 
}
 
}
  
// convert array into a tuple
+
//
 
template<typename Array, std::size_t... I>
 
template<typename Array, std::size_t... I>
 
auto a2t_impl(const Array& a, std::index_sequence<I...>)
 
auto a2t_impl(const Array& a, std::index_sequence<I...>)
Строка 106: Строка 106:
 
}
 
}
  
// pretty-print a tuple
+
//
 
template<class Ch, class Tr, class Tuple, std::size_t... Is>
 
template<class Ch, class Tr, class Tuple, std::size_t... Is>
 
void print_tuple_impl(std::basic_ostream<Ch,Tr>& os,
 
void print_tuple_impl(std::basic_ostream<Ch,Tr>& os,
Строка 133: Строка 133:
 
     std::array<int, 4> array = {1, 2, 3, 4};
 
     std::array<int, 4> array = {1, 2, 3, 4};
  
     // convert an array into a tuple
+
     //
 
     auto tuple = a2t(array);
 
     auto tuple = a2t(array);
 
     static_assert(std::is_same_v<decltype(tuple),
 
     static_assert(std::is_same_v<decltype(tuple),
 
                                 std::tuple<int, int, int, int>>, "");
 
                                 std::tuple<int, int, int, int>>, "");
  
     // print it to cout
+
     // cout
     std::cout << "The tuple: " << tuple << '\n';
+
     std::cout << ": " << tuple << '\n';
 
}
 
}
 
|output=
 
|output=
The sequence of size 7: 9 2 5 1 9 1 6  
+
7: 9 2 5 1 9 1 6  
The sequence of size 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  
+
20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  
The sequence of size 10: 0 1 2 3 4 5 6 7 8 9  
+
10: 0 1 2 3 4 5 6 7 8 9  
The sequence of size 3: 0 1 2  
+
3: 0 1 2  
The tuple: (1, 2, 3, 4)
+
: (1, 2, 3, 4)
 
}}
 
}}
  
===See also===
+
======
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc | cpp/container/array/dsc to_array}}
 
{{dsc inc | cpp/container/array/dsc to_array}}

Текущая версия на 21:05, 5 сентября 2023

 
 
Библиотека метапрограммирования
Свойства типов
Категории типов
(C++11)
(C++14)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Свойства типов
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(до C++20*)
(C++11)(устарело в C++20)
(C++11)
Константы свойств типа
Метафункции
(C++17)
Поддерживаемые операции
Запросы отношений и свойств
Модификации типов
(C++11)(C++11)(C++11)
Преобразования типов