1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <cstddef>
#include <iostream>
#include <algorithm>
#include <string>
template <typename iter>
void print(iter begin, iter end, const std::string& separator = " ", const std::string& terminator = "\n", std::ostream& os = std::cout)
{
while (begin != end)
{
os << *begin++ ;
if (begin != end)
os << separator;
}
os << terminator;
}
void myreverse(int* beg, int* end)
{
using std::swap;
if (beg && end && beg < --end)
while (beg < end)
swap(*beg++, *end--);
}
int main()
{
const std::size_t size = 5;
int arr[size] = { 1, 2, 3, 4, 5 };
int* arr_beg = arr;
int* arr_end = arr + size;
print(arr_beg, arr_end, ", ");
std::reverse(arr_beg, arr_end);
print(arr_beg, arr_end, ", ");
myreverse(arr_beg, arr_end);
print(arr_beg, arr_end, ", ");
}
|