tags:

views:

112

answers:

4

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.

+1  A: 

You need to use a multiset instead of a set.

http://www.cplusplus.com/reference/stl/multiset/

Joel
+1  A: 

The std::map is limited to allowing only one value for each unique key (author). If you want multiple values per key/author then you should use an std::multimap.

Peter Alexander
A: 

How about checking to see whether the author is in allBooksByAuthor before inserting them?

allBooksByAuthor[author] = obj;

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

ItemSetMap::iterator authorsBooks = allBooksByAuthor.find(author);

if(authorsBooks != allBooksByAuthor.end())
{
    authorsBooks->second->insert(item);
}
else
{
    allBooksByAuthor.insert(make_pair(author, obj));
}

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.

Jonathan M Davis
A: 

You should use multiset/multimap and the equal_range method instead of find. equal_range returns a pair of iterators. The pair.first is the first thing you're searching and pair.second first thing beyond the last thing you're searching.

You can iterate pair.first until pair.second.

You can also count the number of similar key with the count method.

anno