473,386 Members | 1,715 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,386 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 1821
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.