Sign in to follow this  

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

This topic is 4841 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
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
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
Heh :)

"
. While the number of dimensions must remain the same, the shape of the array may change so long as the total number of elements contained remains the same."

How can I add more dimensions then? ( if the inventory grows )

Thanks ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by rakoon2
How can I add more dimensions then? ( if the inventory grows )
That's not a new dimension, that's an increase in extent in a particular dimension. Your inventory is rectangular: that means it has two dimensions (width and height, or columns and rows). Adding another dimension would give it depth, making it a cuboid. Adding another dimension after that would make it a 4D figure, like a tesseract. And so on.

Since extents can change, you have no problems.

Share this post


Link to post
Share on other sites
Argh! :)


typedef boost::shared_ptr<Item> item_ptr;
typedef boost::multi_array<item_ptr, 2> array_type;
typedef array_type::extent_range range;

array_type::extent_gen extents;
array_type A( extents[10][1] );

boost::array<array_type::index,ndims> dims = {{}};
A.reshape(dims);



How can I find out the size? ia like std::vector v; v.size();
What about boost::multi_array? =3 =)

Share this post


Link to post
Share on other sites

This topic is 4841 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this