I have declared a vector with 3 Dimensions with the first code below.
Then I have managed to push_back all the 3 dimensions so my vector is filled up like this:

vector1[2][2][2]

typedef std::vector<string> String1D;
typedef std::vector<String1D> String2D;
typedef std::vector<String2D> String3D;

String3D vector1;

What I now need to do is to iterate from vector1[0][0][0] until vector1[2][2][2]
with the code below.
But I get a huge of compileerrors. I think The code below can work if you havn´t declared the vector with typedef but I am not sure really.
I wonder if I am near a solution for this ? It is difficult to find examples for 3D vectors.

std::vector< std::vector<string> >::iterator OneDStart = vector1.begin();
std::vector< std::vector<string> >::iterator OneDEnd = vector1.end();
  
for ( ; OneDStart != OneDEnd; OneDStart++ ) 
{  //1D
  std::vector<string>::iterator TwoDStart = OneDStart->begin();
  std::vector<string>::iterator TwoDEnd = OneDStart->end();
  
  for ( ; TwoDStart != TwoDEnd; TwoDStart++ )
  {   //2D
      std::vector<string>::iterator ThreeDStart = TwoDStart->begin();
      std::vector<string>::iterator ThreeDEnd = TwoDEnd->end();
      
      for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ )
      {   //3D


} //1D
} //2D
} //3D

This works for me

#include <vector>
#include <string>
using namespace std;


typedef std::vector<string> String1D;
typedef std::vector<String1D> String2D;
typedef std::vector<String2D> String3D;

int main()
{
    String3D vector1;	
    String3D::iterator OneDStart = vector1.begin();
    String3D::iterator OneDEnd = vector1.end();
  
    for ( ; OneDStart != OneDEnd; OneDStart++ ) 
    {  //1D
        String2D::iterator TwoDStart = OneDStart->begin();
        String2D::iterator TwoDEnd = OneDStart->end();
        for ( ; TwoDStart != TwoDEnd; TwoDStart++ )
        {   //2D
            String1D::iterator ThreeDStart = TwoDStart->begin();
            String1D::iterator ThreeDEnd = TwoDEnd->end();
            for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ )
            {   //3D


            } //1D
        } //2D
    } //3D
}

That was fantastico, I saw the adjustments you did to the code and it compiles fine.
When I run the code I am getting an Error message that says:

"Expression: vector iterator not dereferencable"

I dont know what could be ment by this ?

I tried to do this and this works but I dont understand the logic. For me it should be 1D first and 3D last but I begin to iterate the 3:rd dimension at top of code from what I understand.
Shouldn´t I iterate the 1:st dimension first or does this happening but I cant see it ?
It iterates from vector1[0][0][0] until vector[2][2][2]. It is correct !

for (String3D::iterator i = vector1.begin(); i != vector1.end(); ++i)
{
     for (String2D::iterator j = i->begin(); j != i->end(); ++j)
   {
	for (String1D::iterator k = j->begin(); k != j->end(); ++k)
	{
		std::string GetText = *k;
	}
   }
}

If you want a real 3D array C++ surrogate then try indicies, not iterators:

// int base for simplicity
typedef std::vector<int> int1D;
typedef std::vector<int1D> int2D;
typedef std::vector<int2D> int3D;

int main()
{
    int dim = 3;
    int3D v;
    // initiate the cube so v[i][j][k] == ijk...
    v.resize(dim);
    for (int i = 0; i < (int)v.size(); ++i)
    {
        v[i].resize(dim);
        for (int j = 0; j < (int)v[i].size(); ++j)
        {
            v[i][j].resize(dim);
            for (int k = 0; k < (int)v[i][j].size(); ++k)
                v[i][j][k] = (i+1)*100+(j+1)*10+(k+1);
        }
    }

    // print plane by plane then modify cube elements
    for (int i = 0; i < (int)v.size(); ++i)
    {
        cout << i << ":\n"; // i-th plane
        for (int j = 0; j < (int)v.size(); ++j)
        {
            for (int k = 0; k < (int)v.size(); ++k)
            {
                cout << '\t' << v[i][j][k];
                v[i][j][k] = 1; // why? See below
            }
            cout << "\n";	
        }
    }
    cout << endl;

    // calculate sum of elements i*j*k*1 then go to Fortran...
    int sum = 0;
    for (size_t i = 0; i < v.size(); ++i)
    for (size_t j = 0; j < v[i].size(); ++j)
    for (size_t k = 0; k < v[i][j].size(); ++k)
        sum += v[i][j][k];
    // must be 3*3*3 == 27...
    cout << "Sum(v): " << sum << endl;

    return 0;
}

Apropos, dont tread on typedefs: they never create new types, they are type aliases only...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.