c++ - C++ 中的短参数类型

标签 c++

假设我有一个名为 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]。 typedefusingequivalent ,因此您可以使用其中任何一个。

让我们考虑使用类型别名的语法:

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/

相关文章:

c++ - 客户端和服务器之间的通信不稳定

c++ - 我如何解决 Visual Studio 2012 不支持显式转换运算符的问题?

C++函数转换题

c++ - 避免堆碎片的最佳 STL 容器

c++ - 这种内存使用优化技术在 union 的情况下是否可行?

c++ - 有没有办法在进程结束时对静态成员执行某些操作?

c++ - 对 int/float 进行排序的最快并行 C++ 排序实现是什么?

c++ - 如何在我的 g++ 编译器中满足 C++ 标准版本?

c++ - 查找 "cannot access private member declared in class ' QObject 的根本原因'”

c++ - 将字符串转换为 char*