[ create a new paste ] login | about

Link: http://codepad.org/qQHz3rMo    [ raw code | output | fork ]

C++, pasted on Dec 2:
#include <locale>
#include <iostream>
#include <algorithm>
#include <functional>
#include <limits>

#include <time.h>

template <
	typename _E, typename _Tr, template <typename, typename> class O,
	typename T, const size_t Rows>
struct outer : public std::unary_function<const T (&)[Rows], void>
{
	// ---------------------------------------------------------------------------------------------------------------------------
	typedef std::unary_function<const T (&)[Rows], void> base_type;
	typedef typename base_type::argument_type argument_type;
	typedef typename base_type::result_type result_type;
	// ---------------------------------------------------------------------------------------------------------------------------
	
	outer(O<_E,_Tr>& os) : os_(os) {}
	result_type operator()(argument_type v) const
	{
		const _E sep[] = { _E('\t'), 0 };
		std::ostream_iterator<T,_E,_Tr> oi(os_, sep); 
		
		std::copy(&v[0], &v[0]+Rows, oi);
		os_ << std::endl;
	}
	O<_E,_Tr>& os_;
};

template <
	typename _E, typename _Tr, template <typename, typename> class O,
	typename T, const size_t Cols, const size_t Rows>
O<_E,_Tr>& operator << (O<_E,_Tr>& os, T (&arr)[Cols][Rows])
{
	std::for_each(&arr[0], &arr[0]+Cols, outer<_E, _Tr, O, T, Rows>(os));
	return (os);
}

int main()
{
	setlocale(LC_ALL, "");

	const size_t N = 5;
	int arr[N][N];

	srand(static_cast<unsigned>(time(NULL)));
	std::generate_n(&arr[0][0], N*N, &rand);
	
	std::cout << arr << std::endl;

	return 0;
}


Output:
1
2
3
4
5
6
419889908	1087578616	464538228	414261032	2125777977	
1365203041	1841097411	684441252	118040330	1229995181	
456342475	767337740	1538219031	452222416	7562231	
1307178783	1127738097	1106261903	512476211	500287611	
1559540205	690171923	1556107381	385340674	1066880397	



Create a new paste based on this one


Comments: