• Advertisement
Sign in to follow this  

Vector help.

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
You can access each string in the vector with either:
[code]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.
[/code]

(always check inventory.empty() or inventory.size() before accessing an element)

[size=5]To print out everything:[/size]

[b][size=3]By index:[/size][/b]
[code]for(unsigned int i = 0; i < inventory.size(); i++)
{
std::cout << inventory[i] << std::endl;
}[/code]

[size=3][b]With iterators:[/b][/size]
[code]typedef std::vector<std::string>::iterator StrVectorIterator;
for(StrVectorIterator myIt = inventory.begin(); myIt != inventory.end(); myIt++)
{
std::cout << (*myIt) << std::endl;
}[/code]

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.

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1351657773' post='4995692']
... good stuff ...
[/quote]
With C++11:
[code]
for(auto const& val : inventory)
std::cout<<val<<std::endl;
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Mathew Bergen' timestamp='1351659517' post='4995696']
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.
[/quote]

Did you make sure to include <string>?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
BTW, why not have the inventory store the actual Item instead of just the name?
[code]
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[i].getName();
}
[/code]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement