Board index » cppbuilder » Multi-dimensional vectors?

Multi-dimensional vectors?


2005-08-26 04:41:21 AM
cppbuilder61
Hi everyone, can anyone help me out with multi-dimensional vectors, I
tried the following but no joy, I get an error about a closing bracket,
I can't see the problem, however I have never tried this before?
std::vector < std::vector <AnsiString>>file1(100, 3);
With regards multi-dimensional vectors do I resize as I would with
normal 1D vector, or how do I resize the rows with a 100 for example and
the column with 3? I tried to find stuff on the net but I couldn't find
much that helps or provides some kind of useful explanation. Any help is
much appreciated! Cheers,
Rory.
 
 

Re:Multi-dimensional vectors?

Rory Walsh < XXXX@XXXXX.COM >wrote:
Quote

std::vector < std::vector <AnsiString>>file1(100, 3);
For what you're doing, a DynamicArray is probably a better
choice:
DynamicArray< DynamicArray < AnsiString>>My2DArray;
// allocate 100 'rows'
My2DArray.Length = 100;
// allocate 3 'cols' for each 'row'
// note that each row may be variable size of 'cols'
for( int x = 0; x < My2DArray.Length; ++x )
{
My2DArray[x].Length = 3;
}
~ JD
 

Re:Multi-dimensional vectors?

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
For what you're doing, a DynamicArray is probably a better
choice:
Why would you say that? DynamicArray esentially does the same thing that
std::vector does. The only difference being that std::vector is STL and
DynamicArray is VCL.
Gambit
 

{smallsort}

Re:Multi-dimensional vectors?

"Rory Walsh" < XXXX@XXXXX.COM >wrote in message
Quote
I tried the following but no joy, I get an error about a closing bracket
Please always quote the exact error messages, and show the actual lines that
the error messages refer to.
Quote
how do I resize the rows with a 100 for example and the
column with 3?
std::vector < std::vector <AnsiString>>file1(100, 3);
file1.resize(101); // <-- rows
for(sizt_t i = 0; i < 101; ++i)
file1[i].resize(5); // <-- columns
Gambit
 

Re:Multi-dimensional vectors?

The error I get is this
[C++ Error] Unit1.h(55): E2293 ) expected
which is generated from the following line
std::vector < std::vector <AnsiString>>file1(100, 3);
When I take this line out it compiles fine?
Rory.
Remy Lebeau (TeamB) wrote:
Quote
"Rory Walsh" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...


>I tried the following but no joy, I get an error about a closing bracket


Please always quote the exact error messages, and show the actual lines that
the error messages refer to.


>how do I resize the rows with a 100 for example and the
>column with 3?


std::vector < std::vector <AnsiString>>file1(100, 3);

file1.resize(101); // <-- rows
for(sizt_t i = 0; i < 101; ++i)
file1[i].resize(5); // <-- columns


Gambit


 

Re:Multi-dimensional vectors?

"Rory Walsh" < XXXX@XXXXX.COM >wrote in message
Quote
The error I get is this

[C++ Error] Unit1.h(55): E2293 ) expected

which is generated from the following line

std::vector < std::vector <AnsiString>>file1(100, 3);
Do this instead:
std::vector < std::vector <AnsiString>>file1(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);
Or:
std::vector < std::vector <AnsiString>>file1;
file1.resize(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);
Which you could then wrap into a function:
typedef std::vector<AnsiString>StringVector1D;
typedef std::vector<StringVector1D>StringVector2D;
void ResizeArray(StringVector2D &arr, size_t Rows, size_t Cols)
{
arr.resize(Rows);
for(size_t i = 0; i < Rows; ++i)
arr[i].resize(Cols);
}
StringVector2D file1;
ResizeArray(file1, 100, 3);
//...
ResizeArray(file1, 101, 5);
//...
You cannot pass both of the initial array sizes to the constructor of a
multi-dimensional vector, unless you do it like the following, which may
have too much overhead for your tastes:
std::vector< std::vector< AnsiString>>file1(100,
std::vector<AnsiString>(3));
Or:
typedef std::vector<AnsiString>StringVector1D;
typedef std::vector<StringVector1D>StringVecor2D;
StringVecor2D file1(100, StringVector1D(3));
Gambit
 

Re:Multi-dimensional vectors?

Cheers, I'll give it a go, thanks for the help.
Rory.
Remy Lebeau (TeamB) wrote:
Quote
"Rory Walsh" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>The error I get is this
>
>[C++ Error] Unit1.h(55): E2293 ) expected
>
>which is generated from the following line
>
>std::vector < std::vector <AnsiString>>file1(100, 3);


Do this instead:

std::vector < std::vector <AnsiString>>file1(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);

Or:

std::vector < std::vector <AnsiString>>file1;
file1.resize(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);

 

Re:Multi-dimensional vectors?

Quote
std::vector < std::vector <AnsiString>>file1(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);
This give me the same error as before in the same place, i.e.
std::vector < std::vector <AnsiString>>file1(100);
and
Quote
std::vector < std::vector <AnsiString>>file1;
file1.resize(100);
for(size_t i = 0; i < 100; ++i)
file1[i].resize(3);
gives me this error;
[C++ Error] Unit1.h(53): E2303 Type name expected
from this line
file1.resize(100);
I have included #include <vector>in my header, that's the correct file
right?
 

Re:Multi-dimensional vectors?

"Rory Walsh" < XXXX@XXXXX.COM >wrote in message
Quote
This give me the same error as before in the same place, i.e.

std::vector < std::vector <AnsiString>>file1(100);
Stop putting so many spaces surrounding the brackets. At the very least,
have the '<' brackets touching the class type, ie:
std::vector< std::vector< AnsiString>>file1(100);
Gambit
 

Re:Multi-dimensional vectors?

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"JD" < XXXX@XXXXX.COM >wrote in message
news:430e30e9$ XXXX@XXXXX.COM ...

>For what you're doing, a DynamicArray is probably a better
>choice:

Why would you say that?
Because it's a VCL solution. No extra includes and the syntax
is intuitive - which is what the OP was having trouble with in
the first place.
~ JD
 

Re:Multi-dimensional vectors?

Quote
Stop putting so many spaces surrounding the brackets. At the very least,
have the '<' brackets touching the class type, ie:

std::vector< std::vector< AnsiString>>file1(100);

Still results in the following error I'm afraid,
[C++ Error] Unit1.h(52): E2293 ) expected
 

Re:Multi-dimensional vectors?

Ok, there is something which I haven't mentioned yet which I didn't
think made any difference but obviously does, I want my 2D vector array
to be a public member of class TForm1. When I declare it in there with
the code you have all provided me with it results in errors. When I put
the code into the class constructor or into a member function it works
only I can't access it from other member functions without having to
pass it each time as an extra parameter. Can anyone explain why this is?
Rory.
P.S. The same thing is true of dynamic arrays when I tried them.
Rory Walsh wrote:
Quote
>Stop putting so many spaces surrounding the brackets. At the very least,
>have the '<' brackets touching the class type, ie:
>
>std::vector< std::vector< AnsiString>>file1(100);
>

Still results in the following error I'm afraid,

[C++ Error] Unit1.h(52): E2293 ) expected
 

Re:Multi-dimensional vectors?

Rory Walsh wrote:
Quote
Ok, there is something which I haven't mentioned yet which I didn't
think made any difference but obviously does, I want my 2D vector
array to be a public member of class TForm1. When I declare it in
there with the code you have all provided me with it results in
errors. When I put the code into the class constructor or into a
member function it works only I can't access it from other member
functions without having to pass it each time as an extra parameter.
Can anyone explain why this is?
It would have helped if you'd explained that earlier; you've sent a
few people on wild goose chases. Explanation and fix is below.
In future, when you pose problems with code, it is a good idea to work
to create a SMALL but COMPLETE example which illustrates your problem.
The process of doing so may help you find the issue. And sets the
scene for people being able to help you effectively.
Quote

Rory.

P.S. The same thing is true of dynamic arrays when I tried them.

Rory Walsh wrote:
>>Stop putting so many spaces surrounding the brackets. At the very
least,>>have the '<' brackets touching the class type, ie:
>>
>>std::vector< std::vector< AnsiString>>file1(100);
>>
>
>Still results in the following error I'm afraid,
>
>[C++ Error] Unit1.h(52): E2293 ) expected
Within the class declaration, use
#include <vector>
class Whatever
{
public:
std::vector< std::vector< AnsiString>>file1;
};
and in an appropriate constructor for class Whatever do
file1.resize(100);
for (size_t i = 0; i < 100; ++i)
file1[i].resize(3);
The reason is that you can't generally initialise class members within
the class declaration.
 

Re:Multi-dimensional vectors?

Quote
It would have helped if you'd explained that earlier; you've sent a
few people on wild goose chases. Explanation and fix is below.
Sorry about that, I wasn't thinking of the bigger picture.
Quote
In future, when you pose problems with code, it is a good idea to work
to create a SMALL but COMPLETE example which illustrates your problem.
The process of doing so may help you find the issue. And sets the
scene for people being able to help you effectively.
Will do. Thanks again for the help,
Rory.
Quote

>Rory.
 

Re:Multi-dimensional vectors?

One last thing, can I now fill my array as I would with a normal 2d
array, i.e. array[1][2] = "test"; ?
I just tried it but got an access error, although I imagine it is being
caused by something else I have done wrong! Thanks again for sorting out
the other issue.
Rory.
Rory Walsh wrote:
Quote
>It would have helped if you'd explained that earlier; you've sent a
>few people on wild goose chases. Explanation and fix is below.


Sorry about that, I wasn't thinking of the bigger picture.

>In future, when you pose problems with code, it is a good idea to work
>to create a SMALL but COMPLETE example which illustrates your problem.
>The process of doing so may help you find the issue. And sets the
>scene for people being able to help you effectively.


Will do. Thanks again for the help,
Rory.

>
>>Rory.