#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
#include <iterator>
int main()
{
// Index: 0 1 2 3 4 5 6 7
int sequence[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int * iter_beg = &sequence[0];
int * iter_end = &sequence[8];
std::reverse(iter_beg, iter_end);
std::copy(&sequence[0] ,
&sequence[8] ,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
// Output: 8 7 6 5 4 3 2 1
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 386-7.
Tuesday, December 19, 2017
C++98: std::reverse()
std::reverse() reverses elements in a range. Strings have their own reverse() function. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment