主要练习使用 std::sort 对 std::vector, std::string 等进行排序,并学习如何通过指定自定义排序方法或者重载 operator < 操作符等方式来自定义排序规则。
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
struct Vec2D
{
float x;
float y;
Vec2D() : x(0.f), y(0.f)
{
}
Vec2D(float fx, float fy) : x(fx), y(fy)
{
}
};
std::ostream& operator << (std::ostream &out, const Vec2D &val)
{
return out<<"("<<val.x<<","<<val.y<<")";
}
struct Vec3D
{
float x;
float y;
float z;
Vec3D() :x(0.f), y(0.f), z(0.f)
{
}
Vec3D(float fx, float fy, float fz) : x(fx), y(fy), z(fz)
{
}
float length_squared() const
{
return x * x + y * y + z * z;
}
float length() const
{
return sqrtf(length_squared());
}
bool operator < (const Vec3D &rhs)
{
return length_squared() < rhs.length_squared();
}
};
std::ostream& operator << (std::ostream &out, const Vec3D &val)
{
return out<<"("<<val.x<<","<<val.y<<","<<val.z<<")";
}
template <class T>
void print_vector(const std::vector<T> &v)
{
using std::cout;
using std::endl;
for (std::vector<T>::const_iterator iter = v.begin(); iter != v.end(); ++iter)
{
cout<<(*iter)<<"\t";
}
cout<<endl;
}
// vector<int>排序方法,这里按绝对值由小到大排序
static bool sort_comp_int(int elem1, int elem2)
{
return abs(elem1) < abs(elem2);
}
// vector<Vec2D>排序方法,这里先按x由大到小排序,如果x相同再按y由小到大排序
static bool sort_comp_Vec2D(const Vec2D &elem1, const Vec2D &elem2)
{
if (elem1.x > elem2.x)
return true;
else if (elem1.x == elem2.x)
return elem1.y < elem2.y;
return false;
}
int main(int argc, char **argv)
{
using std::cout;
using std::cin;
using std::endl;
cout<<"========== sort std::vector<int> ==============="<<endl;
std::vector<int> vi;
vi.push_back(3);
vi.push_back(-4);
vi.push_back(1);
vi.push_back(5);
cout<<"before sort: "<<endl<<"\t";
print_vector<int>(vi);
// 使用默认的排序算法(operator <)由小到大排序
std::sort(vi.begin(), vi.end());
cout<<"sort default: "<<endl<<"\t";
print_vector<int>(vi);
// 使用 STL 提供的由大到小的排序算法
std::sort(vi.begin(), vi.end(), std::greater<int>());
cout<<"sort greater: "<<endl<<"\t";
print_vector<int>(vi);
// 使用自定义的排序算法
std::sort(vi.begin(), vi.end(), sort_comp_int);
cout<<"sort user defined: "<<endl<<"\t";
print_vector<int>(vi);
//
cout<<"========== sort std::string ==============="<<endl;
std::string s = "Hello, world!";
cout<<"before sort: "<<endl<<"\t";
cout<<s<<endl;
std::sort(s.begin(), s.end());
cout<<"sort default: "<<endl<<"\t";
cout<<s<<endl;
//
cout<<"========== sort std::vector<Vec2D> ==============="<<endl;
std::vector<Vec2D> v2;
v2.push_back(Vec2D(3.f, -1.f));
v2.push_back(Vec2D(1.f, 6.f));
v2.push_back(Vec2D(3.f, 3.2f));
v2.push_back(Vec2D(2.f, 0.f));
cout<<"before sort: "<<endl<<"\t";
print_vector<Vec2D>(v2);
// 使用自定义排序算法
std::sort(v2.begin(), v2.end(), sort_comp_Vec2D);
cout<<"sort user defined: "<<endl<<"\t";
print_vector<Vec2D>(v2);
//
cout<<"========== sort std::vector<Vec3D> ==============="<<endl;
std::vector<Vec3D> v3;
v3.push_back(Vec3D(1.f, 0.f, 0.f));
v3.push_back(Vec3D(1.f, 1.f, 1.f));
v3.push_back(Vec3D(0.f, 2.f, 0.f));
v3.push_back(Vec3D(0.f, 1.f, 1.f));
cout<<"before sort: "<<endl<<"\t";
print_vector<Vec3D>(v3);
// 默认的排序算法使用 operator < 作为排序方法
std::sort(v3.begin(), v3.end());
cout<<"sort operator <: "<<endl<<"\t";
print_vector<Vec3D>(v3);
cin.get();
return EXIT_SUCCESS;
}
输出结果:
========== sort std::vector<int> ===============
before sort:
3 -4 1 5
sort default:
-4 1 3 5
sort greater:
5 3 1 -4
sort user defined:
1 3 -4 5
========== sort std::string ===============
before sort:
Hello, world!
sort default:
!,Hdellloorw
========== sort std::vector<Vec2D> ===============
before sort:
(3,-1) (1,6) (3,3.2) (2,0)
sort user defined:
(3,-1) (3,3.2) (2,0) (1,6)
========== sort std::vector<Vec3D> ===============
before sort:
(1,0,0) (1,1,1) (0,2,0) (0,1,1)
sort operator <:
(1,0,0) (0,1,1) (1,1,1) (0,2,0)