473,372 Members | 1,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to define a this particular type?

Hi,

How to define a two dimensional array where each row is of type
vector<map<string,int>>?

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>.

thanks
suresh
Dec 5 '07 #1
5 1819
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 5 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@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? 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, intKeyMapType;
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;
}

Dec 5 '07 #3
Jim Langston wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@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, intKeyMapType;
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?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 5 '07 #4
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@news.datemas.de...
Jim Langston wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@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, intKeyMapType;
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<KeyMapTypey[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'm thinking I must not quite understand what the OP is asking for.

Reguardless, with all this code he should be able to figure it out.
Dec 5 '07 #5
Jim Langston wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@news.datemas.de...
>Jim Langston wrote:
>>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fj**********@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, intKeyMapType;
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<KeyMapTypey[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
Dec 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bolin | last post by:
I am using a macro to define template functions that look the same except for a type. However when the return type it a template class with several template arguments (say, A<T1, T2>) then the...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
2
by: psundara | last post by:
Hi, I'm facing a peculiar problem of finding a way to interpret header information in a smart way. I have this header file that is shared by many users, which contains, among things, a few...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
10
by: O Plameras | last post by:
Are there differences in terms of functionality of, #define and typedef ? By the above I mean any instance when the outcome obtained by running two versions (1) and (2) below of C codes...
15
by: Ben Hinkle | last post by:
I'm curious, what was the rationale for making a builtin type _Bool but then having #define true 1 #define false 0 in stdbool.h? That seems very odd that true and false don't have type _Bool. In...
3
by: Zachary Pincus | last post by:
Hi folks, I'm sure this has come up before, but the search terms I've been using are so non-specific that I can't get any traction on Google. Here's my question: I have written a subclass of...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.