12,759,971 members (30,738 online)
Rate this:
See more:
Hi,

I got some problem to set the values in 2d and 3d vector.
I want to know the process to create and return the 2dimensional and 3dimensional vector values where size will be changed dynamically.

I also tried the following code but i didn't get any help:

3D vector:
```vector < vector < vector<int> > > tube;

for(int i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<15;k++)
{
tube.push_back( vector< vector<int> >() );
tube[k].push_back (vector<int> ());
tube[i][j].push_back(value)
}
}
}```

but I don't know whether it will work and also don't know where should I set i,j,k parameter.

and I also want to do the same thing for 2d vector

Please let me know that what is the correct way to do this.

Thanks...
Posted 10-Sep-12 10:19am
Updated 10-Sep-12 10:20am
v2
Sergey Alexandrovich Kryukov 10-Sep-12 15:32pm

Not clear what's the problem? To try to run your code and see what happens? What is that supposed to mean "where should I set i, j, k parameter"? You set them where you want to access the element by index...
--SA

Rate this:

## Solution 2

Possibly you mean something similar to
```#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

int main ()
{
vector < vector < vector<int> > > tube;
for(int i = 0; i < 2; i++)
{
vector < vector < int > > w;
tube.push_back( w );
for(int j = 0; j < 4; j++)
{
vector <int> v;
tube[i].push_back( v );
for(int k = 0; k < 15; k++)
{
tube[i][j].push_back( rand());
}
}
}

for (size_t i = 0; i < tube.size(); i++)
for (size_t j = 0; j < tube[i].size(); j++)
for (size_t k = 0; k < tube[i][j].size(); k++)
cout << "tube[" << i << "][" << j << "][" << k << "] = " << tube[i][j][k] << endl;
}
```

?
devender_t 15-Apr-16 15:37pm

Wow!!! your solution solved my issue which was not solved on Stackoverflow also.
Thank You. I truly appreciate it.
CPallini 16-Apr-16 12:14pm

Almost 4 years later? :-)
You are welcome.
Rate this:

## Solution 1

```vector < vector < vector<int> > > tube;
for(int i=0;i<2;i++)
{
tube.push_back(vector<vector<int> >());
for(int j=0;j<4;j++)
{
tube[i].push_back(vector<int>());
for(int k=0;k<15;k++)
{
tube[i][j].push_back(value);
}
}
}
```

OR more efficiently:
```vector < vector < vector<int> > > tube;
tube.resize(2);
for(int i=0;i<2;i++)
{
tube[i].resize(4);
for(int j=0;j<4;j++)
{
tube[i][j].resize(15, value);
}
}
```

However if you have to use and resize 3D vectors often then its profitable to write a specialized class for the job but that's a bit more difficult than what we are discussing here.
v2
saqib.akhter 10-Sep-12 15:43pm

It looks good.....
but I have to implement like this:
vector < vector < vector<int> > > tube;
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<15;k++)
{
SetTubeValue(value,range,cycle,seq); //i=range,j=cycle,k=seq
}
}
}
void MyClass::SetTubeValue(int value,int range,int cycle,int seq)
{
//then set the value to tube vector;
}

Please take a look for above scenario
pasztorpisti 10-Sep-12 16:39pm

And why would you set values in the middle of the loops? First create the whole array with default values and after that issue your SetTubeValue() calls.

Top Experts
Last 24hrsThis month
 OriginalGriff 230 Karthik Bangalore 60 Dave Kreskowiak 50 Bryian Tan 45 Peter Leow 40
 OriginalGriff 4,557 Peter Leow 3,289 ppolymorphe 2,713 Karthik Bangalore 2,634 Graeme_Grant 2,406