Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to declare the size, and fill vector<vector <T>> ?

Reply
Thread Tools

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

 
 
eb
Guest
Posts: n/a
 
      09-13-2006
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).


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-13-2006
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


 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      09-13-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
redirect and automatically fill out form and submit John Devlon ASP .Net 1 09-24-2009 11:56 AM
When you declare an array of chars and store a string in it, where isthe position of the null character \0? And what happens to the unused memorylocations? Gary C Programming 5 05-07-2008 03:11 AM
how to declare a structure and a class interlinked mosfet C++ 1 12-15-2003 10:45 AM
declare and return struct in one line Matt Graham C++ 10 07-25-2003 09:52 PM



Advertisments