std::ranges::views::zip, std::ranges::zip_view - cppreference.com (original) (raw)
Defined in header | ||
---|---|---|
template< ranges::input_range... Views > requires (ranges::view<Views> && ...) && (sizeof...(Views) > 0) class zip_view : public ranges::view_interface<zip_view<Views...>> | (1) | (since C++23) |
namespace views { inline constexpr /*unspecified*/ zip = /*unspecified*/; } | (2) | (since C++23) |
Call signature | ||
template< ranges::viewable_range... Rs > requires /* see below */ constexpr ranges::view auto zip( Rs&&... rs ); | (since C++23) |
zip_view
is a range adaptor that takes one or more views, and produces a view whose_i_
th element is a tuple-like value consisting of the_i_
th elements of all views. The size of produced view is the minimum of sizes of all adapted views.
zip_view
always models input_range, and models forward_range, bidirectional_range, random_access_range, or sized_range if all adapted view types model the corresponding concept.
zip_view
models common_range if
- sizeof...(Views) is equal to 1, and the only adapted view type models common_range, or
- at least one adapted view type does not model bidirectional_range, and every adapted view type models common_range, or
- every adapted view type models both random_access_range and sized_range.
Contents
- 1 Customization point objects
- 2 Data members
- 3 Member functions
- 4 Deduction guides
- 5 Nested classes
- 6 Helper templates
- 7 Notes
- 8 Example
- 9 See also
Customization point objects
The name views::zip
denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.
[edit] Data members
Member | Description |
---|---|
std::tuple<Views...> views_ | all adapted view objects(exposition-only member object*) |
[edit] Member functions
(constructor) | constructs a zip_view (public member function) [edit] |
---|---|
begin | returns an iterator to the beginning (public member function) [edit] |
end | returns an iterator or a sentinel to the end (public member function) [edit] |
size | returns the number of elements, provided only if each underlying (adapted) range satisfies sized_range (public member function) [edit] |
Inherited from std::ranges::view_interface | |
empty | returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range (public member function of std::ranges::view_interface) [edit] |
cbegin(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit] |
cend(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit] |
operator bool | returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit] |
front | returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit] |
back | returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit] |
operator[] | returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit] |
[edit] Deduction guides
[edit] Nested classes
| | the iterator type(exposition-only member class template*) | | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | | the sentinel type used when zip_view is not a common_range(exposition-only member class template*) |
[edit] Helper templates
| template< class... Views > constexpr bool enable_borrowed_range<ranges::zip_view<Views...>> = (ranges::enable_borrowed_range<Views> && ...); | | (since C++23) | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ------------- |
This specialization of ranges::enable_borrowed_range makes zip_view satisfy borrowed_range when each underlying view satisfies it.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_zip | 202110L | (C++23) | ranges::zip_view,ranges::zip_transform_view,ranges::adjacent_view,ranges::adjacent_transform_view |
[edit] Example
#include #include #include #include #include #include #include void print(auto const rem, auto const& range) { for (std::cout << rem; auto const& elem : range) std::cout << elem << ' '; std::cout << '\n'; } int main() { auto x = std::vector{1, 2, 3, 4}; auto y = std::list<std::string>{"α", "β", "γ", "δ", "ε"}; auto z = std::array{'A', 'B', 'C', 'D', 'E', 'F'}; print("Source views:", ""); print("x: ", x); print("y: ", y); print("z: ", z); print("\nzip(x,y,z):", ""); for (std::tuple<int&, std::string&, char&> elem : std::views::zip(x, y, z)) { std::cout << std::get<0>(elem) << ' ' << std::get<1>(elem) << ' ' << std::get<2>(elem) << '\n'; std::get<char&>(elem) += ('a' - 'A'); // modifies the element of z } print("\nAfter modification, z: ", z); }
Output:
Source views: x: 1 2 3 4 y: α β γ δ ε z: A B C D E F zip(x,y,z): 1 α A 2 β B 3 γ C 4 δ D After modification, z: a b c d E F