Copy elements in one vector into another vector in reverse order
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
int a1[ 10 ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 };
std::vector< int > v1( a1, a1 + 10 ); // copy of a
std::ostream_iterator< int > output( cout, " " );
cout << "Vector v1 contains: ";
std::copy( v1.begin(), v1.end(), output );
std::vector< int > results2;
std::reverse_copy( v1.begin(), v1.end(), std::back_inserter( results2 ) );
cout << "\nAfter reverse_copy, results2 contains: ";
std::copy( results2.begin(), results2.end(), output );
cout << endl;
return 0;
}
Vector v1 contains: 1 3 5 7 9 1 3 5 7 9
After reverse_copy, results2 contains: 9 7 5 3 1 9 7 5 3 1
Use reverse_copy to print all of them in reverse order
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
// print all of them in reverse order
reverse_copy (coll.begin(), coll.end(), // source
ostream_iterator<int>(cout," ")); // destination
cout << endl;
}
coll: 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
Using reverse_copy, a copying version of the generic reverse algorithm
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
int main() {
int a[100], b[100];
int i;
for (i = 0; i < 100; ++i)
a[i] = i;
reverse_copy(&a[0], &a[100], &b[0]);
for (i = 0; i < 100; ++i)
cout << " a: "<< a[i] << "b: " << b[i] << " \n";
return 0;
}
a: 0b: 99
a: 1b: 98
a: 2b: 97
a: 3b: 96
a: 4b: 95
a: 5b: 94
a: 6b: 93
a: 7b: 92
a: 8b: 91
a: 9b: 90
a: 10b: 89
a: 11b: 88
a: 12b: 87
a: 13b: 86
a: 14b: 85
a: 15b: 84
a: 16b: 83
a: 17b: 82
a: 18b: 81
a: 19b: 80
a: 20b: 79
a: 21b: 78
a: 22b: 77
a: 23b: 76
a: 24b: 75
a: 25b: 74
a: 26b: 73
a: 27b: 72
a: 28b: 71
a: 29b: 70
a: 30b: 69
a: 31b: 68
a: 32b: 67
a: 33b: 66
a: 34b: 65
a: 35b: 64
a: 36b: 63
a: 37b: 62
a: 38b: 61
a: 39b: 60
a: 40b: 59
a: 41b: 58
a: 42b: 57
a: 43b: 56
a: 44b: 55
a: 45b: 54
a: 46b: 53
a: 47b: 52
a: 48b: 51
a: 49b: 50
a: 50b: 49
a: 51b: 48
a: 52b: 47
a: 53b: 46
a: 54b: 45
a: 55b: 44
a: 56b: 43
a: 57b: 42
a: 58b: 41
a: 59b: 40
a: 60b: 39
a: 61b: 38
a: 62b: 37
a: 63b: 36
a: 64b: 35
a: 65b: 34
a: 66b: 33
a: 67b: 32
a: 68b: 31
a: 69b: 30
a: 70b: 29
a: 71b: 28
a: 72b: 27
a: 73b: 26
a: 74b: 25
a: 75b: 24
a: 76b: 23
a: 77b: 22
a: 78b: 21
a: 79b: 20
a: 80b: 19
a: 81b: 18
a: 82b: 17
a: 83b: 16
a: 84b: 15
a: 85b: 14
a: 86b: 13
a: 87b: 12
a: 88b: 11
a: 89b: 10
a: 90b: 9
a: 91b: 8
a: 92b: 7
a: 93b: 6
a: 94b: 5
a: 95b: 4
a: 96b: 3
a: 97b: 2
a: 98b: 1
a: 99b: 0