标签: prefix-operator

Haskell中一元运算符的前缀形式

在GHCi中:

  1. 序曲>(+3)2
    5
  2. 序曲>(*3)2
    6
  3. 前奏>(/ 3)2
    0.6666666666666666
  4. 前奏>( - 3)2
    没有实例(Num(t - > t1))
    来自文字':it =( - 3)23' at <interactive>:1:2
    Possible fix: add an instance declaration for (Num (t -> t1))
    In the expression: 3
    In the expression: (- 3) 2
    In the definition of

如何更正最后一个使其返回-1?

haskell unary-operator ghci prefix-operator

3
推荐指数
2
解决办法
2039
查看次数

C:使用错误:"化合物分配"和"前缀减量"一起使用

有人可以告诉我为什么C编译器在使用a Compound Assignment和a Prefix Dec/Inc时输出错误?[但C++不]

int myVar = 5;
(--myVar) -= 4;
// C  : error C2106: '-=' : left operand must be l-value
// C++: myVar=0;
Run Code Online (Sandbox Code Playgroud)

我知道错误说的是什么......

但是,我无法理解为什么C编译器无法识别myVar为l值而是C++

c c++ compiler-errors prefix-operator compound-assignment

3
推荐指数
1
解决办法
104
查看次数

postfix前缀运算符重载错误c ++

当使用运算符重载前缀和后缀增量时,我从编译器收到错误:

"Fajl Fajl :: operator ++(int)':已定义或声明的成员函数"

以下是operator ++的标题:

Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
Run Code Online (Sandbox Code Playgroud)

而我的实施:

Fajl& Fajl::operator ++ () // prefix
{
    ++(*poz);
    return *this;
}

Fajl& Fajl::operator -- () // prefix
{
    --(*poz);
    return *this;
}

Fajl Fajl::operator ++ (int dummy) // postfix
{
    Fajl temp(*this);
    ++(*this);
    return temp;
}

Fajl Fajl::operator -- (int dummy) // postfix
{
    Fajl temp(*this);
    --(*this); …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading prefix-operator postfix-operator operator-keyword

2
推荐指数
1
解决办法
746
查看次数

为什么同时使用前缀和后缀都是非法的?

为什么下面的代码是非法的?

using System;

class Program
{
    static void Main(string[] args) {
        int i = 0;
        --i++;
        Console.WriteLine(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下错误--i++:

The operand of an increment or decrement operator must be a variable, property or indexer

我知道这段代码没有实际用途; 我只是好奇为什么不允许这样做.我不关心它可以通过删除该行而没有其他影响来修复.由于这是用标记的,请包括语言规范中的证据.

c# language-lawyer prefix-operator postfix-operator

2
推荐指数
2
解决办法
436
查看次数

c ++表达式值(运算符优先级)

以下表达式:-

int main()
{
    int x=2, y=9;
    cout << ( 1 ? ++x, ++y : --x, --y);
}
Run Code Online (Sandbox Code Playgroud)

给出以下输出:-

9
Run Code Online (Sandbox Code Playgroud)

根据我的理解,它应该返回 ++y,它应该是 10。出了什么问题?

c++ conditional-operator operator-precedence prefix-operator

2
推荐指数
1
解决办法
85
查看次数

为什么要避免在C ++中使用后缀运算符?

我听到一位教授说“避免在上下文允许选择前缀的后缀运算符”。我进行搜索,但在stackoverflow中找不到相关的帖子来解释这一点。

当我们有能力选择操作员++时,为什么更喜欢使用prefixoperator ++ postfix

c++ operators prefix-operator postfix-operator

0
推荐指数
1
解决办法
939
查看次数

方法名称开头的Bang(!)

taking_damage在此代码示例中,方法之前的感叹号的目的是什么?

if warrior.health < 20 && !taking_damage?(warrior)
     warrior.rest!
end
Run Code Online (Sandbox Code Playgroud)

ruby prefix-operator

0
推荐指数
1
解决办法
579
查看次数

后缀前缀++和二元和运算符的优先级

我对C中的postfix和前缀运算符优先级感到困惑,任何帮助和提示都会有所帮助.

我会在这里粘贴我的测试代码:

#include <stdio.h>

int a = 0;

int main(int argc, char *argv[])
{
   if (++a & 0x01) // prefix version
   // if (a++ & 0x01) // postfix version
   {
      printf("++ first\n");
   }
   else
   {
      printf("& first\n");
   }
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我现在可以理解,在postfix版本中,虽然postfix ++有一个更大的优先级,但是a++会返回0到这里&,0x01并且会a在此表达式后增加值.

但我无法理解的是为什么在前缀版本中,为什么++a首先进行评估?运算符优先级表指示prefix ++&具有相同的优先级,此外,它们的关联性为right-to-left.这种方法&不应该先评估吗?

编辑: 我正在使用的图表:C运算符优先表

c operator-precedence prefix-operator postfix-operator

0
推荐指数
1
解决办法
984
查看次数

前缀或中缀:哪个是 Haskell 的惯用语?

我是 Haskell 的新手,不知道哪个是惯用的 - 使用前缀形式或中缀形式的运算符。从我目前为止发现,有特殊字符功能名称,如<$><*>>>=等在中缀形式,但功能,其名称由字母最多的前缀形式使用使用。我的猜测是这是从数学中获得的灵感,尽管我不确定。

我查过Blow your mind , Category:Idioms并搜索了 stackoverflow ,但无济于事。

编辑

澄清一下,什么时候中缀函数和前缀运算符是惯用的?

haskell infix-operator prefix-operator

0
推荐指数
1
解决办法
251
查看次数

前缀在C++中递增变量两次

我面临着一个关于postfix和前缀运算符的经典考试问题,前缀是我无法理解的.考虑以下:

#define MAX( a, b ) ( a > b ) ? (a) : (b) 

int main()
{
    int x = 2, y = 2;

    if( MAX( ++x, y ) == x )
    {
        printf( " %d is greater than %d ", x, y );
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

考试问题要求提供该课程的输出.对我来说,它将是"3大于2"但实际输出是"4大于2"

我理解post和前缀是如何工作的(或者至少我是这么认为的)但是我不知道变量如何递增两次.对此有何解释?

c++ prefix-operator postfix-operator

-5
推荐指数
1
解决办法
193
查看次数