Jump to content

  • Log In with Google      Sign In   
  • Create Account

Vector help.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 30 October 2012 - 10:12 PM

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.

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 19556

Like
0Likes
Like

Posted 30 October 2012 - 10:29 PM

You can access each string in the vector with either:
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[i] << 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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 Washu   Senior Moderators   -  Reputation: 5189

Like
3Likes
Like

Posted 30 October 2012 - 10:40 PM

... good stuff ...

With C++11:
for(auto const& val : inventory)
    std::cout<<val<<std::endl;

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#4 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 30 October 2012 - 10:58 PM

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.

#5 Washu   Senior Moderators   -  Reputation: 5189

Like
3Likes
Like

Posted 30 October 2012 - 11:07 PM

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>?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#6 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 30 October 2012 - 11:11 PM

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.

#7 BeerNutts   Crossbones+   -  Reputation: 2945

Like
0Likes
Like

Posted 31 October 2012 - 11:33 AM

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[i].getName();
}

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS