Tripple vector
-
Wo ist das Problem? Du musst das Selbe nochmal eine Rekursionstiefe weiter machen.
Also für jedes Element deines 3D-Vector einen 2D-vector erstellen, wie du es ja offensichtlich schon kannst. Ist total unübersichtlich und ziemlicher Hirnfick wenn mans sich nicht aufmalt, aber von der grundsätzlichen Logik her kein Zauberstück.
Du solltest dir aber für die Lesbarkeit udn Wartbarkeit deines Codes überlegen ob du das nicht doch irgendwie anders lösen kannst oder durch mehr typedefs etc. übersichtlicher gestalten kannst.
-
Eigentlich genauso:
vector < vector < vector <int> > > codemap; codemap.resize(2); codemap[0].resize(2); codemap[1].resize(2); codemap[0][0].push_back(1); codemap[0][0].push_back(2); codemap[0][1].push_back(3); codemap[0][1].push_back(4); codemap[1][0].push_back(5); codemap[1][0].push_back(6); codemap[1][1].push_back(7); codemap[1][1].push_back(8); for (vector < vector < vector <int> > >::iterator x=codemap.begin(); x != codemap.end(); ++x) for (vector < vector <int> >::iterator y=x->begin(); y != x->end(); ++y) for (vector <int>::iterator z=y->begin(); z != y->end(); ++z) std::cout << *z << std::endl;Ungetestet, daher kann ich kleine Syntaxfehler übersehen haben.
-
Ok thanx,
I test this code
std::vector <std::vector < std::vector <int> > >testmap2; for(byte i(0); i < 10; i++) testmap2[i].resize(10);after that I insert a testvalue like this
testmap2[0][0].push_back(45);The app compiles without error but when I run the app it crashs ????
P.S.: It crash during debud session after this line:
testmap2[0][0].push_back(45);
-
ismaild schrieb:
std::vector <std::vector < std::vector <int> > >testmap2; for(byte i(0); i < 10; i++) codemap[i].resize(10);std::vector <std::vector < std::vector <int> > >testmap2; testmap2.resize(10); for(byte i(0); i < 10; i++) testmap2[i].resize(10); std::cout << testmap2[0][0] << std::endl;
-
Ok now the app don´t crash,
but I get this
X:\path\to\_myapp++\plzbroke\main.cpp|161|error: no match for 'operator<<' in 'std::cout << ((std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)testmap2.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, _Alloc = std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >](0u))->std::vector<_Tp, _Alloc>::operator[]|when I add this line
std::cout << testmap2[0][0] << std::endl;
-
Fellhuhn schrieb:
ismaild schrieb:
std::vector <std::vector < std::vector <int> > >testmap2; for(byte i(0); i < 10; i++) codemap[i].resize(10);std::vector <std::vector < std::vector <int> > >testmap2; testmap2.resize(10); for(byte i(0); i < 10; i++) testmap2[i].resize(10); std::cout << testmap2[0][0] << std::endl;std::vector <std::vector < std::vector <int> > >testmap2; testmap2.resize(10); for(int i = 0; i < 10; i++) { testmap2[i].resize(10); for(int ii = 0; ii < 10; ii++) { testmap2[i][ii].resize(10); } } std::cout << testmap2[0][0][0] << std::endl;
-
ismaild schrieb:
Ok now the app don´t crash,
but I get this
X:\path\to\_myapp++\plzbroke\main.cpp|161|error: no match for 'operator<<' in 'std::cout << ((std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*)testmap2.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, _Alloc = std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >](0u))->std::vector<_Tp, _Alloc>::operator[]|when I add this line
std::cout << testmap2[0][0] << std::endl;Thats because you have a 3D-vector and therefore you need three indices to specify an element.
testmap2[0][0]is of the typevector<int>.testmap2[0][0][0]would be of typeint.
-
sorry Ich habe garnicht gemerkt das ich englisch weiter gepostet habe.
Leute Ihr seid Klasse jetzt verstehe ich den salm erst.
habe da nur noch ne klitze kleine frage.
folgendes Konstrukt iteriert ja über alle elemente im Container.
for (vector < vector < vector <int> > >::iterator x=testmap2.begin(); x != testmap2.end(); ++x) for (vector < vector <int> >::iterator y=x->begin(); y != x->end(); ++y) for (vector <int>::iterator z=y->begin(); z != y->end(); ++z) std::cout << *z << std::endl;wie kann ich denn den bereich von vornerein eingrenzen in dem er iterieren soll.
sagen wirmal er soll über alle elemente iterieren die in
testmap2[0][4][*] drinne sind.
-
for (vector < vector < vector <int> > >::iterator x=testmap2.begin(); x != testmap2.begin() + 1; ++x) for (vector < vector <int> >::iterator y=x->begin()+4; y != x->begin()+5; ++y) for (vector <int>::iterator z=y->begin(); z != y->end(); ++z) std::cout << *z << std::endl;EDIT:
Wobei natürlich ein
for(vector<int>::iterator z = testmap2[0][4].begin(); z != testmap2[0][4].end(); ++z) std::cout << *z << std::endl;sinnvoller ist.
-
So jetzt bin ich schlauer ;o)) besten dank