小白求问类的数组怎么用sort排序啊

「已注销」 2018-01-11 06:09:23
class book
{
private:
string isbn; //书号
string name; //书名
string writer; //作者
string publisher; //出版社
string time; //出版时间
string price; //图书价格
int inventory; //图书库存量
public:
book() {}
book(string a,string b,string c,string d,string e,string f,int g=0)
{
isbn=a;
name= b;
writer=c;
publisher=d;
time=e;
price=f;
inventory=g;

}

string get_name() const
{
return name;
}

string get_price() const
{
return price;
}

string get_isbn() const
{
return isbn;
}

string get_writer() const
{
return writer;
}
string get_time() const
{
return time;
}

string get_publisher() const
{
return publisher;
}
int get_inventory() const
{
return inventory;
}
void BuyBook ();
void creat();
void edit(string a, string b,string c,string d,string e,string f,int g,bool flag);
void display() ; //图书内容的显示
};

book books[1000];

我想对价格进行升序排序,怎么排
...全文
454 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
super_admi 2018-01-11
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;

class book
 {
 private:
    string isbn;        //书号
    string name;        //书名
    string writer;      //作者
    string publisher;   //出版社
    string time;        //出版时间
    string price;       //图书价格
    int inventory;      //图书库存量
public:
     book() {}
     book(string a,string b,string c,string d,string e,string f,int g=0)
     {
         isbn=a;
         name= b;
         writer=c;
         publisher=d;
         time=e;
         price=f;
         inventory=g;

     }

     string  get_name() const
     {
         return name;
     }

     string  get_price() const
     {
         return price;
     }

     string  get_isbn() const
     {
         return isbn;
     }

     string  get_writer() const
     {
         return writer;
     }
     string  get_time() const
     {
         return time;
     }

     string  get_publisher() const
     {
         return publisher;
     }
     int get_inventory() const
     {
         return inventory;
     }
     void BuyBook ();
     void creat();
     void edit(string a, string b,string c,string d,string e,string f,int g,bool flag);
     void display()  ;       //图书内容的显示
};

bool compare(book b1, book b2)
{
    return(b1.get_name() > b2.get_name());
}

int main()
{
    vector<book> books;
    books.push_back(book("a1","b1","c1","d1","e1","f1", 10));
    books.push_back(book("a2","b2","c2","d2","e2","f2", 10));
    std::cout<<"init:"<<std::endl;
    for(int i = 0; i < books.size(); i++)
    {
        std::cout<< books[i].get_name()<<std::endl;
    }
    std::cout<<"sort:"<<std::endl;
    std::sort(books.begin(), books.end(), compare);
    for(int i = 0; i < books.size(); i++)
    {
        std::cout<< books[i].get_name()<<std::endl;
    }
    return (0);
}
幻夢之葉 2018-01-11
  • 打赏
  • 举报
回复
重载<比较操作符,使用库函数sort进行排序即可 或者自己写个排序算法
真相重于对错 2018-01-11
  • 打赏
  • 举报
回复
http://blog.csdn.net/bz201/article/details/543001

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧