这是一个shell脚本:
globvar=0
function myfunc {
let globvar=globvar+1
echo "myfunc: $globvar"
}
myfunc
echo "something" | myfunc
echo "Global: $globvar"
Run Code Online (Sandbox Code Playgroud)
调用时,会打印出以下内容:
$ sh zzz.sh
myfunc: 1
myfunc: 2
Global: 1
$ bash zzz.sh
myfunc: 1
myfunc: 2
Global: 1
$ zsh zzz.sh
myfunc: 1
myfunc: 2
Global: 2
Run Code Online (Sandbox Code Playgroud)
问题是:为什么会发生这种情况,哪种行为是正确的?
PS我有一种奇怪的感觉,管道后面的功能是在一个分叉的shell中调用的......那么,有一个简单的解决方法吗?
PPS此功能是一个简单的测试包装器.它运行测试应用程序并分析其输出.然后它增加$ PASSED或$ FAILED变量.最后,您将在全局变量中获得许多通过/失败的测试.用法如下:
test-util << EOF | myfunc
input for test #1
EOF
test-util << EOF | myfunc
input for test #2
EOF
echo "Passed: $PASSED, failed: $FAILED"
Run Code Online (Sandbox Code Playgroud) 这样定义,我们既不做++x++也不做++x--.但另一方面,无论是(++x)++和(++x)--是有用的表达式:(++x)++增量x由两个并返回"中间"的价值,而(++x)--实质上等同于x+1而是完全避免了打电话operator+,这是非常有用的时候.
那么为什么没有定义的优先级++x++自动扩展到(++x)++而不是++(x++)?对于后者有一些隐藏的含义,我不明白,或者只是将优先级保持为一个简单的列表,所有前缀运算符组成一个单一的级别?
编辑好的,我没有明确说出来,但是:当然我的意思x是用户定义的类型.对于内置类型,(x+=2)-1当然比更好的(++x)++,并且x+1是一个很多好过(++x)--.我想到的情况是一个相当复杂的半关联容器的迭代器,其中运算符+=和+(为随机访问而设计)必须重建缓存才能有效地处理一般请求,因此是一个命令比...慢++.但是我当然可以将它们修改为总是首先检查参数是否是一个非常小的整数,并且在这种情况下只是operator++反复调用而不是执行随机访问过程.这应该可以正常工作,虽然我可以想象我可能在某种程度上有一种情况,我想operator+=总是采用随机访问方式,无论我呈现的数字有多少.
具有简单的和精心memorizeable优先列表,其中的优点所有之前来后缀运算符任何前缀运营商足以容忍的总是不必使用括号来构成前和后缀运算符的小缺点
++/--,因为该组合物是非常少用.
更简单的"C就是这样做的",虽然看起来可能是真正的原因,但对我来说远不那么令人满意,因为C中++x++根本不允许这样做,所以可以重新定义这个非常复杂的构图而不会损坏任何构图.现有代码.
无论如何,我会继续使用(++x)--,因为括号真的没有那么多伤害.
c++ increment operator-precedence prefix-operator postfix-operator
我想知道为什么在C#中以下是好的:
int y = x++-+-++x;
Run Code Online (Sandbox Code Playgroud)
但
int y = x+++-+++x;
Run Code Online (Sandbox Code Playgroud)
是不是?为什么对+有偏见?
最近我从VB转到C#,所以我经常使用C#到VB.NET转换器来理解语法差异.在将下一个方法移动到VB时,我发现了一个有趣的事情
C#原始代码:
public bool ExceedsThreshold(int threshold, IEnumerable<bool> bools)
{
int trueCnt = 0;
foreach(bool b in bools)
if (b && (++trueCnt > threshold))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
VB.NET结果:
Public Function ExceedsThreshold(threshold As Integer, bools As IEnumerable(Of Boolean)) As Boolean
Dim trueCnt As Integer = 0
For Each b As Boolean In bools
If b AndAlso (System.Threading.Interlocked.Increment(trueCnt) > threshold) Then
Return True
End If
Next
Return False End Function
Run Code Online (Sandbox Code Playgroud)
C#的++运算符被替换为System.Threading.Interlocked.Increment
是否意味着++如果在foreach循环中使用,线程安全运算符不会成为线程安全的?它是一种语法糖吗?如果这是真的,那么为什么转换器放在Interlocked.IncrementVB版本中?我认为C#和VB中的foreach完全相同.或者它只是一个转换器"保险"?
我想增加一个Int?
目前我写的这个:
return index != nil ? index!+1 : nil
Run Code Online (Sandbox Code Playgroud)
有没有更漂亮的方式来写这个?
我不小心写道:
total_acc =+ accuracy
Run Code Online (Sandbox Code Playgroud)
代替:
total_acc += accuracy
Run Code Online (Sandbox Code Playgroud)
我在网上搜索,找不到任何东西.那么发生了什么,为什么Python认为我的意思是我在打字?
计算机太信任我们了.:)
我需要编写一个方法,通过循环旋转将字符串值从AAA递增到ZZZ(ZZZ之后的下一个值是AAA)
这是我的代码:
public static string IncrementValue(string value) {
if (string.IsNullOrEmpty(value) || value.Length != 3) {
string msg = string.Format("Incorrect value ('{0}' is not between AAA and ZZZ)", value);
throw new ApplicationException(msg);
}
if (value == "ZZZ") {
return "AAA";
}
char pos1 = value[0];
char pos2 = value[1];
char pos3 = value[2];
bool incrementPos2 = false;
bool incrementPos1 = false;
if (pos3 == 'Z') {
pos3 = 'A';
incrementPos2 = true;
} else {
pos3++;
}
if (incrementPos2 && pos2 …Run Code Online (Sandbox Code Playgroud) 在x = x + 1,x评估两次?如果是这样,这意味着什么x += 1,x只评估一次?两个表达式如何根据编译器中间代码进行评估?
例如,x++可能意味着:获取位置x,将内容加载x到寄存器中,并增加x内存中的值.
另外我读过,x += 1当x它不是一个简单的变量,而是一个涉及数组的表达式时很有用.任何想法为什么会这样?
我一直想知道是否可以在PHP中执行以下操作:
for($x = 1; $x <= 50; $x++)
echo $x;
Run Code Online (Sandbox Code Playgroud)
这将输出:
1234 etc...
Run Code Online (Sandbox Code Playgroud)
显然它不是特定的代码,因为它几乎是瞬间的,你甚至无法看到增量发生.现在问我的问题:是否有可能echo删除它,重复一遍?简单地说输出应该从头到尾在同一条线上.我不知道如何解释它或显示,但这是一个例子(有点):
1 (backspace) 2 (backspace) etc...
Run Code Online (Sandbox Code Playgroud)
我希望你能理解它,我不知道怎么解释它.-.
感谢您的帮助!
编辑:这是控制台顺便说一句,抱歉,我忘了包含它.这不适合网络!
假设我有一个如下定义的结构
struct my_struct
{
int num;
};
Run Code Online (Sandbox Code Playgroud)
....
这里我有一个指针my_struct,我想做一个增量num
void foo(struct my_struct* my_ptr)
{
// increment num
// method #1
my_ptr->num++;
// method #2
++(my_ptr->num);
// method #3
my_ptr->++num;
}
Run Code Online (Sandbox Code Playgroud)
这三种递增方式是否num做同样的事情?虽然我们正处于这种状态,但预增量是否比后增量更有效?
谢谢!