Форум » C/C++ для начинающих (C/C++ for beginners) » 2D vector to 3D » Ответить

2D vector to 3D

ramees: i have a 2D vector that contain data like [0][1] = 11 [0][2] = 10 [0][3] = 12 [1][1] = 54 [1][2] = 65 [1][3] = 46 [2][1] = 524 [2][2] = 655 [2][3] = 446 i like to add this 2D vector data to a 3D vector how can i do this in a perfect way

Ответов - 5

Сыроежка: Having one two-dimensional array you can create a 3D vector only with one element. For example [pre2] #include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { const size_t N = 3; int a[N][N] = { { 11, 10, 12 }, { 54, 65, 46 }, { 524, 655, 446 } }; std::vector<std::vector<std::vector<int>>> v( 1 ); std::transform( std::begin( a ), std::end( a ), std::back_inserter( v[0] ), []( const int ( &row )[N] ) { return std::vector<int>( std::begin( row ), std::end( row ) ); } ); std::for_each( v[0].begin(), v[0].end(), []( const std::vector<int> &v1 ) { std::copy( v1.begin(), v1.end(), std::ostream_iterator<int>( std::cout, " " ) ); std::cout << std::endl; } ); /* Or using range-based for statements for ( const std::vector<int> &v1 : v[0] ) { for ( int x : v1 ) std::cout << x << ' '; std::cout << std::endl; } */ } [/pre2] The output is [pre2] 11 10 12 54 65 46 524 655 446 [/pre2] If the 2D vector is not an array but in turn an object of type std::vector<std::vector<int>> then you can simply use push_back method.. For example [pre2] std::vector<std::vector<std::vector<int>>> v; std::vector<std::vector<int>> v1; //... some code of filling v1 v.push_back( v1 ); [/pre2]

ramees: how can i control index of 3d vector to pushback 2D vector to it i don't get complete idea of 3D vector. for example if we creating a 2D array we use two for loop to control index (column index & row index ) if i use your last method to pushback 2D vector. in 3D vector --> [0][0][1] , [0][0][2] i like to manipulate other index to how can i do that

Сыроежка: I have not understood what is unclear for you. You can use vectors as arrays accessing their elements by indices. 3D vector contains 2D vectors. So for example if v is 3D vector then v[0] provides access to the first 2D vector, v[1] provides access to the next 2D vector.


ramees: i ask you how i write for loop for that to access [0][0][1],[0][1][1],[1][1][1] . i got it thanks. is there any better way of doing this please show me . [/pre2] #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; } [/pre2]

Сыроежка: In fact that or other way you have to use loops. In C++ 11 I would write the code the following way [pre2] #include <iostream> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> int main() { size_t K = 2; size_t N = 4; size_t M = 15; std::vector<std::vector<std::vector<int>>> v( K ); std::srand( ( unsigned int )std::time( 0 ) ); for ( std::vector<std::vector<int>> &w : v ) { w.assign( N, std::vector<int>( M, 0 ) ); for ( std::vector<int> &u : w ) { std::generate( u.begin(), u.end(), []{ return std::rand() % 10; } ); } } for ( const std::vector<std::vector<int>> &w : v ) { for ( const std::vector<int> &u : w ) { for ( int x : u ) std::cout << x << ' '; std::cout << std::endl; } std::cout << std::endl; } return 0; } [/pre2] The output is [pre2] 9 1 5 7 9 8 3 4 9 9 3 6 8 7 6 9 0 1 5 9 2 1 1 3 3 4 7 7 2 3 2 3 6 8 0 7 6 3 2 7 4 7 3 2 4 0 1 4 3 8 3 7 9 6 0 3 1 9 2 5 4 4 8 3 4 8 0 2 3 4 9 7 3 3 1 7 5 4 4 0 2 9 7 1 6 9 6 9 0 8 6 4 5 4 7 9 4 8 2 7 4 1 6 8 6 7 7 1 1 1 3 5 3 0 6 9 1 5 0 1 [/pre2]



полная версия страницы