Re: How to define a this particular type?
Jim Langston wrote:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:fj6tpa$bkb$1@news.datemas.de...
Jim Langston wrote:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:fj6lhd$c9r$1@news.datemas.de...
suresh wrote:
How to define a two dimensional array where each row is of type
vector<map<string,int>>?
There is no way. You cannot define a two-dimensional array where
rows (one-dimensional arrays, the first-level elements) are objects
of some other type. It's just plain nonsense.
You can however, define a _one-dimensional_ array of those vectors:
vector<map<string,int> > myOneDimArray[12345];
My idea is, if "x" is such a variable, x[0] is a vector where each
cell of the vector is a map<string,int>.
Similarly x[1] is a vector where each cell of it is a
map<string,int>.
Right. It's not a two-dimensional array, though.
Why do you say there is no way?
I guess the rest of my paragraph that begins with "There is no way"
does not look like an explanation to you. Do you find it hard to
comprehend or is my meaning unclear or did you just forget to read
it altogether?...
Although it's very easy to confuse
oneself accessing an element, what is wrong with this code?
#include <iostream>
#include <vector>
#include <string>
#include <map>
typedef std::map<std::string, int> KeyMapType;
int main()
{
std::vector<std::vector<KeyMapType > > x;
x.push_back( std::vector<KeyMapType >() );
x.push_back( std::vector<KeyMapType >() );
x.push_back( std::vector<KeyMapType >() );
x[0].push_back( KeyMapType() );
x[0].push_back( KeyMapType() );
x[0].push_back( KeyMapType() );
x[0][0]["0 1"] = 11;
x[0][0]["0 2"] = 12;
}
Nothing wrong with the code. Where is the two-dimensional array,
though? A vector of vectors is NOT a two-dimensional array since
I has more freedom in how the elements are stored and how many
there are elements in each "row".
A two-dimensional array would be an array of arrays of some type,
with each dimension fixed at the time of creating the array (for
automatic and static arrays). You can create a dynamic two-
dimensional array, it is possible to do with a typedef (but only
the top-most dimension is variable, the others have to be fixed,
still).
Am I being incomprehensible again?
Okay, then what is wrong with
std::vector<KeyMapType> y[10][10];
y[0][0].push_back( KeyMapType() );
y[0][0][0]["0 0"] = 00;
If you want a fixed size array.
Doesn't that fit the OP's original requirements?
I may have misunderstood, but didn't the OP say "every row is of type
vector"? How can a row be of type vector? What you have here is
a two-dimensional array where every *element* is of type vector<>.
Now, a row in your array is an array itself. y[0] is a row. y[1]
is, as well. So are y[2], y[3], etc. They don't have the type
'vector', they have the type 'array of' something. That's how it is
in a two-dimensional array.
Either the OP doesn't understand what a two-dimensional array is, or
the OP is using wrong terminology, or both.
I'm thinking I must not quite understand what the OP is asking for.
Your approximation is different from mine, but I believe you reached
the same conclusion -- the OP isn't sure what he needs.
Reguardless, with all this code he should be able to figure it out.
Yes, hopefully.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask