Jump to content
  • Advertisement
Sign in to follow this  
rakoon2

Help please: std::vector boost::shared_ptr std::vector ...

This topic is 5152 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

How can I add Items to the Item_Inv? :]
typdef boost::shared_ptr<Item> Item_p;
typdef boost::shared_ptr< std::vector< Item_p > > Item_row_y;
typdef boost::shared_ptr< std::vector< Item_row_y > > Item_row;
typdef std::vector< Item_row > Item_Inv;

Here a picture so you guys can see why I want to do that! http://members.chello.at/ennemoser/inventory-design.jpg Thank you very much! =3 :)

Share this post


Link to post
Share on other sites
Advertisement
Hi rakoon2 hows it going?

if you where to add just one element at a time then something like:


Item_Inv item_inventory;

Item_p it_ptr(new Item());

Item_row_y iry_ptr(new std::vector< Item_p >());

iry_ptr->push_back(it_ptr);

/////////////////////////////////////////////////////

Item_row ir_ptr(new std::vector< Item_row_y >());

ir_ptr->push_back(iry_ptr);

item_inventory.push_back(ir_ptr);


EDIT: if Item is a abstract type then instantiate the what ever sub-type needed.

Share this post


Link to post
Share on other sites
If you're just trying to create a rectangular array of Items, all you need is std::vector< std::vector< boost::shared_ptr< Item > > >. If you want a sparse matrix, use std::map:

typedef boost::shared_ptr< Item > Item_p;
typedef std::map< int, Item_p > Item_row;
typedef std::map< int, Item_row > Item_inv;

Share this post


Link to post
Share on other sites
Argh, that is unmanageably! How should I do this? An Inventory with N Rows that can Hold N rows of items from same type.
Hmm..



Item_Inv item_inventory;

Item_row ir_ptr( new std::vector< Item_row_y >() );
Item_row ir_ptr2( new std::vector< Item_row_y >() );
Item_row ir_ptr3( new std::vector< Item_row_y >() );

item_inventory.push_back(ir_ptr);
item_inventory.push_back(ir_ptr2);
item_inventory.push_back(ir_ptr3);



Item_row_y iry_ptr( new std::vector< Item_p > );
Item_row_y iry_ptr2( new std::vector< Item_p > );
Item_row_y iry_ptr3( new std::vector< Item_p > );
Item_row_y iry_ptr4( new std::vector< Item_p > );


item_inventory[0].push_back( iry_ptr );
item_inventory[0].push_back( iry_ptr2 );
item_inventory[1].push_back( iry_ptr3 );
item_inventory[2].push_back( iry_ptr4 );

// Inventory with 3 rows that can holde items




boost::shared_ptr<Item> item( new Item_common() );
// How can I add the Item now to item_inventory[0]'s 2nd row?





Thank you! :3



Oluseyi: I didn't use std::map yet.. so I have no idea how to use it! :/

Share this post


Link to post
Share on other sites
Quote:
Original post by rakoon2
Argh, that is unmanageably! How should I do this? An Inventory with N Rows that can Hold N rows of items from same type.
Hmm..
Since your inventory is perfectly rectangular, use a vector of vectors. No need adding unnecessary indirection (additional smart pointers) since vector is a dynamically expanding array!

Quote:
Oluseyi: I didn't use std::map yet.. so I have no idea how to use it! :/
Maybe this is a good time to look it up?

Share this post


Link to post
Share on other sites
Also now your using boost more maybe you should try this aswell:


#include <boost\pool\pool_alloc.hpp>
#include <boost\smart_ptr.hpp>
#include <utility> //<- std::pair declared
#include <functional> //<- std::less here
#include <map>

typedef boost::shared_ptr< Item > Item_p;

typedef boost::pool_allocator< std::pair< const int, Item_p > > pool_allocator;

typedef std::map< int, Item_p, std::less<int>, pool_allocator > Item_row;

typedef std::map< int, Item_row > Item_inv;


New thing here is using boost::pool_allocator, because the size of a pair of int & shared_ptr will be the same and there small objects then it should make things more efficent & faster than using the default allocator type for pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by rakoon2
Can you explain that? Please. Thank you! =)


as you know STL containers are class templates, normally you would just give the type being contained but there are other template type parameters that have default types so you don't normally need to explicitly write it out, all containers have an allocator type parameter, an allocator encapsulate memory de/allocation & de/initialization and they all have a default that uses the general c++ memory operators new/delete etc but you can give it custom allocator types to use different memory models/schemes etc, boost has a custom allocator that uses pooled object scheme this is better to use on on objects that are the same size & there small like pointers & smart pointer to particular type are small objects.

The reason for writing this:


typedef std::map< int, Item_p, std::less<int>, pool_allocator > Item_row;


was in the case of map the allocator type parameter was last in the list so i couldn't use the default for the comparator type which std::less is used for, std::less is a functor. More information is here.

Basically a map container is:

Quote:
Originally by SGI's STL docs
Map is a Sorted Associative Container that associates objects of type Key with objects of type Data. Map is a Pair Associative Container, meaning that its value type is pair<const Key, Data>. It is also a Unique Associative Container, meaning that no two elements have the same key.


the comparator type is used to keep the container sorted based on keys.

Share this post


Link to post
Share on other sites

#include "boost/multi_array.hpp"


typedef boost::shared_ptr<Item> Item_p;
typedef boost::multi_array<Item_p, 2> Item_Inv;
Item_Inv inventory(boost::extents[20][10]);

inventory[2][5] = new Item("sword");


Share this post


Link to post
Share on other sites
minor tweak ;-)


#include <boost\pool\pool_alloc.hpp>
#include <boost\smart_ptr.hpp>
#include <boost\multi_array.hpp>

typedef boost::shared_ptr<Item> Item_p;
typedef boost::pool_allocator< Item_p > item_allocator;

typedef boost::multi_array<Item_p, 2, item_allocator > Item_Inv;
Item_Inv inventory(boost::extents[20][10]);
inventory[2][5] = new Item("sword");


Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!