| | |
iterate a 3d vector
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 204
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 }
My NewYear's resolution is to lose weight.
Weight Lost since 1 Jan 2010: 8.7 lbs
Weight Lost since 1 Jan 2010: 8.7 lbs
•
•
Join Date: Mar 2008
Posts: 204
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??
Views: 2445 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






