C++ STL - sort() (about vector, pair and sort())
C++ STL sort()
sorting algorithm is already implemented in C++ library
Defined in header algorithm
C ++ std::sort library sorts the data in ascending order by index order by default
template< class RandomIt, class Compare >
constexpr void sort( RandomIt first, RandomIt last, Compare comp );
- first, last - the range of elements to sort
- comp - comparison function object which returns true if the first argument is less than the second.
sort(RandomIt first, RandomIt last)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10] = {9,5,4,3,2,1,6,7,10,8};
sort(a, a+10);
for(int i = 0; i <10; ++i)
{
cout << a[i] << " ";
}
}
- sort(a, a+10);
- a : start element of the data (the name of the array indicates the first data of the array)
- a+10 : last element of the data (+10 means 10 is added from the start array address)
sort(RandomIt first, RandomIt last, Compare comp)
The powerful point of the sort() function is that we can set the criteria we want to sort by ourselves
Create comp() function and input as the third argument value of sort () function
Sort the datas according to comp() function
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(int a, int b)
{
return a > b;
}
int main()
{
int a[10] = {9,5,4,3,2,1,6,7,10,8};
sort(a, a+10, compare);
for(int i = 0; i <10; ++i)
{
cout << a[i] << " ";
}
}
Sort the Object
#include <iostream>
#include <algorithm>
using namespace std;
class Student
{
public:
string name;
int score;
Student(string name, int score) //Constructor to initilize
{
this->name = name;
this->score = score;
}
bool operator < (Student &student)
{
return this->score < student.score;
}
};
int main()
{
Student students[] = {
Student("Monica", 97),
Student("Rachel", 89),
Student("Pheebs", 92)
};
sort(students, students+3);
for(int i = 0; i <3; i++)
{
cout << students[i].name << " ";
cout << students[i].score <<endl;
}
}
- bool operator < (Student &student)
- When sort by using operator overloading, “<” operator will be used, so redefine if necessary
- return this->score < student.score;
- Return true if my score this.score is low when comparing with other student
Sort the data in Vector using pair
Use library Vector and Pair
Vector: Vector library in the form of a linked list
(1) single pair
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<pair<int, string>> v;
v.push_back(pair<int, string>(90,"Alex"));
v.push_back(pair<int, string>(85,"Tom"));
v.push_back(pair<int, string>(82,"Bread"));
v.push_back(pair<int, string>(98,"Jonny"));
v.push_back(pair<int, string>(79,"Luke"));
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); ++i)
{
cout << v[i].second << " ";
cout << v[i].first <<endl;
}
}
- push_back : inser the data in the end of the list
- v.size(): return the size of the vector
- Second of the currently declared pair means name information
- pair
- std::pair is a struct template that provides a way to store two heterogeneous objects as a single unit.
- A pair is a specific case of a std::tuple with two elements.
- The pair can have first and second values after it was declared.
(2) double pair
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<string, pair<int, int>> a,
pair<string, pair<int, int>> b)
{
if(a.second.first == b.second.first)
return a.second.second > b.second.second;
else
return a.second.first > b.second.first;
}
int main()
{
vector<pair<string, pair<int, int>>> v;
v.push_back(pair<string, pair<int, int>>("Alex", pair<int, int>(90, 19960504)));
v.push_back(pair<string, pair<int, int>>("Dave", pair<int, int>(87, 19940218)));
v.push_back(pair<string, pair<int, int>>("Andy", pair<int, int>(74, 19871214)));
v.push_back(pair<string, pair<int, int>>("Luke", pair<int, int>(98, 19980123)));
v.push_back(pair<string, pair<int, int>>("Clair", pair<int, int>(90, 19820612)));
sort(v.begin(), v.end(), compare);
for(int i = 0; i < v.size(); ++i)
{
cout << v[i].first << " ";
cout << v[i].second.first <<endl;
}
}