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

  • Home
  • SEARCH
  • 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 3214390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:03:19+00:00 2026-05-17T15:03:19+00:00

Is there any practical way to get objects to work with maps? (I don’t

  • 0

Is there any practical way to get objects to work with maps? (I don’t really know much about maps so sorry if this is a bad question). I’m interested in using a map for an inventory system that will contain item objects. An item object has a name, description, and money value. The key would be the item’s name, and the linking variable would be the the quantity of items I have for that particular item.

And if a map doesn’t work, does anyone have a good alternative to this type of system? I need something keeping track of the quantity of each type of item I have.

  • 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-17T15:03:20+00:00Added an answer on May 17, 2026 at 3:03 pm

    The C++ standard library template map is just a storage container so it can definitely be used with objects. The map will take your object as its templated argument parameter.

    A map would work well for your inventory system. Use something like:

    #include <pair>
    #include <map>
    #include <string>
    #include <iostream>
    
    class Item {
      public:
       Item(void) {}
       ~Item(void) {}
       Item(std::string new_name) {
          my_name=new_name;
       }
       void setName(std::string new_name) {
          my_name= new_name;
       }
       std::string getName(void) {
          return my_name;
       }
      private:
       std::string my_name;
    };  
    
    class Item_Manager {
      public:
       Item_Manager(void) {}
       ~Item_Manager(void) {}  
       void addItem(Item * my_item, int num_items) {
          my_item_counts.insert( std::pair<std::string,int>(Item.getName(),num_items) );
       }
       int getNumItems(std::string my_item_name) {
          return my_item_counters[my_item_name];
       }
      private: 
       std::map<std::string, int> my_item_counts;
    };
    
    main () {
       Item * test_item = new Item("chips");
       Item * test_item2 = new Item("gum");
       Item_Manager * my_manager = new Item_Manager();
    
       my_manager->addItem(test_item, 5);
       my_manager->addItem(test_item2,10);
       std::cout << "I have " << my_manager->getNumItems(test_item->getName())
                 << " " << test_item->getName() << " and " 
                 << my_manager->getNumItems(test_item2->getName())
                 << " " << test_item2->getName() << std::endl;
    
       delete test_item;
       delete test_item2;
       delete my_manager;
    }
    

    Here’s a reference on the stdlib map and its functions:
    http://www.cplusplus.com/reference/stl/map/

    Look at the function pages for examples of how to iterate through/index a map, etc.

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

Sidebar

Related Questions

Is there any real practical difference between java -server and java -client? All I
This is more of an academic inquiry than a practical question. Are there any
Is there any way to check whether a file is locked without using a
Is any practical way to reference a method on a class in a type-safe
Are there any practices regarding using codenames of products in Visual Studio projects and
Are there any best practices (or even standards) to store addresses in a consistent
Is there any good practice related to dynamic_cast error handling (except not using it
Is there any best practice data model for authentication/authorization scheme?
Are there any guidelines/best practices for deciding what type of data should be stored
Are there any known how-tos or best practices for web service REST API versioning?

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.