PrevUpHomeNext

std::views::zip to transpose matrix


std::views::zip - Posted on Jan 20, 2024 - See std::views::zip - Logs Home - d0004

std::views::zip

std::ranges::zip_view, std::views::zip

class std::ranges::zip_view;

std::ranges::zip_view is a range adaptor that takes one or more views, and produces a view whose ith element is a tuple-like value consisting of the ith elements of all views. The size of produced view is the minimum of sizes of all adapted views.

std::views::zip is a customization point object.

c++ example

std::views::zip takes element from every ith element of every views

auto tuples_in_one = std::view::zip(v1, v2, v3, v4, ...);

tuples_in_one is a view, and every element of tuples_in_one is a tuple, every element of the tuple is taken from ith element of every v1, v2, v3, ... .

Get every tuple

for (auto tuple: tuples_in_one)
	...

Every tuple element reference

for (using tuple = std::tuple<type1 &, type2 &, ...>; tuple tup: tuples_in_one)
{
	utx::print(std::get<0>(tup), std::get<1>(tup), ...);
}

Print

std::cout << e1 << ' ' << e2 << ...;

utx::print

utx::print(e1, e2, ...);

Full c++ example (transpose matrix)

#include <utxcpp/core.hpp>
#include <ranges>
#include <vector>

int main()
{
	std::vector<std::vector<utx::ix32>> matrix =
		{
			{1,2,3,4},
			{5,6,7,8},
			{9,0,1,2},
			{3,4,5,6}
		};
	
	utx::print("matrix:", matrix);
	
	std::stringstream output;
	utx::print.set_ostream(output);
	
	for (
		using tuple = std::tuple<utx::ix32 &, utx::ix32 &, utx::ix32 &, utx::ix32 &>;
		tuple tup:
			std::views::zip(matrix[0], matrix[1], matrix[2], matrix[3])
	)
	{
		utx::print(tup);
	}
	
	utx::print.restore_ostream();
	
	utx::print("transposed matrix:\n", output.view());
}

See Also

utx::print


PrevUpHomeNext

E

U