Last active
August 29, 2015 14:05
-
-
Save MiSawa/f555ca8b723db10e699b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <bits/stdc++.h> | |
using namespace std; | |
template<typename T, size_t n> | |
struct Vec{ | |
typedef vector<typename Vec<T, n-1>::V> V; | |
static V init(const T &x, initializer_list<size_t>::iterator it){ | |
const size_t s = *it; | |
return V(s, Vec<T, n-1>::init(x, ++it)); | |
} | |
static V init(const T &x, initializer_list<size_t> d){ | |
assert(d.size() == n); | |
return init(x, d.begin()); | |
} | |
static V init(initializer_list<size_t> d){ | |
assert(d.size() == n); | |
return init(T(), d.begin()); | |
} | |
}; | |
template<typename T> | |
struct Vec<T, 0>{ | |
typedef T V; | |
static V init(const T &x, initializer_list<size_t>::iterator it){ return x; } | |
}; | |
int main(){ | |
auto v = Vec<int, 4>::init(numeric_limits<int>::max(), {3, 3, 3, 3}); | |
cout << v[0][0][0][0] << endl; | |
return 0; | |
} |
This file contains hidden or 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 <bits/stdc++.h> | |
using namespace std; | |
template<typename T, size_t n> | |
struct Vec{ | |
typedef vector<typename Vec<T, n-1>::V> V; | |
template<typename Size, typename... Sizes> | |
static V init(const T &x, Size size, const Sizes&... sizes){ | |
return V(size, Vec<T, n-1>::init(x, sizes...)); | |
} | |
template<typename Size, typename... Sizes> | |
static V init(Size size, const Sizes&... sizes){ | |
return V(size, Vec<T, n-1>::init(T(), sizes...)); | |
} | |
}; | |
template<typename T> | |
struct Vec<T, 0>{ | |
typedef T V; | |
static V init(const T &x){ return x; } | |
}; | |
int main(){ | |
auto v = Vec<int, 4>::init(3, 3, 3, 3); | |
cout << v[0][0][0][0] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment