Hello,
I made a vector of a string and added a member function of an item class getName, witch just returns the name:
[source lang="cpp"]string Item::getName() {
return m_Name;
}[/source]
so i initialized the vector and iterator like so:
[source lang="cpp"]vector<string> inventory;
vector<string>::iterator iter;[/source]
and then:
[source lang="cpp"]inventory.push_back(sword.getName());[/source]
Now my question is how to print the names of the items. the item class:
[source lang="cpp"]class Item{
public:
enum Type{WEAPON, ARMOR, CONSUMABLE, TRASH};
Item(string name, Type type, int rarity, int value);
string getName();
private:
Type m_Type;
string m_Name;
int m_Rarity;
int m_Value;
};
Item::Item(string name, Type type, int rarity, int value): m_Name(name), m_Type(type), m_Rarity(rarity), m_Value(value) {}
string Item::getName() {
return m_Name;
}[/source]
Now what i want to do is make an inventory class that handles adding dropping, equipping, and listing all the items in the inventory. I have no idea how to print the names though. all help is appreciated.
Vector help.
You can access each string in the vector with either:
(always check inventory.empty() or inventory.size() before accessing an element)
To print out everything:
With iterators:
inventory.at(0); //Get the first string in the vector, or throw an exception if out of range.
inventory[0]; //Get the first string in the vector, or hopefully crash if out of range.
inventory.back(); //Get the last string in the vector, or hopefully crash if the vector is empty.
inventory.front(); //Get the first string in the vector, or hopefully crash if the vector is empty.
(always check inventory.empty() or inventory.size() before accessing an element)
To print out everything:
By index:
for(unsigned int i = 0; i < inventory.size(); i++)
{
std::cout << inventory << std::endl;
}
{
std::cout << inventory << std::endl;
}
With iterators:
typedef std::vector<std::string>::iterator StrVectorIterator;
for(StrVectorIterator myIt = inventory.begin(); myIt != inventory.end(); myIt++)
{
std::cout << (*myIt) << std::endl;
}
Iterators are designed to be similar to pointers; you don't need to create an iterator until you actually need it - infact, the iterators will be invalidated if you operate on the vector after getting the iterator.
for(StrVectorIterator myIt = inventory.begin(); myIt != inventory.end(); myIt++)
{
std::cout << (*myIt) << std::endl;
}
Servant, i tried both those ways but i get an error on the output operator saying operator "<<" matches these operands. i think i need to overload a the operator, but the book i learned from was kinda confusing when it taught that.
Servant, i tried both those ways but i get an error on the output operator saying operator "<<" matches these operands. i think i need to overload a the operator, but the book i learned from was kinda confusing when it taught that.
Did you make sure to include <string>?
oh my god, someone told me that the sting was already in iostream so i never needed to include it. god that pisses me off, all i needed to do was include string. well that's for the help.
BTW, why not have the inventory store the actual Item instead of just the name?
std::vector<Item> Inventory;
// Add a sword ...is typical parameters
Item Sword(...);
Inventory.push_back(Sword);
// List your inventory
for (int i = 0; i < Inventory.size(); ++i) {
std::cout << Inventory.getName();
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement