By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

pointer to 2d array

P: n/a
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.

Dec 28 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);


# include <iostream>

int main()
{
int map[16][16] = {0};
map[5][5] = 3;
std::cout << map[5][5] << "\n";
}
Jonathan

Dec 28 '05 #2

P: n/a

Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.


You can use vector to simulate this dynamically.
#include <iostream>
#include <vector>
using namspace std;
.....

vector<vector<int> > MyMap(16, vector<int>(16));
MyMap[5][5] = 3;
cout << MyMap[5][5] << endl;

Dec 28 '05 #3

P: n/a
Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.


Personally, I would

vector< vector<int> > map;
map.resize( 16 );
for ( int i = 0; i < 16; ++i )
map[i].resize( 16 );
map[5][5] = 3;
cout << map[5][5] << endl;

But that might not be "most simply" since I'm using STL.

-JJ
Dec 28 '05 #4

P: n/a
Jonathan Mcdougall, the idea was so that i could reassign map to
another matrix of a different size. perhaps i should have specified
that

thanks Axter. i'll try that.

Dec 28 '05 #5

P: n/a
Axter wrote:
Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.


You can use vector to simulate this dynamically.
#include <iostream>
#include <vector>
using namspace std;
....

vector<vector<int> > MyMap(16, vector<int>(16));


Nice way to initialize! Amazing how you can program for years and not
notice something obvious like this. Thanks!

-JJ
Dec 28 '05 #6

P: n/a

James Juno wrote:
Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.


Personally, I would

vector< vector<int> > map;
map.resize( 16 );
for ( int i = 0; i < 16; ++i )
map[i].resize( 16 );
map[5][5] = 3;
cout << map[5][5] << endl;

But that might not be "most simply" since I'm using STL.


You can resize after the vector has been initialize by using following
method:
map.resize(16, vector<int>(16));

Also, to the original questioner, I would advise avoiding naming
variables that have the same name as objects in the STL library. map
is one of the containers in the C++ library.
Using the same name can be confusing to any one maintaining the code.
Also check out methods in the following links:
http://code.axter.com/dynamic_2d_array.h
http://www.codeguru.com/forum/showthread.php?t=231046
http://www.codeguru.com/forum/showth...hreadid=297838

C-Style compatible method:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c

Dec 28 '05 #7

P: n/a
i actually prefer your way James, at least for this scenario, since I
don't know what size I'll be initializing it to anyway.

Now quick question,

void map_resize(int x, int y)
{
map.resize(y);
for(int i=0; i<y; i++)
map[i].resize(x);
}

have I got my x and y correct, or should they be switched? It's hard
to visualize.

I tried running

map_resize(10,20);
map[15][5] = 5;
printf("%i", map[15][5]);

which works, but 15 > 10 should be out of bounds, if I wrote my
function correctly. however it is a container class and may be
resizing my map...

Dec 28 '05 #8

P: n/a
Mark wrote:
Jonathan Mcdougall, the idea was so that i could reassign map to
another matrix of a different size. perhaps i should have specified
that


Please 1) answer to the message you are answering to.. and 2) quote it.

You can assign arrays by hand:

void f(int source[10], int target[10])
{
for (int i=0; i<10; ++i)
target[i] = source[i];
}

But arrays are not value-types, meaning they cannot be assigned
directly:

target = source; // invalid;

You should use standard containers in this case, such as std::vector.
You've already been proposed good solutions.

You could also define a class yourself with the corresponding
operators. Search for matrix classes on google for some free
implementations.

Jonathan

Dec 28 '05 #9

P: n/a
Mark wrote:
i actually prefer your way James, at least for this scenario, since I
don't know what size I'll be initializing it to anyway.

Now quick question,

void map_resize(int x, int y)
{
map.resize(y);
for(int i=0; i<y; i++)
map[i].resize(x);
}

have I got my x and y correct, or should they be switched? It's hard
to visualize.

I tried running

map_resize(10,20);
map[15][5] = 5;
printf("%i", map[15][5]);

which works, but 15 > 10 should be out of bounds, if I wrote my
function correctly. however it is a container class and may be
resizing my map...


I like to visualize it this way: In your function, the first resize
creates y columns of empty vectors. The loop then goes through the
columns and sets the length of the vectors to x rows. So, map[15][5] is
the 15th column (zero-based), 5th row, which is fine by the way you've
written your function. If you try to address a non-existent element
(for instance, map[5][15]), you'll have a problem. It won't resize
itself automatically.

-JJ
Dec 28 '05 #10

P: n/a

James Juno wrote in message ...
Mark wrote:
put most simply, what is the c++ equivalent to this java code?

int map[][];
map = new int[16][16];
map[5][5] = 3;
System.out.println(map[5][5]);

thanks in advance.


Personally, I would

vector< vector<int> > map;
map.resize( 16 );
for ( int i = 0; i < 16; ++i )
map[i].resize( 16 );
map[5][5] = 3;
cout << map[5][5] << endl;

But that might not be "most simply" since I'm using STL.
-JJ


Try this :

// int main(){
std::vector<std::vector<int> > Vvmap(16, 16);
std::cout << Vvmap.size()<<std::endl;
std::cout << Vvmap.at(0).size()<<std::endl;
Vvmap.at(5).at(5) = 3;
Vvmap.at(5).push_back(5);
std::cout <<Vvmap.at(5).at(5)<<std::endl;
for( size_t i(0); i < Vvmap.size(); ++i ){
std::cout<<Vvmap.at( i ).size()<<std::endl;;
}
// return 0;
// } // main() end
--
Bob R
POVrookie
Dec 29 '05 #11

P: n/a

James Juno wrote in message ...
[snip]
I like to visualize it this way: In your function, the first resize
creates y columns of empty vectors. The loop then goes through the
columns and sets the length of the vectors to x rows.
** So, map[15][5] is the 15th column (zero-based), 5th row, **
No. It's the 16th col, 6th row. <G>
which is fine by the way you've
written your function. If you try to address a non-existent element
(for instance, map[5][15]), you'll have a problem. It won't resize
itself automatically.


So, use the at() to access. That way you'll get an out_of_range exception
(which you can 'catch'):
map.at(5).at(15) = 7;

/ -----------------
size_t rows(7);
size_t cols(7);
std::vector<std::vector<int> > Vvmap( rows, cols);
try{
Vvmap.at(6).at(26) = 32;
}
catch(std::out_of_range &Oor){
std::cout<<"caught "<<Oor.what()<<std::endl;
}
catch(...){
std::cout<<"caught something"<<std::endl;
}
// output: caught vector [] access out of range
/ -----------------

Only use the [] operator if you are *positive* the index is in range(...and
actually need the *tiny* speed increase). Like in this for loop:
for(size_t i(0); i < map.size(); ++i){
map[ i ].push_back( i );
}

[corrections always welcome ]
--
Bob R
POVrookie
Dec 29 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.