Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 655653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:37:41+00:00 2026-05-13T22:37:41+00:00

I am trying to find an author in a set, and i am having

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:37:42+00:00Added an answer on May 13, 2026 at 10:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a regex to separate out the author and book title
Trying to find a way to remove blank pages from a document I wrote
Trying to find a way to send a POST HTTPS request from Python to
Im trying to find a set of results in a database based on dates.
I'm trying to find a better way to express this in jade: p.author.alignleft |
I'm trying to use the MySQL command show indexes from table_name to find out
I am trying to extract (source code line, author label) pair from git repositories.
Trying to find a standard. The CmdLet will process data - multiple input, defined
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
Trying to find the best way of create an overlap/overlay layer to fill the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.