开发者

help with set & map on STL for c++

I am taking a CS class, and most of the assignments were in java. in the last java assignment we learned collections. This assignment we are using c++ and i need to learn the STL The required book is all in java. We were given this webpa开发者_开发技巧ge http://www.cplusplus.com/ however, that and google is not going to get me through the assignment. Can you recommend a good book on STL and c++? I need to learn mostly about set and map.

This assignment is about a media library(book,cd,dvd info)

we were given a skeleton program to work with and in the library class i am not sure what the set and map are actually for regarding what goes in what for the book, CD, DVD information..? can someone tell me based off of the functions what goes into what set/map?

typedef set<Item*>             ItemSet;
typedef map<string,Item*>       ItemMap;
typedef map<string,ItemSet*>     ItemSetMap;

class Library
{

public:
// general functions

void addKeywordForItem(const Item* const item, const string& keyword);
const ItemSet* itemsForKeyword(const string& keyword) const;
void printItem(ostream& out, const Item* const item) const;

// book-related functions

const Item* addBook(const string& title, const string& author, int const nPages);
const ItemSet* booksByAuthor(const string& author) const;
const ItemSet* books() const;

// music-related functions

const Item* addMusicCD(const string& title, const string& band, const int nSongs);
void addBandMember(const Item* const musicCD, const string& member);
const ItemSet* musicByBand(const string& band) const;
const ItemSet* musicByMusician(const string& musician) const;
const ItemSet* musicCDs() const;

// movie-related functions

const Item* addMovieDVD(const string& title, const string& director, const int nScenes);
void addCastMember(const Item* const movie, const string& member);
const ItemSet* moviesByDirector(const string& director) const;
const ItemSet* moviesByActor(const string& actor) const;
const ItemSet* movies() const;
};


Okay, the key question is which operations you need to support. The operations dictate which data structure you should use.

For example, for books you've got:

const Item* addBook(const string& title, const string& author, int const nPages);
const ItemSet* booksByAuthor(const string& author) const;
const ItemSet* books() const;

This means that you need sets of books: all books and by author. Therefore you need a map which maps author to the set of books having that author.

Your functions might look like this:

ItemSet allBooks;
ItemSetMap allBooksByAuthor;
...

const Item* addBook(const string& title, const string& author, int const nPages)
{
    BookItem* item = new BookItem(...);
    allBooks.insert(item); // add to set of all books
    allBooksByAuthor[author].insert(item); // add to set of books by this author
    return item;
}

const ItemSet* booksByAuthor(const string& author) const
{
    return allBooksByAuthor[author];
}

const ItemSet* books() const
{
    return allBooks;
}


I've found the STL Tutorial and Reference Guide to be both definitive and very helpful in learning about the basic STL data structures.


The "good books" part has been handled elsewhere - e.g. here.

The obvious missing detail is the definition of the "Item" type. Based on the "add" functions, it probably just contains three fields - two strings and an int.

"set" is a set of pointers to items. It's used for results from query functions - your results are basically sorted sets of unique records, since a set contains a sorted sequence of unique items.

"map" is a mapping from strings to item pointers. Most is most likely to be used to "index" your data items. A map contains (key, data) pairs, the keys being sorted and unique. You will probably have some private member data using this type to hold the data from the "add" methods. Since the keys are unique, this index only works for a key that has no duplicates.

"map" follows the same principles as before, but with some extra twists, since the data part of the (key, data) pairs is a pointer to a set of items. An index for data items, again, but allowing for duplicates by holding a set of results for each key.

This is odd - you'd normally use a multimap to allow duplicates directly. I'm guessing the reason why not is that you're not ready for iterators. As it is, you should be able to implement these methods using the "insert" method and (for the maps) the "[]" subscripting operator.

  • http://www.cppreference.com/wiki/stl/map/start
  • http://www.cppreference.com/wiki/stl/set/start
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜