[ create a new paste ] login | about

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

C++, pasted on Nov 30:
#include <locale>
#include <iostream>
#include <algorithm>
#include <functional>
#include <limits>

#include <time.h>

template <typename T, const size_t Dimension>
void swap(T (&arr)[Dimension][Dimension], size_t N)
{
	T maximum = std::numeric_limits<T>::min();
	size_t row = 0;
	for (size_t i=0; i<Dimension; i++)
		if (arr[i][i] > maximum)		
			row = i, maximum = arr[i][i];

	if (row == N)
		return;

	T tmp[Dimension];
	std::copy(arr[row], arr[row] + Dimension, tmp);
	std::copy(arr[N], arr[N] + Dimension, arr[row]);
	std::copy(tmp, tmp + Dimension, arr[N]);
}

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 = 3;
	int arr[N][N];

	srand(static_cast<unsigned>(time(NULL)));
	std::generate_n(&arr[0][0], N*N, &rand);
	
	// номер переставляемой строки
	const size_t row = 1;

	std::cout << "До: " << std::endl;
	std::cout << arr << std::endl;

	swap(arr, row);

	std::cout << "После: " << std::endl;
	std::cout << arr << std::endl;

	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
До: 
1794065559	1630929139	1387551155	
947247103	1457332913	1182208928	
370085022	279226564	1862698864	

После: 
1794065559	1630929139	1387551155	
370085022	279226564	1862698864	
947247103	1457332913	1182208928	



Create a new paste based on this one


Comments: