Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
permutation.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for next_permutation, prev_permutation
30#include <functional> // for less
31#include <iostream> // for cout, endl
32#include <iterator> // for ostream_iterator
33#include <numeric> // for accumulate
34#include <vector> // for vector
35
36#include <examples.h>
37
38int main ()
39{
40 typedef std::vector<int, std::allocator<int> > IntVec;
41 typedef std::vector<char, std::allocator<char> > CharVec;
42
43 // Initialize a vector using an array of integers.
44 const IntVec::value_type a1[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 };
45 const CharVec::value_type a2[] = "abcdefghji";
46
47 // Create the initial set and copies for permuting.
48 IntVec m1 (a1 + 0, a1 + sizeof a1 / sizeof *a1);
49 IntVec prev_m1 (10);
50 IntVec next_m1 (10);
51
52 CharVec m2 (a2 + 0, a2 + sizeof a2 / sizeof *a2 - 1);
53 CharVec prev_m2 (10);
54 CharVec next_m2 (10);
55
56 std::copy (m1.begin (), m1.end (), prev_m1.begin ());
57 std::copy (m1.begin (), m1.end (), next_m1.begin ());
58 std::copy (m2.begin (), m2.end (), prev_m2.begin ());
59 std::copy (m2.begin (), m2.end (), next_m2.begin ());
60
61 // Create permutations.
62 typedef std::less<IntVec::value_type> IntLess;
63 typedef std::less<CharVec::value_type> CharLess;
64
65 std::prev_permutation (prev_m1.begin (), prev_m1.end (), IntLess ());
66 std::next_permutation (next_m1.begin (), next_m1.end (), IntLess ());
67 std::prev_permutation (prev_m2.begin (), prev_m2.end (), CharLess ());
68 std::next_permutation (next_m2.begin (), next_m2.end (), CharLess ());
69
70 // Output results.
71
72 typedef std::ostream_iterator<IntVec::value_type, char,
73 std::char_traits<char> >
74 IntOSIter;
75
76 typedef std::ostream_iterator<CharVec::value_type, char,
77 std::char_traits<char> >
78 CharOSIter;
79
80 std::cout << "Example 1: \n Original values: ";
81 std::copy (m1.begin (), m1.end (), IntOSIter (std::cout, " "));
82
83 std::cout << "\n Previous permutation: ";
84 std::copy (prev_m1.begin (), prev_m1.end (), IntOSIter (std::cout, " "));
85
86 std::cout << "\n Next Permutation: ";
87 std::copy (next_m1.begin (), next_m1.end (), IntOSIter (std::cout, " "));
88
89 std::cout << "\n\nExample 2: \n Original values: ";
90 std::copy (m2.begin (), m2.end (), CharOSIter (std::cout, " "));
91
92 std::cout << "\n Previous Permutation: ";
93 std::copy (prev_m2.begin (), prev_m2.end (), CharOSIter (std::cout, " "));
94
95 std::cout << "\n Next Permutation: ";
96 std::copy (next_m2.begin (), next_m2.end (), CharOSIter (std::cout, " "));
97
98 std::cout << std::endl << std::endl;
99
100 return 0;
101}
#define m2
Definition 17.names.cpp:262
#define m1
Definition 17.names.cpp:236
#define a2
Definition 17.names.cpp:251
#define a1
Definition 17.names.cpp:225
int main()