- 论坛徽章:
- 0
|
ms我写复杂了,这样更简单:
// Purpose: convert list<list<...<T> > > to vector<vector<...<T> > >
// Author: Bood
// Date: 2008-08-28
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <iterator>
using namespace std;
// Construct return type
template <typename T>
struct ReturnType
{
typedef typename T return_type;
};
// Construct return type
template <typename T>
struct ReturnType<list<T> >
{
typedef typename vector< typename ReturnType<T>::return_type > return_type;
};
// Convert function
template <typename T>
vector<typename ReturnType<T>::return_type> foo(const list<T> &src)
{
vector<typename ReturnType<T>::return_type> v;
for(list<T>::const_iterator it = src.begin();
it != src.end();
++it) {
v.push_back(foo(*it));
}
return v;
};
// Return immediately for other types
template <typename T>
T foo(const T &t)
{
return t;
};
int main()
{
list< list<int> > l;
for(int i=0;i<10;i++) {
list<int> ll;
for(int j=i*10;j<i*11;j++) {
ll.push_back(j);
}
l.push_back(ll);
}
vector< vector<int> > v;
v = foo(l);
for(vector< vector<int> >::const_iterator it = v.begin();it!=v.end();++it) {
copy(it->begin(), it->end(), ostream_iterator<int>(cout, "\n"));
}
return 0;
}
|
[ 本帖最后由 bood 于 2008-9-1 10:40 编辑 ] |
|