Repository navigation
Expand file tree
/
Copy pathSTLStructuredSort.cpp
More file actions
146 lines (103 loc) · 2.43 KB
/
Copy pathSTLStructuredSort.cpp
File metadata and controls
146 lines (103 loc) · 2.43 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
using namespace std;
/*
STL sort, Structure Sort, Vector sort, String sort
*/
struct Student {
string name;
int roll;
double cgpa;
};
void gradefillup(Student student[])
{
student[0].name = "Chumma";
student[0].roll = 129;
student[0].cgpa = 3.55;
student[1].name = "dede";
student[1].roll = 123;
student[1].cgpa = 3.55;
student[2].name = "gandu";
student[2].roll = 134;
student[2].cgpa = 3.56;
student[3].name = "marunga";
student[3].roll = 145;
student[3].cgpa = 3.56;
student[4].name = "sala";
student[4].roll = 154;
student[4].cgpa = 3.88;
student[5].name = "Jhukunga";
student[5].roll = 143;
student[5].cgpa = 3.88;
student[6].name = "nehi";
student[6].roll = 132;
student[6].cgpa = 3.76;
student[7].name = "sala";
student[7].roll = 211;
student[7].cgpa = 3.76;
}
bool cmp(Student A, Student B)
{
if(A.cgpa != B.cgpa) return A.cgpa < B.cgpa;
return A.roll < B.roll;
}
int main()
{
int ar[20+3];
vector <int> ar2;
for(int i = 0; i<20; i++)
ar2.push_back(ar[i] = rand());
cout<<"Unsorted: ";
for(int i = 0; i<20; i++)
printf("%d ", ar[i]);
cout<<endl;
/*
to sort using STL function set limit from beginning to end + 1
*/
sort(ar, ar+20);
cout<<"Sorted : ";
for(int i = 0; i<20; i++)
printf("%d ", ar[i]);
cout<<endl<<endl;
/*
Structure sort.
a sort technique that allows multiple restrictions
*/
Student student[8];
gradefillup(student);
sort(student, student+8, cmp);
for(int i = 0; i<8; i++)
{
cout<<student[i].name<<" "<<student[i].roll<<" "<<student[i].cgpa<<endl<<endl;
}
/* vector sort*/
cout<<endl<<endl<<"Unsorted Vector: "<<endl;;
for (auto i = ar2.begin(); i< ar2.end(); i++)
{
cout<<*i<<" ";
}
cout<<endl;
cout<<"Sorted Vector: "<<endl;;
sort(ar2.begin(), ar2.end());
for (auto i = ar2.begin(); i< ar2.end(); i++)
{
cout<<*i<<" ";
}
cout<<endl;
/* String sort*/
vector <string> vstr;
int n;
char s[100];
scanf("%d", &n);
for(int i = 0; i<n; i++)
{
scanf("%s", s);
vstr.push_back(s);
}
sort(vstr.begin(), vstr.end());
for (auto i = vstr.begin(); i< vstr.end(); i++)
{
cout<<*i<<" ";
}
cout<<endl;
return 0;
}