Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C++
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
Edited 10-Sep-12 10:20am
v2
Comments
Sergey Alexandrovich Kryukov at 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: bad
good
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;   
}
                                                            
 
?
  Permalink  
Rate this: bad
good
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.
  Permalink  
v2
Comments
saqib.akhter at 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 at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 Sep 2012
Copyright © CodeProject, 1999-2014
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