Last active
August 29, 2015 14:19
-
-
Save pung96/e8142fe40a8367915b7c to your computer and use it in GitHub Desktop.
multi_vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T, int dim> class multi_vector_tool; | |
template <typename T, int dim> using multi_vector = typename multi_vector_tool<T,dim>::type; | |
template <typename T, int dim> | |
struct multi_vector_tool{ | |
typedef std::vector<typename multi_vector_tool<T,(dim-1)>::type> type; | |
static void resize( multi_vector<T,dim> & vec, std::array<int,dim> index, T v = T{} ){ | |
vec.resize( index[0] ); | |
std::array<int,dim-1> index2; | |
std::copy(index.begin()+1,index.end(), index2.begin() ); | |
for( auto & a : vec ) | |
multi_vector_tool<T,dim-1>::resize( a, index2, v ); | |
} | |
}; | |
template <typename T> | |
struct multi_vector_tool<T,0>{ | |
typedef T type; | |
static void resize( multi_vector<T,0> & vec, std::array<int,0> index, T v = T{}){ | |
vec = v ; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "multi_vector.h" | |
useing namespace std; | |
int main(){ | |
multi_vector<int,2> a1; | |
multi_vector_tool<int,2>::resize( a1, { 2, 3 } ); | |
cout<<"a1 : "<<a1.size()<<endl; | |
for( auto a2 : a1 ){ | |
cout<<"a2 : "<<a2.size()<<endl; | |
for( auto a3 : a2) { | |
cout<<a3<<endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment