Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
sort.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for sort
30#include <iostream> // for cout
31#include <utility> // for pair
32#include <vector> // for vector
33
34#include <examples.h>
35
36
37typedef std::pair<int, char> Pair;
38
40{
41 bool operator() (const Pair &x, const Pair &y) const {
42 return x.first < y.first;
43 }
44};
45
46inline std::ostream& operator<< (std::ostream &s, const Pair &x)
47{
48 return s << '<' << x.first << ';' << x.second << '>';
49}
50
51int main ()
52{
53 const Pair array[] = {
54 Pair (-4, ' '), Pair (16, ' '), Pair (17, ' '), Pair (-3, 's'),
55 Pair (14, ' '), Pair (-6, ' '), Pair (-1, ' '), Pair (-3, 't'),
56 Pair (23, ' '), Pair (-3, 'a'), Pair (-2, ' '), Pair (-7, ' '),
57 Pair (-3, 'b'), Pair (-8, ' '), Pair (11, ' '), Pair (-3, 'l'),
58 Pair (15, ' '), Pair (-5, ' '), Pair (-3, 'e'), Pair (15, ' ')
59 };
60
61 typedef std::vector<Pair, std::allocator<Pair> > Vector;
62
63 const Vector::size_type array_size = sizeof array / sizeof *array;
64
65 // Create two modifiable sequences.
66 Vector v1 (array, array + array_size);
67 Vector v2 (v1);
68
69 // Sort vector #1.
70 std::sort (v1.begin (), v1.end (), PairCompare ());
71
72 // Stable sort vector #2.
73 std::stable_sort (v2.begin (), v2.end (), PairCompare ());
74
75 // Display the original and the sorted sequences.
76 std::cout << "original std::sort std::stable_sort\n";
77
78 for (Vector::size_type i = 0; i != array_size; ++i)
79 std::cout << array [i] << " "
80 << v1 [i] << " "
81 << v2 [i] << '\n';
82
83 return 0;
84}
#define i
Definition 17.names.cpp:126
#define y
Definition 17.names.cpp:141
#define v2
Definition 17.names.cpp:271
#define x
Definition 17.names.cpp:140
#define array
Definition 17.names.cpp:447
#define s
#define v1
Definition 17.names.cpp:245
std::vector< UserClass, std::allocator< UserClass > > Vector
A TOML array.
Definition array.hpp:258
std::ostream & operator<<(std::ostream &s, const Pair &x)
Definition sort.cpp:46
std::pair< int, char > Pair
Definition sort.cpp:37
int main()
Definition sort.cpp:51
bool operator()(const Pair &x, const Pair &y) const
Definition sort.cpp:41