GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2007, 16:04
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

three dimensional array


i used a three dimensional vector in my program.But while accesing it with [] operator ,i'm living problem.It gives no problem while compiling but when it runned gives a access violation problem.i think after two dimension the use of [] is outbounded.here's my vector:
vector<int> kod;
vector<vector<int> > lines;
vector<vector<vector<int> > > nums;
  #2  
Old 02-Mar-2007, 19:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: three dimensional array


Quote:
Originally Posted by tufan
i used a three dimensional vector in my program.But while accesing it with [] operator ,i'm living problem.It gives no problem while compiling but when it runned gives a access violation problem.i think after two dimension the use of [] is outbounded.here's my vector:
vector<int> kod;
vector<vector<int> > lines;
vector<vector<vector<int> > > nums;

Two things to remember (but they are really the same thing):

1. The overloaded [] operator does not do bounds checking.

2. You must have the correct sizes of vectors before you can reference the elements with the [] operator.


I'll create and initialize a 3-d vector. I do it in stages and I use typdefs to try to keep my sanity, such as it is.

CPP / C++ / C Code:
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> intVector;
typedef vector<intVector> int2dVector;
typedef vector<int2dVector> int3dVector;

int main()
{
    int xSiz = 2;
    int ySiz = 3;
    int zSiz = 4;

    intVector   kod(zSiz, 0);     // 1D: size=xSiz, initialized with zeros
    int2dVector lines(ySiz, kod); // 2D: size=ySiz, initialized with 1D vectors
    int3dVector nums(xSiz, lines);// 3D: size=zSiz, initialized with 2D vectors
    unsigned i, j, k;

    int x = 0;

    for (i = 0; i < nums.size(); i++) {
        for (j = 0; j < nums[i].size(); j++) {
            for (k = 0; k < nums[i][j].size(); k++) {
                nums[i][j][k] = x++;
            }
        }
    }

    for (i = 0; i < nums.size(); i++) {
        for (j = 0; j < nums[i].size(); j++) {
            for (k = 0; k < nums[i][j].size(); k++) {
                cout << "nums[" 
                     << i << "]["
                     << j << "][" 
                     << k << "]= "
                     << nums[i][j][k] << endl;
            }
        }
    }
    return 0;
}

Output:

Code:
nums[0][0][0]= 0 nums[0][0][1]= 1 nums[0][0][2]= 2 nums[0][0][3]= 3 nums[0][1][0]= 4 nums[0][1][1]= 5 nums[0][1][2]= 6 nums[0][1][3]= 7 nums[0][2][0]= 8 nums[0][2][1]= 9 nums[0][2][2]= 10 nums[0][2][3]= 11 nums[1][0][0]= 12 nums[1][0][1]= 13 nums[1][0][2]= 14 nums[1][0][3]= 15 nums[1][1][0]= 16 nums[1][1][1]= 17 nums[1][1][2]= 18 nums[1][1][3]= 19 nums[1][2][0]= 20 nums[1][2][1]= 21 nums[1][2][2]= 22 nums[1][2][3]= 23

Regards,

Dave
Last edited by davekw7x : 02-Mar-2007 at 20:26.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
Dynamic allocation of multi dimensional array pointer C Programming Language 7 14-May-2005 00:50
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 22:53
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:45.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.