3 dimensional string array // fill, change, delete.

Markus Freitag 3,786 Reputation points


I have a 3 dimensional array that I need to fill at runtime.
I also need to read out a specific value and, if necessary, change a type value.
I do not know the size of the array in advance.
How can I achieve this?

std::vector<string>             vecstrNodeValue;

vecstrNodeValue = {

I found this, but I don't know the size. And I have a string array. Do you have an example for me?

// C++ Program to Store value entered by user in
// three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
    // This array can store upto 12 elements (2x3x2)
    int test[2][3][2] = {
                                {1, 2},
                                {3, 4},
                                {5, 6}
                                {7, 8}, 
                                {9, 10}, 
                                {11, 12}

    // Displaying the values with proper index.
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;

    return 0;
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,634 questions
{count} votes

Accepted answer
  1. Barry Schwarz 2,511 Reputation points

    Two common approaches to this issue are an array of pointers and a vector of vectors. One advantage with vectors is that initial allocation and run-time expansion of a dimension is handled under-the-covers for you. If the size of each dimension is fixed at run time, this wouldn't be an issue. In either case, typedef helps significantly.

    Using vectors, start with the type (not an object) vector of strings. Then create the type vector of the first type. And finally create the type vector of the second type. In code this looks like

    typedef vector<string> dimension_1;
    typedef vector<dimension_1> dimension_2;
    typedef vector<dimension_2> dimension_3;

    Once you have calculated the size of the dimensions (max_1, max_2, max_3), you can create and size the object with

    dimension_3 myarray(max_1);
    for (int i=0; i<max_1;i++)
       for (int j=0; j<max_2; j++)

    Now you can access any element of the 3D array with myarray[x][y][z].

    A similar approach works with arrays but yu have to perform all the allocations yourself.

    0 comments No comments

0 additional answers

Sort by: Most helpful