[ create a new paste ] login | about

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

C++, pasted on Jan 4:
#include <locale>
#include <algorithm>
#include <iostream>
#include <iterator>

template <typename T, size_t N>
std::ostream& operator<< (std::ostream& os, const T (&arr)[N])
{
	std::copy(arr, arr + N, std::ostream_iterator<T>(os, " "));
	return os;
}

void array_reverse(int* begin, int* end)
{
	for (; begin != end && begin != --end; ++begin)
	{
		int tmp = *begin;
		*begin = *end;
		*end = tmp;
	}
}

template <typename T, size_t N>
void test(T (&arr)[N])
{
	std::cout << (const char*) "Normal:\t\t" << arr << std::endl;
	array_reverse(arr, arr+N);
	std::cout << (const char*) "Reversed:\t" << arr << std::endl;

	std::cout << std::endl;
}

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

	int m1[] = {1};
	int m2[] = {1,2};
	int m3[] = {1,2,3};
	int m4[] = {1,2,3,4};

	test(m1);
	test(m2);
	test(m3);
	test(m4);

	return 0;

}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
Normal:		1 
Reversed:	1 

Normal:		1 2 
Reversed:	2 1 

Normal:		1 2 3 
Reversed:	3 2 1 

Normal:		1 2 3 4 
Reversed:	4 3 2 1 



Create a new paste based on this one


Comments: