Runtime two dimesional array?
Nov 13, 2015 at 10:53pm UTC
So far I can make a run time one dimensional array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
using namespace std;
struct bob {
bob();
~bob();
int x;
int z;
void createMap {
}
};
bob::bob() {
cout << "constrcuting" ;
}
bob::~bob() {
cout << "destroying" ;
}
int main() {
bob k;
int *p;
cout << "enter in a value " ;
cin >> k.x >> k.z;
int x = 0;
int * myArray = new int [k.x];
delete [] myArray;
for (int i = 0; i < k.x; i++) {
cout << myArray[i];
x += sizeof (myArray[i]);
if (i == (k.x - 1)) {
cout << endl;
}
else
continue ;
}
cout << x << "\tBytes of memory" << endl;
}
Well I tried to make a two dimesional version and it gave me errors.
int * myArray = new int [k.x][k.z];
Then I tried to do this
int ** myArray = new int [k.x][k.z];
Soo yeah... Any ideas?
Nov 14, 2015 at 6:08am UTC
Any thoughts?
Nov 14, 2015 at 11:57am UTC
Strongly favour using
std::vector< std::vector<int> > for this.
https://cal-linux.com/tutorials/vectors.html
Use std::vector where you have a choice - and you have a choice in most contexts. However, arrays existed long before vectors and are roughly equivalent to what is offered in other languages (notably C), so you need to know arrays and know them well, to be able to cope with older code and with code written by people who don't appreciate the advantages of vector. - Stroustrup
http://www.cplusplus.com/forum/beginner/176462/2/#msg871761
If we must deal with raw arrays, something like this, perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
struct array_2d
{
std::size_t nrows = 0 ;
std::size_t ncols = 0 ;
int ** data = nullptr ;
};
array_2d create_array( std::size_t nrows, std::size_t ncols )
{
int * buffer = new int [ nrows * ncols ] {}; // hold data for the flattened two-dimensional array
try
{
int ** data = new int * [nrows] ; // this may throw
// point to the beginning of each row in buffer
for ( std::size_t row = 0 ; row < nrows ; ++row ) data[row] = buffer + row*ncols ;
return { nrows, ncols, data } ;
}
catch ( const std::bad_alloc& )
{
delete [] buffer ; // we do not want a leak, do we?
return {} ;
}
}
void destroy_array( array_2d a2d )
{
if ( a2d.data ) delete [] a2d.data[0] ;
delete [] a2d.data ;
}
void iota( array_2d a2d, int start, int incr )
{
for ( std::size_t row = 0 ; row < a2d.nrows ; ++row )
{
for ( std::size_t col = 0 ; col < a2d.ncols ; ++col )
{
a2d.data[row][col] = start ;
start += incr ;
}
}
}
std::ostream& print_array( const array_2d& a2d, std::ostream& stm = std::cout )
{
for ( std::size_t row = 0 ; row < a2d.nrows ; ++row )
{
for ( std::size_t col = 0 ; col < a2d.ncols ; ++col ) std::cout << a2d.data[row][col] << ' ' ;
std::cout << '\n' ;
}
return stm ;
}
int main()
{
auto a2d = create_array( 5, 8 ) ;
print_array(a2d) << "-------------------\n" ;
iota( a2d, 100, 7 ) ;
print_array(a2d) ;
destroy_array(a2d) ;
}
http://coliru.stacked-crooked.com/a/23374b8a4ab75461
Nov 14, 2015 at 12:00pm UTC
http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
Topic archived. No new replies allowed.