Sorting book title in order alphabetically

How do I return a vector of all books sorting by title in alphabetical order? I created a vector called book which consist of the book title, ID and author but I just need to sort and print the title.

Bookshelf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 #include "Bookshelf.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

Bookshelf::Bookshelf(vector <Book*> listofBooks){
	this->listofBooks = listofBooks;
}

void Bookshelf::addBook(int id, string title, string author){
	
	cout << "Enter an ID:";
	cin >> id;
	cout << "Enter a title:";
	cin >> title;
	cout << "Enter an author:";
	cin >> author;

	
}

vector<Book*>Bookshelf::returnListofBooks(string title){

	for (vector<Book*>::iterator it = myBook.begin(); it != myBook.end(); ++it)
		cout << ' ' << *it;

	sort(myBook.begin(), vs.end());

	cout << "\n";

	for (vector<Book*>::iterator it = myBook.begin(); it != myBook.end(); ++it)
		cout << ' ' << *it;



}


int main(){
	vector <Book*> myBook;
	myBook.push_back(new Book(01,"The Da Vinci Code","Dan Brown"));
	myBook.push_back(new Book(02, "Charlotte's Web", "E.B. White"));
	return 0;


}
One way would be to sort the vector of the books and then loop through the vector and print the titles.

Do you have to use a vector ? A better way to store the books would be a set or map.
if you just want to print the titles alphabetically, the returnListofBook function doesn't need to return a vector<Book *>, it just need to return a set<string>...
and the returnListofBook function need to be granted friendship in the Book class

1
2
3
4
5
6
7
8
9
set<string> returnListofBook(vector<Book *> myBook)   // #include<set>
{
    set<string> titles;
    for(auto item: mybook)
    {
        titles.insert(item->title);       // i guess the Book class has a title data member
    }
    return titles;
}


after this function, you'll get a collection of titles ordered alphabetically
@Thomas1965 I could use either a vector or an array as my lecturer did not taught set/map yet.

@qishuhao Thanks for the help but I'm having errors with this particular code. It says "myBook is unidentified"

 
 for(auto item: mybook)


i'm sorry for typing it wrong, it should be myBook, not mybook
Ok JoanT,

if you can't use set or map then I would do it like this.

1
2
3
4
5
6
7
8
9
10
11

void GetBookTitles(vector<Book *> &books, vector<string> &titles)
{
   for(auto book: books)
    {
        titles.push_back(book->title);   
    }
    sort(books.begin(), books.end()); // only if books is not sorted already
    return titles;
}
@qishuhao @Thomas1965 Thank you so much for your help. I managed to run the program with no errors now. Also, is there any way whereby i create a bookshelf object and calling the addbook method and allowing the user to input the detail of each book and store it into the vector 'Book'?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Bookshelf::addBook(int id, string title, string author){
	
	cout << "Enter an ID:";
	cin >> id;
	cout << "Enter a title:";
	cin >> title;
	cout << "Enter an author:";
	cin >> author;

	
}

int main(){
	vector <Book*> myBook;
	myBook.push_back(new Book(01, "The Da Vinci Code", "Dan Brown"));
	myBook.push_back(new Book(02, "Charlotte's Web", "E.B. White"));


	Bookshelf *myBookshelf = new Bookshelf(); // this the part I'm strucked at. 
	


	return 0;
Last edited on
One option would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Bookshelf::addBook(int id, string title, string author)
{
   Book *book = new Book(id, title, author);
	
  // Not sure  what the name of the vector with the books  is.
  
  YourBookVector.push_back(book);
}

int main()
{
   cout << "Enter an ID:";
   cin >> id;
   cout << "Enter a title:";
   cin >> title;
   cout << "Enter an author:";
   cin >> author;

   Bookshelf myBookshelf;
   myBookshelf.addBook(id, title, author);

   return 0;
@Thomas1965 Thanks for your help once again. I'm still having errors with some of the code I added and I'm not sure what do these errors mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Bookshelf::addBook(int ID, string Title, string Author){
	
	Book *mybook = new Book(ID, Title, Author);
	vector<Book*>.push_back(mybook); //Error: Expected an identifier and variable 'mybook' is not a type name.
	
}

int main(){
	vector <Book*> myBook;
	myBook.push_back(new Book(01, "The Da Vinci Code", "Dan Brown"));
	myBook.push_back(new Book(02, "Charlotte's Web", "E.B. White"));

	cout << "Enter an ID:";
	cin >> ID; //Error: Identifier 'ID' is undefined.
	cout << "Enter a title:";
	cin >> Title; //Error: Identifier 'Title' is undefined.
	cout << "Enter an author:";
	cin >> Author; //Error: Identifier 'Author' is undefined.
	
	Bookshelf myBookshelf; //Error: No default constructor exists for class 'Bookshelf'
	myBookshelf.addBook(ID, Title, Author);

	return 0;

}

 
vector<Book*>.push_back(mybook);



You need to instantiate the vector first, like what you did in line 9.
1
2
vector<Book*> vBook;
vBook.push_back(myBook);


I would make a vector a parameter of the addBook function.
1
2
3
4
void Bookshelf::addBook(int ID, string Title, string Author, vector<Book*> vBook)
{
// do your stuff here
}


Make sure to delete the dynamically allocated memory!
1
2
3
// C++11 range based for loop
for(auto b: vBook)
        delete b;


The reason why ID, Title and Author are undefined is because they don't exist in the scope of main(). Just make them variables in main and that should fix them being undefined.

To explicitly define a default constructor do
1
2
// C++11 default constructor, just learnt this recently
Book() = default;
Last edited on
@integralfx Thanks for the help. So this are my codes so far for bookshelf.cpp. I'm still not sure how to define a default constructor.

Bookshelf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Bookshelf.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

Bookshelf::Bookshelf(vector <Book*> listofBooks){
	this->listofBooks = listofBooks;
}

void Bookshelf::addBook(int ID, string Title, string Author, vector<Book*> myBook){
	
	Book *mybook = new Book(ID, Title, Author);
	vector<Book*> vBook;
	vBook.push_back(mybook);
	
}



set<string> returnListofBooks(vector<Book *> myBook)   
{
	set<string> titles;
	for (auto item : myBook)
	{
		titles.insert(item->getTitle());     
	}
	return titles;
}




int main(){
	vector <Book*> myBook;
	myBook.push_back(new Book(01, "The Da Vinci Code", "Dan Brown"));
	myBook.push_back(new Book(02, "Charlotte's Web", "E.B. White"));

	int ID;
	string Title;
	string Author;

	cout << "Enter an ID:";
	cin >> ID;
	cout << "Enter a title:";
	cin >> Title;
	cout << "Enter an author:";
	cin >> Author;
	
	Bookshelf() = default; //Error: No instance of constructor "Bookshelf::Bookshelf" matches the argument list.
	Bookshelf myBookshelf; 
	myBookshelf.addBook(ID, Title, Author, myBook);

return 0;

}

Hi JoanT,

a default constructor is a constructor without parameters.

1
2
3
4
5
6
7
8
9
10
11
class Bookshelf
{
public:
  Bookshelf(); // declaration of the default constructor
};


Bookshelf::Bookshelf() // definition / implementation of the default constructor
{
  // initialization of variables and other stuff here
}



I thought you can't use a set?

set<string> returnListofBooks(vector<Book *> myBook)


BTW. The Bookshelf class should maintain the collection(vector) of the books and nobody else.
@Thomas1965 Thanks. I understand more about default constructor now. I thought i wasn't supposed to use set as my lecturer has not taught us this topic yet but he said it's fine if I could do it.
Topic archived. No new replies allowed.