【发布时间】:2021-10-27 10:51:54
【问题描述】:
为了调试代码,我正在尝试创建一个用于轻松输出容器的模板。我希望能够做到
int a = 3;
pair<int,int> b = {2,3};
vector<int> c = {1,2,3,4};
map<int,int> m;
m[2] = 3;
m[3] = 4;
dbg(a,b,c,m);
OUTPUT:
[a,b,c,m] = [ 3 , (2,3) , {1,2,3,4} , {(2,3),(3,4)} ]
到目前为止,我有这个:
#define dbg(x...) cout << "[" << #x << "] = [ "; _dbg(x); cout << "]"
template <typename T> void _dbg(T t)
{
cout << t << " ";
}
template<typename T, typename... Args>
void _dbg(T t, Args... args) // recursive variadic function
{
cout << t << " , ";
_dbg(args...);
}
template <typename T, typename V>
ostream& operator<<(ostream& os, const pair<T, V> p)
{
cout << "(" << p.first << "," << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& dt)
{
cout << "{";
auto preEnd = dt.end();
preEnd--;
for (auto bgn = dt.begin(); bgn != preEnd; bgn++)
cout << *bgn << ",";
cout << *preEnd << "}";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& dt)
{
cout << "{";
auto preEnd = dt.end();
preEnd--;
for (auto bgn = dt.begin(); bgn != preEnd; bgn++)
cout << *bgn << ",";
cout << *preEnd << "}";
return os;
}
template <typename T, typename V>
ostream& operator<<(ostream& os, const map<T, V>& dt)
{
cout << "{";
auto preEnd = dt.end();
preEnd--;
for (auto bgn = dt.begin(); bgn != preEnd; bgn++)
cout << *bgn << ",";
cout << *preEnd << "}";
return os;
}
而且效果很好!只是我不想为每个容器类型定义一个函数,因为它们都具有相同的主体(谈论最后 3 个函数)。 我尝试了类似的东西
template<typename C, typename T>
ostream& operator<<(ostream& os, const C<T>& dt)
但我得到了
error: C is not a template
我试过了
template<typename C>
ostream& operator<< (ostream& os, const C& dt)
得到了
error: no match for 'operator<<' (operand types are
'std::ostream {aka std::basic_ostream<char>}' and 'std::set<int>')|
那么我如何只拥有一个处理任何容器的通用函数(例如,一个是 template<typename T> vector<T>,另一个是 set<T>??)
【问题讨论】:
-
使用迭代器(在辅助函数中)代替容器,并将容器传递给接受模板模板参数的单个函数,然后调用该辅助函数!
-
您能否提供一些示例代码来说明接受模板模板参数的函数?我以前从未听说过。
-
这能回答你的问题吗? Template class with template container
标签: c++ algorithm templates operator-overloading function-templates