12,403,983 members (69,587 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 9:19am
Updated 10-Sep-12 9: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 > > 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 313 Richard MacCutchan 247 Karthik Bangalore 210 Suvendu Shekhar Giri 185 ppolymorphe 145
 OriginalGriff 7,386 ppolymorphe 3,175 Karthik Bangalore 3,127 Richard MacCutchan 2,372 F-ES Sitecore 2,262

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 10 Sep 2012