answersLogoWhite

0


Best Answer

#include<iostream>

#include<string>

#include<vector>

struct book

{

std::string title;

std::string author;

// other members ...

};

int main()

{

std::vector<book> books;

books.push_back ( book {"Moby Dick", "Herman Melville"});

// ...

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<iomanip>

#include<set>

#include<string>

using std::string;

class Book

{

friend bool operator< (const Book& lhs, const Book& rhs);

public:

Book (const string& title, const string& author, const string& title_key, const string& author_key):

m_title (title), m_author (author), m_title_key (title_key), m_author_key (author_key) {}

public:

string title() const { return m_title; }

string author() const { return m_author; }

private:

string m_title;

string m_author;

string m_title_key;

string m_author_key;

};

inline bool operator< (const Book& lhs, const Book& rhs)

{

// books are sorted in ascending order by title_key then by author_key

return

lhs.m_title_key < rhs.m_title_key (

!(rhs.m_title_key < lhs.m_title_key) &&

lhs.m_author_key < rhs.m_author_key);

}

int main()

{

std::set<Book> books;

// Initialise books in unsorted order

books.insert (Book ("The Lord of the Rings", "J. R. R. Tolkien", "Lord of the Rings, The", "Tolkien, J. R. R."));

books.insert (Book ("A Tale of Two Cities", "Charles Dickens", "Tale of Two Cities, A", "Dickens, Charles"));

books.insert (Book ("Moby-Dick", "Herman Melville", "Moby-Dick", "Melville, Herman"));

books.insert (Book ("The Hobbit", "J. R. R. Tolkien", "Hobbit, The", "Tolkien, J. R. R."));

// Print the set (automatically sorted)

for (auto book : books)

std::cout << std::setw(30) << std::left << book.title() << book.author() << std::endl;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in c plus plus to sort a book list in library?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Sort a book list in a library based on the discipline USING C plus plus programming?

Write your own C++ functions for the following problems:o Sort a book list in a library based on the disciplineo Print the sorted output on the console


How to write aC program to merge two singly linked list?

write a c program to circular queue


Write a program in C to create a Circular Linked list?

#include&lt;iostream.h&gt;


Where is list of Broadway shows since 1984?

it is in the library inside a book call Broadway


How can i find list of professors who taught at Princeton university in the 1800s?

Write to the Library at Princeton University.


Write a c program add two polynomial using link list?

GOUDHMARINI


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


What describes who was prosecuted in the holocaust?

Go to the library and check a book out or buy the movie "The Schindler's List."


Write a HTML program to insert a selection list in a form?

&lt;select id="selectlist"&gt;&lt;option&gt; First&lt;/option&gt; &lt;option&gt; Second&lt;/option&gt;. This is a sample program to insert a selection list.


What a list or index of books in a library called?

The book list. Or book index. I forget. Google it. Ask a librarian. Just stop asking me. Man, I wish I never took this job


What is A list or index of books in library is called?

The book list. Or book index. I forget. Google it. Ask a librarian. Just stop asking me. Man, I wish I never took this job


Where can you get a list of the 1000 most important artists?

Such a list may not exist, but look in a library for a really big Art History book. The artists will be in it.