Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:
C++
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
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

Possibly you mean something similar to
C++
#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;   
}


?
 
Share this answer
 
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.
C++
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:
C++
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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900