#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
template<typename T>
using Con = vector<T>;
template<typename T>
using Con2 = deque<T>;
template<
typename T,
template<typename,typename> typename C=vector,
template<typename> typename E=std::allocator >
C<T,E<T>> f(int size) {
C<T,E<T>> con;
for (int i = 0; i < size; ++i)
con.push_back(T());
return con;
}
int main() {
auto x = f<double>(10);
cout << typeid(x).name() << endl;
auto y = f<int, deque>(10);
cout << typeid(y).name() << endl;
auto z = f<string, deque>(10);
cout << typeid(z).name() << endl;
}
备查。
本文展示了一个使用C++模板元编程创建泛型容器的示例。通过定义通用模板函数,可以为不同数据类型生成特定数量的容器实例。示例中包括了如何为double、int和string类型生成不同类型的容器,并通过运行时输出展示了容器的具体类型。
4065

被折叠的 条评论
为什么被折叠?



