[ create a new paste ] login | about

Link: http://codepad.org/Nyxhzddm    [ raw code | output | fork ]

kinaba - C++, pasted on Nov 29:
#include <vector>
#include <iostream>
using namespace std;

// meta-function (for computing the return type of array2Vector)
template<typename T> struct a2v
	{ typedef T type; };
template<typename T, size_t N> struct a2v<T[N]>
	{ typedef vector<typename a2v<T>::type> type; };

// runtime-function
template<typename T>
	T array2Vector( T v )
	{
		return v;
	}
template<typename T, size_t N>
	typename a2v<T[N]>::type array2Vector( T (&arr)[N] )
	{
		typename a2v<T[N]>::type vec(N);
		for(size_t i=0; i<N; ++i)
			vec[i] = array2Vector( arr[i] );
		return vec;
	}

// test
int main()
{
	int kArray[3][2] = {{1,2}, {4, 5}, {6, 7}};
	vector<vector<int> > a(array2Vector(kArray));

	for(size_t x=0; x<a.size(); ++x, cout<<endl)
		for(size_t y=0; y<a[x].size(); ++y)
			cout << a[x][y] << " ";
}


Output:
1
2
3
1 2 
4 5 
6 7 


Create a new paste based on this one


Comments: