I don't understand how to create and use dynamic arrays in C ++

So, I have a;

int grid_x = 5
int * grid;
grid = new int[grid_x];
*grid = 34;
cout << grid[0];

Should row 3 create an array with 5 elements? Or fill the first element with the number 5?

Line 4 fills in the first element, how can I fill in the rest?

Without line 4, line 5 reads "-842150451".

I donโ€™t understand what is happening, I am trying to create a 2-dimensional array using the x and y values โ€‹โ€‹set by the user, and then each element is filled with each element by the numerical values โ€‹โ€‹also set by the user. My code above was an attempt to try it with a 1-dimensional array.

+4
source share
6 answers

Should row 3 create an array with 5 elements?

. , .

5?

new int(grid_x), , , , .

new ( ). .

4 , ?

[] :

grid[0] = 34;  // Equivalent to: *(grid)   = 34
grid[1] = 42;  // Equivalent to: *(grid+1) = 42
// ...
grid[4] = 77;  // That the last one: 5 elements from 0 to 4.

, ; delete[] , , . . :

#include <vector>

std::vector<std::vector<int>> grid(grid_x, std::vector<int>(grid_y));
grid[x][y] = 42; // for any x is between 0 and grid_x-1, y between 0 and grid_y-1

; . - :

template <typename T>
class Grid {
public:
    Grid(size_t x, size_t y) : size_x(x), size_y(y), v(x*y) {}

    T       & operator()(size_t x, size_t y)       {return v[y*size_x + x];}
    T const & operator()(size_t x, size_t y) const {return v[y*size_x + x];}

private:
    size_t size_x, size_y;
    std::vector<T> v;
};

Grid grid(grid_x,grid_y);
grid(x,y) = 42;
+9

++- (ally resizable) int :

std::vector<int> grid;

, .

5 , :

std::vector<int> grid(5);

[]:

grid[0] = 34;
grid[1] = 42;

:

// grid.size() is 5
grid.push_back(-42);
// grid.size() now returns 6

, , std::vector.

+11

3 5 ? 5?

5 .

4 , ?

grid[n] = x;

n - , , x - .

+1

3 5 , ...

x[y] *(x+y), 4 grid[0] = 34;, ( grid[2] , 2[grid]! )

0
int grid_x = 5 
int * grid; 
grid = new int[grid_x];
*grid = 34; 
cout << grid[0];

Should line 3 create an array with 5 elements? Or fill the first
element with the number 5?

. "new"

4 , ?

[], :

for int (i=0; i < grid_x; i++) { //Reset the buffer
  grid[i] = 0;
}


4 5 "-842150451".

, .

, , 2 x y, , , . .

, . , boost::scoped_array, , .

, , - , scoped_array scoped_arrays. for.

using boost::scoped_array;
int grid_x;
int grid_y;
///Reading values from user...
scoped_array<scoped_array<int> > grid(new scoped_array<int> [grid_x]);
for (int i = 0; i < grid_x; i++)
  grid[i] = scoped_array<int>(new int[grid_y] );

grid[x][y];

: scoped_array ,

typedef int* p_int_t;
p_int_t* grid = new p_int_t [grid_x];
for (int i = 0; i < grid_x; i++)
  grid[i] = new int[grid_y];

, .

0

- . .

int * grid;

C- , * "". , . , 3

grid = new int [grid_x];

( ) grid. - , , . , , 4 .

, *? 4 " " , ,

*grid = 34;

, , grid, 34. 3 grid . , 4 34.

C , , 0, - - array , .

for(int index = 0; index < grid_x; index++)
{
    grid[index] = 34;
}

.

for(int* pointerToElement = grid; 0 < grid_x; grid_x-- )
{
    // save 34 to the address held by the pointer
    /// and post-increment the pointer to the next element.
    *pointerToElement++ = 34;
}

, , , , , .. ..

0

All Articles