How to declare the size, and fill vector<vector <T>> ?

E

eb

I have this working code :

foo.h
/* nothing */

foo.cpp
...
std::vector<std::vector<T *> > my_T(board_size,board_size) ;

for (i=0; i<board_size; i++)

for (j=0; j<board_size; j++)
{
my_T[j] = new T ;
my_T[j]->do_something_with_my_T()
}
....



I'd like do do the same with :


foo.h
std::vector<std::vector<T *> > my_T


foo.cpp

???? -> pass the size to my_T and fill it with T


Any idea ?
google did not help me there ...


(sorry for the inappropriate vocabulary, I'm not a coder in real life).
 
V

Victor Bazarov

eb said:
I have this working code :

foo.h
/* nothing */

foo.cpp
...
std::vector<std::vector<T *> > my_T(board_size,board_size) ;

for (i=0; i<board_size; i++)

for (j=0; j<board_size; j++)
{
my_T[j] = new T ;
my_T[j]->do_something_with_my_T()
}
...



I'd like do do the same with :


foo.h
std::vector<std::vector<T *> > my_T


foo.cpp

???? -> pass the size to my_T and fill it with T


Any idea ?
google did not help me there ...


Look into 'std::generate'. You can write your own generator that would
allocate your T and do something with it before assigning it to the
element of the vector.

V
 
E

Earl Purple

eb said:
I have this working code :

foo.h
/* nothing */

foo.cpp
...
std::vector<std::vector<T *> > my_T(board_size,board_size) ;

You cannot initialise your vector this way. vector does have a
constructor that takes 2 parameters. The first is the size, the second
is the element to which each element is initialised. Now as far as I'm
aware this cosntructor is explicit so there will be no implicit
conversion from int (or whatever integral type your board_size is) to
vector< T * >.

Note that if the constructor is not explicit then it would indeed work.
for (i=0; i<board_size; i++)

for (j=0; j<board_size; j++)
{
my_T[j] = new T ;
my_T[j]->do_something_with_my_T()
}
...


vector< vector< X > > is not generally the best way to implement a
matrix. Also beware of having vectors of pointers. And where is this
initialised? If it's in the constructor of your class you may have
exception-safety issues as you'll need to clean up the pointers you've
already allocated if a later one fails. If it isn't in your
constructor, you'll have a partially initialised vector, i.e. it will
have some valid pointers but many NULLs and will probably be unusable
by most of your class functions that will not be expecting a
partially-initialised vector.
I'd like do do the same with :


foo.h
std::vector<std::vector<T *> > my_T


foo.cpp

???? -> pass the size to my_T and fill it with T


Any idea ?
google did not help me there ...

Have a look at the matrix example in the comp.lang.c++ FAQ (at
www.parashift.com) and also consider that you'll want shared_ptr< T >
as the member type.

I also have a sophisticated Matrix template based on the one in the FAQ
which would allow iteration thus you could use a generate() on it to
fill the values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top