I am trying to find an author in a set, and i am having problems doing so.
in the library.cpp is where i am adding the information from main()
#include "Library.h"
#include "book.h"
#include "cd.h"
#include "dvd.h"
#include <iostream>
// general functions
ItemSet allBooks;
ItemSet allCDS;
ItemSet allDVDs;
ItemSetMap allBooksByAuthor;
ItemSetMap allmoviesByDirector;
ItemSetMap allmoviesByActor;
ItemSetMap allMusicByBand;
ItemSetMap allMusicByMusician;
const Item* Library::addBook(const string& title, const string& author, const int nPages)
{
ItemSet* obj = new ItemSet();
Book* item = new Book(title,author,nPages);
allBooks.insert(item); // add to set of all books
obj->insert(item);
allBooksByAuthor[author] = obj;
return item;
}
const ItemSet* Library::booksByAuthor(const string& author) const
{
return allBooksByAuthor[author];
}
I want to be able to return all the books by a particular author. Right now i am adding them by
allBooksByAuthor[author] = obj;
it does work. However,
it is a problem since its treating an author of two different books as a duplicate.
so, it will only return one book.
What can i do.. keep in mind i cannot change the function or the sets. so, any const must stay… but can make a new set.
How about checking to see whether the author is in allBooksByAuthor before inserting them?
will replace the ItemSet* which is already in there. So, your ItemSet will only ever have one Item* in it. How about something more like
That way, you’ll add the new Item to the existing ItemSet rather than always creating a fresh one with one item.
I would also point out that by having your ItemSet containing sets of Item* rather than Item, it’s only going to make sure that you’re not holding the same pointer in there. You could still have identical Items in your ItemSet. You’ll need to either make it hold Items directly (which won’t work in this case because the Items pointed to are actually subclasses of Item) or give it a custom comparator if you want to guarantee that it won’t contain duplicate Items.