| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"eb" |
| Date: |
13 Sep 2006 01:28:15 AM |
| Object: |
How to declare the size, and fill vector> ? |
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[i][j] = new T ;
my_T[i][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).
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: How to declare the size, and fill vector> ? |
13 Sep 2006 07:35:53 AM |
|
|
eb wrote:
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[i][j] = new T ;
my_T[i][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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
| User: "Earl Purple" |
|
| Title: Re: How to declare the size, and fill vector> ? |
13 Sep 2006 08:45:32 AM |
|
|
eb wrote:
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[i][j] = new T ;
my_T[i][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.
.
|
|
|
|

|
Related Articles |
|
|