12,952,398 members (40,690 online)
Rate this:
Please Sign up or sign in to vote.
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
Comments
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

2 solutions

Rate this:
Please Sign up or sign in to vote.

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;
}```

?
Comments
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:
Please Sign up or sign in to vote.

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
Comments
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.

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 285 RickZeeland 95 CPallini 90 AdvancedDNA 85 F-ES Sitecore 80
 OriginalGriff 6,289 CHill60 3,490 Maciej Los 3,103 Jochen Arndt 1,975 ppolymorphe 1,900

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100