当前位置:编程学习 > C/C++ >>

练习使用 STL sort 对容器进行排序

主要练习使用 std::sort 对 std::vector, std::string 等进行排序,并学习如何通过指定自定义排序方法或者重载 operator < 操作符等方式来自定义排序规则。
[cpp]  
#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";  
  &nb
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,