-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_3_TestStableSort.cpp
More file actions
54 lines (49 loc) · 1 KB
/
11_3_TestStableSort.cpp
File metadata and controls
54 lines (49 loc) · 1 KB
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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename Iter>
void print(Iter begin, Iter end)
{
while (begin != end)
{
cout << "[" << *begin << "] ";
++begin;
}
cout << "\n----------------------\n";
}
class User
{
int age;
string name;
public:
User(string _nm, int _age) : name(_nm), age(_age) {}
bool operator< (const User& user) const
{
return age < user.age;
}
friend ostream& operator<< (ostream& o, const User& user);
};
ostream& operator<< (ostream& o, const User& user)
{
return o << user.name << ", " << user.age;
}
int main(void)
{
vector<User> vec;
for (int i = 0; i < 100; ++i)
{
string name = "";
name.push_back('a' + i / 26);
name.push_back('a' + i % 26);
vec.push_back(User(name, static_cast<int>(rand() % 10)));
}
vector<User> vec2;
vec2 = vec;
print(vec.begin(), vec.end());
sort(vec.begin(), vec.end());
print(vec.begin(), vec.end());
stable_sort(vec2.begin(), vec2.end());
print(vec2.begin(), vec2.end());
}