| | |
iterate a 3d vector
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 167
Reputation:
Solved Threads: 1
I have declared a vector with 3 Dimensions with the first code below.
Then I have managed to push_back all the 3 dimensions so my vector is filled up like this:
vector1[2][2][2]
What I now need to do is to iterate from vector1[0][0][0] until vector1[2][2][2]
with the code below.
But I get a huge of compileerrors. I think The code below can work if you havn´t declared the vector with typedef but I am not sure really.
I wonder if I am near a solution for this ? It is difficult to find examples for 3D vectors.
Then I have managed to push_back all the 3 dimensions so my vector is filled up like this:
vector1[2][2][2]
C++ Syntax (Toggle Plain Text)
typedef std::vector<string> String1D; typedef std::vector<String1D> String2D; typedef std::vector<String2D> String3D; String3D vector1;
What I now need to do is to iterate from vector1[0][0][0] until vector1[2][2][2]
with the code below.
But I get a huge of compileerrors. I think The code below can work if you havn´t declared the vector with typedef but I am not sure really.
I wonder if I am near a solution for this ? It is difficult to find examples for 3D vectors.
C++ Syntax (Toggle Plain Text)
std::vector< std::vector<string> >::iterator OneDStart = vector1.begin(); std::vector< std::vector<string> >::iterator OneDEnd = vector1.end(); for ( ; OneDStart != OneDEnd; OneDStart++ ) { //1D std::vector<string>::iterator TwoDStart = OneDStart->begin(); std::vector<string>::iterator TwoDEnd = OneDStart->end(); for ( ; TwoDStart != TwoDEnd; TwoDStart++ ) { //2D std::vector<string>::iterator ThreeDStart = TwoDStart->begin(); std::vector<string>::iterator ThreeDEnd = TwoDEnd->end(); for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ ) { //3D } //1D } //2D } //3D
Last edited by Lukezzz; Jul 31st, 2008 at 10:56 am.
This works for me
C++ Syntax (Toggle Plain Text)
#include <vector> #include <string> using namespace std; typedef std::vector<string> String1D; typedef std::vector<String1D> String2D; typedef std::vector<String2D> String3D; int main() { String3D vector1; String3D::iterator OneDStart = vector1.begin(); String3D::iterator OneDEnd = vector1.end(); for ( ; OneDStart != OneDEnd; OneDStart++ ) { //1D String2D::iterator TwoDStart = OneDStart->begin(); String2D::iterator TwoDEnd = OneDStart->end(); for ( ; TwoDStart != TwoDEnd; TwoDStart++ ) { //2D String1D::iterator ThreeDStart = TwoDStart->begin(); String1D::iterator ThreeDEnd = TwoDEnd->end(); for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ ) { //3D } //1D } //2D } //3D }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 167
Reputation:
Solved Threads: 1
I tried to do this and this works but I dont understand the logic. For me it should be 1D first and 3D last but I begin to iterate the 3:rd dimension at top of code from what I understand.
Shouldn´t I iterate the 1:st dimension first or does this happening but I cant see it ?
It iterates from vector1[0][0][0] until vector[2][2][2]. It is correct !
Shouldn´t I iterate the 1:st dimension first or does this happening but I cant see it ?
It iterates from vector1[0][0][0] until vector[2][2][2]. It is correct !
C++ Syntax (Toggle Plain Text)
for (String3D::iterator i = vector1.begin(); i != vector1.end(); ++i) { for (String2D::iterator j = i->begin(); j != i->end(); ++j) { for (String1D::iterator k = j->begin(); k != j->end(); ++k) { std::string GetText = *k; } } }
If you want a real 3D array C++ surrogate then try indicies, not iterators:
cpp Syntax (Toggle Plain Text)
// int base for simplicity typedef std::vector<int> int1D; typedef std::vector<int1D> int2D; typedef std::vector<int2D> int3D; int main() { int dim = 3; int3D v; // initiate the cube so v[i][j][k] == ijk... v.resize(dim); for (int i = 0; i < (int)v.size(); ++i) { v[i].resize(dim); for (int j = 0; j < (int)v[i].size(); ++j) { v[i][j].resize(dim); for (int k = 0; k < (int)v[i][j].size(); ++k) v[i][j][k] = (i+1)*100+(j+1)*10+(k+1); } } // print plane by plane then modify cube elements for (int i = 0; i < (int)v.size(); ++i) { cout << i << ":\n"; // i-th plane for (int j = 0; j < (int)v.size(); ++j) { for (int k = 0; k < (int)v.size(); ++k) { cout << '\t' << v[i][j][k]; v[i][j][k] = 1; // why? See below } cout << "\n"; } } cout << endl; // calculate sum of elements i*j*k*1 then go to Fortran... int sum = 0; for (size_t i = 0; i < v.size(); ++i) for (size_t j = 0; j < v[i].size(); ++j) for (size_t k = 0; k < v[i][j].size(); ++k) sum += v[i][j][k]; // must be 3*3*3 == 27... cout << "Sum(v): " << sum << endl; return 0; }
![]() |
Similar Threads
- Why does my code not take my input? (C++)
- Setting an array and reading in a txt file backwards (C++)
- how can i use an iterator for a 2d vector? (C++)
- vector<string> - way to find longest string? (C++)
- How do I work around this? - virtual templates (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ queries
- Next Thread: Passing infile values??
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






