Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Sorting a vector
- Sorting an int vector
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
#include <bits/stdc++.h>
using namespace std;
vector<int> Data = {3,15,-88,-3,88,103,32,1};
void printData(const string& MSG){
cout << MSG<<" is: {";
for (auto& d:Data) cout << d<<",";
cout << "}"<< endl;
}
int SortByAbs(const int& a,const int& b){
return abs(a)==abs(b)?a<b:(abs(a)<abs(b));
}
int main()
{
//Lambda function to sort
std::sort(Data.begin(),Data.end(), [](const int& a,const int& b){return a<b;});
printData("Lambda");
//Normal function. Sorting by absolute value
std::sort(Data.begin(),Data.end(), SortByAbs);
printData("Function");
}
Enter to Rename, Shift+Enter to Preview
- Sorting an object vector by some field
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
#include <bits/stdc++.h>
using namespace std;
class Pod{
public:
float x,y;
float vx,vy;
Pod(){ } //No initialization
Pod(float _x,float _y, float _vx, float _vy){
x=_x;y=_y;vx=_vx;vy=_vy;
}
};
vector<Pod> Data ={
Pod( 3.0f,1.0f,2.0f,7.0f),
Pod( 15.0f,1.0f,2.0f,7.0f),
Pod(-88.0f,1.0f,2.0f,7.0f),
Pod( -3.0f,1.0f,2.0f,7.0f),
Pod( 88.0f,1.0f,2.0f,7.0f),
Pod(103.0f,1.0f,2.0f,7.0f),
Pod( 32.0f,1.0f,2.0f,7.0f),
Pod( 1.0f,1.0f,2.0f,7.0f)
};
void printData(string MSG){
cout << MSG<<" is: {";
for (auto& d:Data) cout << d.x<<","; //We'll only print x for the sake of simplicity
cout << "}"<< endl;
}
int SortByAbsX(const Pod& a,const Pod& b){
return abs(a.x)==abs(b.x)?a.x<b.x:(abs(a.x)<abs(b.x));
}
int main()
{
//Lambda function to sort
std::sort(Data.begin(),Data.end(), [](const Pod& a,const Pod& b){return a.x<b.x;});
printData("Lambda");
//Normal function. Sorting by absolute value
std::sort(Data.begin(),Data.end(), SortByAbsX);
printData("Function");
}
Enter to Rename, Shift+Enter to Preview
Lambda functions are more compact, aimed as small, simple functions. But harder to reuse.
Check http://en.cppreference.com/w/cpp/algorithm/sort for more info
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content