delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/08/12:41:44

Message-ID: <19991008135627.27300.rocketmail@web117.yahoomail.com>
Date: Fri, 8 Oct 1999 06:56:27 -0700 (PDT)
From: Kenn Hamm <vincevalentine AT yahoo DOT com>
Subject: Re: vector of vectors
To: djgpp AT Delorie DOT com
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

>Yes, but not quite the problem I'm trying to
>solve. I understand about checked or unchecked
>indexing, but how do I pre-allocate for a
>particular size of matrix?
>
>vector<int> a(10); allocates a one-d vector of
>ten elements. I want to do something like
>
>vector< vector<int>(10) > a(10); to allocate a
>matrix with ten rows and ten columns, instead of
>doing
>
>int a[10][10];
>
>I'm not sure this is possible with the STL.
>Perhaps I need to write a two-d matrix class?
>

The way I would do this is

vector<vector<int> > a(10);
for_each(a.begin(), a.end(), 
  bind2nd(mem_fun1(&vector::resize), 10));

Make sure to #include <algorithm> and <functional>
as well as <vector>.  Not the most elegant solution
in the world, but still better than having to write
a for loop every time you want to do it, and of
course you could write your own function to do what
the second line of code does for you.

Hope it helps.

Kenn

=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019