假设我有一个名为 Graph 的类。我想通过使用较短的类型名称来缩短参数列表。有可能吗?
void Graph::APUtil(int u, vector <bool> visited, vector <bool> disc,
vector <bool> low, vector <bool> parent, vector <bool> ap)
我试过使用 auto 但它不起作用:
void Graph::APUtil(int u, auto visited, auto disc ...
我认为因为这是一个类方法,所以 lambda 也不起作用。
有什么建议吗?我要求这样做是为了节省编码挑战的时间。
最佳答案
有 3 种方法可以做到这一点。
应避免使用宏 [#define
]。 typedef
和 using
是 equivalent ,因此您可以使用其中任何一个。
让我们考虑使用
类型别名的语法:
using identifier attr(optional) = type-id;
所以,对于你的代码,你应该写
using v = std::vector<bool>;
// . . .
void Graph::APUtil(int u, v visited, v disc,
v low, v parent, v ap);
关于c++ - C++ 中的短参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58639824/