Consider the following declarations:
vector<vector<int> > v2d; vector<vector<vector<string>> > v3d;
How can I find out the "dimensionality" of the vectors in subsequent code? For example, 2 for v2d and 3 for v3d?
Something on these lines:
template<class Y> struct s { enum {dims = 0}; }; template<class Y> struct s<std::vector<Y>> { enum {dims = s<Y>::dims + 1}; };
Then for example,
std::vector<std::vector<double> > x; int n = s<decltype(x)>::dims; /*n will be 2 in this case*/
Has the attractive property that all the evaluations are at compile time.
You could do something like this:
template<typename T> int getDims(const T& vec) { return 0; } template<typename T> int getDims(const vector<T>& vec) { return getDims(T{})+1; }
Sidenote: This quantity is sometimes called "rank".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With