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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
graydragon10
  • 101
  • 5
  • 7
    You might want to learn C++ through tutorial. We can hardly teach you C++ at a Q&A site. See [this list of great C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sbi Sep 28 '10 at 18:22
  • If I knew everything about C++, I wouldn't need to go to a Q&A site. been going through tutorials, reading books, and trying to get help from fellow programmers :) – graydragon10 Sep 28 '10 at 19:22
  • 2
    perhaps some grounding in abstract data types and algorithms might help here as well. Your describing an uncertain problem with a final technical option you have chosen, when you don't seem to have background knowledge in how to pick the right technical solution. 'Inventory system' for a car yard, online book store, computer game, home dvd catalogue? what are the inputs/outputs, how many synchronus changes and how often? – Greg Domjan Sep 28 '10 at 19:59
  • I figure the "game development" tag would make that clear :-/ I described it as an inventory system and that I wanted to link the name of the item with its quantity via a map. Knowing these two pieces of the puzzle, it isn't so much what the items do or how they would be shown to the user that matters but getting the logic and syntax sorted out. – graydragon10 Sep 28 '10 at 20:09

3 Answers3

4

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jason R. Mick
  • 5,177
  • 4
  • 40
  • 69
3

If you're talking about std::map, it's a template which can work with any type of object, as long as a way to compare objects for ordering is provided. Since your key (the name) is a string, it will work right out of the box with std::map

struct Item
{
    std::string description;
    int value;
};


int main()
{
    // associate item names with an item/quantity pair
    std::map<std::string, std::pair<Item, int> > itemmap;
}
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
1

I need something keeping track of the quantity of each type of item I have.

How about std::vector<std::pair<Item, int> >?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662