我正在学习 C++,我发现了这个奇怪的语法。
&_
是什么意思,为什么作者可以将变量用作 _1st
?
std::sort(open_list.begin(), open_list.end(), [] (const auto &_1st, const auto &_2st) {
return _1st->h_value + _1st->g_value < _2st->h_value + _2st->g_value
});
编辑 1
我认为,为了创建变量的引用,有必要使用 & 运算符后跟名称,例如:& _1st(注意它们之间的空格)。
下划线是C++中声明名称变量的一种特殊方式吗?
最佳答案
&
用于声明引用;所以 const auto &_1st
声明了一个名为 _1st
的参数,其类型是左值引用(对 const
)。它与 const auto & _1st
相同,空格多余;你甚至可以把它写成const auto&_1st
,这对下划线来说不是特别的。
请注意下划线可用于 identifiers .
An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (see below for details). A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character).
关于c++ - 使用&符号后跟下划线命名的变量是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659988/