这是您的情况的解决方案。注意不建议在vector使用指针中使用object。
#include <iostream>
#include <vector>
#include <algorithm>
class Book{
public:
Book(std::string title, int pages, std::string gender):
m_title(title),
m_pages(pages),
m_gender(gender)
{}
std::string getTitle() const { return m_title;}
int getPages() const { return m_pages;}
std::string getGender() const { return m_gender;}
private:
std::string m_title;
int m_pages;
std::string m_gender;
};
std::vector<Book*> sortPages(std::vector<Book*> books){ //sorts books in descending order
std::sort(books.begin(), books.end(), [](const Book* lhs, const Book* rhs){
return lhs->getPages() > rhs->getPages();
});
return books;
}
std::vector<Book*> getSpecificGender(std::vector<Book*> books, const std::string gender){ //sorts books in descending order
std::vector<Book*> result;
for(auto it=books.begin();it!=books.end();it++)
{
if(gender == (*it)->getGender())
{
result.push_back(*it);
}
}
std::sort(result.begin(), result.end(), [](const Book* lhs, const Book* rhs){
return lhs->getPages() > rhs->getPages();
});
return result;
}
Book* get2ndRankBook(std::vector<Book*> books, const std::string gender){ //sorts books in descending order
Book* first_rank_book=nullptr;
Book* second_rank_book=nullptr;
std::sort(books.begin(), books.end(), [](const Book* lhs, const Book* rhs){
return lhs->getPages() > rhs->getPages();
});
for(auto it=books.begin();it!=books.end();it++)
{
if(gender != (*it)->getGender())
{
continue;
}
if(!first_rank_book)
{
first_rank_book = *it;
}
if(first_rank_book->getPages()>(*it)->getPages())
{
second_rank_book=first_rank_book;
first_rank_book = *it;
}
}
return second_rank_book;
}
int main(){
Book* book = new Book ("The Hunger Games ", 374, "Adventure fiction");
Book* book1 = new Book ("Game of Thrones", 694, "Fantasy");
Book* book2 = new Book ("Harry Potter and the Philosopher's Stone", 223, "Fantasy");
Book* book3 = new Book ("Holes", 272, "Adventure fiction");
Book* book4 = new Book ("The Hobbit", 400, "Fantasy");
Book* book5 = new Book ("The Maze Runner", 375, "Adventure fiction");
std::vector<Book*> books;
books.push_back(book);
books.push_back(book1);
books.push_back(book2);
books.push_back(book3);
books.push_back(book4);
books.push_back(book5);
sortPages(books);
//get all fantasy books, maybe into a new vector?
//print 2nd element of fantasy books vector
auto fantasy = getSpecificGender(books, "Fantasy");
for(auto it=fantasy.begin();it!=fantasy.end();it++)
{
std::cout<<(*it)->getTitle()<<", "<<(*it)->getPages()<<", "<<(*it)->getGender()<<std::endl;
}
std::cout<<"2nd Rank:"<<std::endl;
Book* second_book = get2ndRankBook(books,"Fantasy");
if(!second_book)
{
std::cout<<"There is no second rank book!"<<std::endl;
}
else
{
std::cout<<second_book->getTitle()<<", "<<second_book->getPages()<<", "<<second_book->getGender()<<std::endl;
}
delete book;
delete book1;
delete book2;
delete book3;
delete book4;
delete book5;
return 0;
}
测试结果:
Game of Thrones, 694, Fantasy
The Hobbit, 400, Fantasy
Harry Potter and the Philosopher's Stone, 223, Fantasy
2nd Rank:
The Hobbit, 400, Fantasy