什么是“<?=”运算符在C ++? [英] What's "<?=" operator in C++?
问题描述
我遇到了以下代码这里,这是来自使用邻接的Dijkstra算法的C ++实现
I came across the following code here, which is from the C++ implementation of Dijkstra algorithm using an adjacency matrix.
//read in edges keeping only the minimum
for(int i=0; i<E; i++) {
int v1,v2,tmp;
fin >> v1; fin >> v2;
fin >> tmp;
adjmat[v1][v2]<?=tmp; // <?= sets left to min(left,right)
adjmat[v2][v1]<?=tmp;
}
注意最后两行,应用运算符 <?=
。正在评论,以下行
Pay attention to the last two lines, which apply operator <?=
. As being commented, the following line
adjmat [v1] [v2]<?= tmp; //<?= set left to min(left,right)
会设置 left
到 min(left,right)
。
我试过在VS中的代码,它不能编译。它是什么?如何设置 left
为 min(left,right)
?
I never see this operator before. I tried the code in VS, it can not compile. What is it? How can it set left
to be min(left,right)
?
推荐答案
这是一个旧的 GCC extension ;它做它在注释中说的(它是最小运算符的复合赋值形式)。这不是标准的C ++。
It is an old GCC extension; it does what it says in the comment (it's the compound assignment form of the "minimum" operator). This is not standard C++.
a = a< b? a:b
和 a<?= b
是后者只评估每个操作数一次。
The difference between a = a < b ? a : b
and a <?= b
is that the latter only evaluates each operand once.
在现代标准C ++中,我相信你可以这样写一个赋值最小算法:
In modern standard C++, I believe you could write an "assign the minimum" algorithm like this:
auto && __a = a;
auto && __b = b;
if (!(__a < __b)) { __a = std::forward<decltype(__b)>(__b); }
这应该是一个宏的主体,它的效果是 a <?= b
和 a
和 b
是任意表达式,效果。或者您可以将其封装到模板中:
This should be the body of a macro which has the effect of a <?= b
, and a
and b
are arbitrary expressions, potentially with side effects. Or you could wrap it into a template:
template <typename T,
typename U,
typename P = std::less<std::common_type_t<std::decay_t<T>, std::decay_t<U>>>
T && clamp_to_minimum(T && a, U && b, P p = P())
{
if (!(p(a, b))) { a = std::forward<U>(b); }
return std::forward<T>(a);
}
这篇关于什么是“<?=”运算符在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!