标签: operator-keyword

Python integer*float = NotImplemented

所以当我发现这个有趣的事实时,我正在乱写一个矢量类.

>>> e = int(3)
>>> e.__mul__(3.0)
NotImplemented
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么会这样,以及随后如何修复我的矢量类?

class Vector(tuple):
    '''A vector representation.'''
    def __init__(self, iterable):
        super(Vector, self).__init__(iterable)

    def __add__(self, other):
        return Vector(map(operator.add, self, other))

    def __sub__(self, other):
        return Vector(map(operator.sub, self, other))

    def __mul__(self, scalar):
        return Vector(map(scalar.__mul__, self))

    def __rmul__(self, scalar):
         return Vector(map(scalar.__mul__, self))

    def __div__(self, scalar):
        return Vector(map(scalar.__rdiv__, self))
Run Code Online (Sandbox Code Playgroud)

编辑:更清楚一点:

>>> a = Vector([10, 20])
>>> a
(10, 20)
>>> b = a / 2.0
>>> b
(5.0, 10.0)
>>> 2 * b
(NotImplemented, NotImplemented)
Run Code Online (Sandbox Code Playgroud)

python operator-keyword

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

C++常量返回类型的后缀增量运算符

在C++中,无论我在web中看到后缀增量运算符声明的示例,它总是被声明为

T& operator++(int);
Run Code Online (Sandbox Code Playgroud)

而且我相信这是后缀增量的正确语法,不是吗?

问题在于,每当我声明后缀增量时,我都会使用const关键字声明返回类型,以便它变为类似lvalue.

请参阅示例代码:

class AClass
{
    int foo;

public:
    AClass(void) : foo(0) {};

    // Suffix increment operator
    // Consider adding const to return type
    /* const */ AClass operator++(int)
    {
        AClass cp(*this);
        foo++;
        return cp;
    };

    // Prefix increment operator
    AClass& operator++()
    {
        foo++;
        return *this;
    };
};

int main(int argc, const char* args[])
{
    /* This code would fail to compile.
    int bar = 5;
    (bar++)++;
     */

    // Similarily, I would expect this to …
Run Code Online (Sandbox Code Playgroud)

c++ coding-style increment operator-keyword

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

- - 在perl中没有正常使用char

以下代码显示的输出不同于预期.'我'应该是ans.

码:

$var = 'i';
$var++;
print $var;
$var--;
print " * ",$var;
Run Code Online (Sandbox Code Playgroud)

结果:

j ***** -1
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下行为吗?我知道我错过了一件非常愚蠢的事情.

谢谢

perl operator-keyword

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

我正在使用operator | 和&但没有得到正确的答案

public class num {
    public static void main(String args[]) {
        int i = 5, j = 9, k = 3;
        int w, x;
        w = i | j | k;
        x = i &j & k;
        System.out.println(w);
        System.out.println(x);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么价值观w = 15x = 1

java bit-manipulation operator-keyword

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

c ++嵌套条件运算符循环

我很好奇c ++如何处理这个嵌套的条件运算符.我很确定我理解它是如何工作的,但我很好奇,任何人都可以通过图解释循环如何执行嵌套的条件运算符.

例如,循环是否会为每个实例执行每个条件运算符的第一个表达式?

这个嵌套的条件运算符也是这样构造的:

(i <2)?x [i]:y;

!一世 ?y:x [1];

我想我对这个性质非常好奇.除非你能够给我一个关于循环如何执行这个条件运算符的充分解释,否则请不要回答.

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
const char x[2] [20] = {" Cloud "," at your service\n"}; 
const char * y = "Strife"; 
for (int i = 0; i < 7; i++) 
    cout << (( i < 2)? !i ? x [i] : y : x[1]);

cout << endl << endl << x[0] << endl << x[1] << endl;

cin.get(); …
Run Code Online (Sandbox Code Playgroud)

c++ conditional loops nested operator-keyword

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

用两个参数重载操作符bool是什么意思?

在一项任务中,我被告知要bool operator()(const T&, const T&)为某些课程实施.重载bool operator意味着允许隐式转换对象bool.用两个参数重载它是什么意思?这些参数如何在主叫方面传递?这是怎么用的?

c++ casting overloading operator-keyword

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

类型为'int'和'double(double*,double*,int)'的二元运算符的操作数无效

我们正在为课堂上的项目进行线性回归.我必须写一个函数.我已经尝试过静态转换和其他将"int n"更改为double的方法,因此它不会抛出错误?还是我完全错误的思路?

功能

void linear_regression(double x[], double y[], int n,
                   double *slope, double *y_int)
{    
    double sum_x, sum_y, sum_x_Squared, sum_Squared_x, product_x_y;
    double m = *slope, b = *y_int;

    sum_x = sum_array(x, n);
    sum_y = sum_array(y, n);

    sum_Squared_x = sum_square_array(x, n);
    sum_x_Squared = sum_array(x, n) * sum_array(x, n);

    product_x_y = sum_product_of_arrays(x, y, n);

    //I'm getting an error on the next statement, about the n
    m = ((sum_x * sum_y) - (n * sum_product_of_arrays)) /
            ((sum_x_Squared) - (n * sum_Squared_x));
    b = …
Run Code Online (Sandbox Code Playgroud)

c++ binary types operands operator-keyword

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

运算符'>'和'<'不能应用于'string'和'string'类型的操作数C#

在Visual Studio 2015中制作的C#程序,要求用户猜测1-10中的数字,该数字将告诉用户猜测是否正确,大于或小于必须猜测的值.

static void Main(string[] args)
    {
        string rightGuess = "7";

        Console.WriteLine("Guess the right number from 1-10: ");
        string userGuess;
        userGuess = Console.ReadLine();
        {
            if (userGuess == rightGuess)
                Console.WriteLine("You guessed right!");
            else if (userGuess > rightGuess)
                Console.WriteLine("Wrong guess. Your guess was greater than the right guess.");
            else (userGuess < rightGuess)
                Console.WriteLine("Wrong guess. Your guess was lesser than the right guess.");
        }
    }
Run Code Online (Sandbox Code Playgroud)

该程序在Visual Studio 2015中返回以下错误: 错误

已经在Google上研究了大约一个小时如何解决错误,但没有一个解决方案能够修复错误.

c# operator-keyword

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

解释我的输出

我的输出应该是5 30,输出是5 25我想知道为什么给5 25

public static void main(String[] args)
{
   int i = 1, j = 5;

   do
   {
       System.out.println( i = i++ * j);
   } 
   while (i <= 10);

   System.out.println();
}
Run Code Online (Sandbox Code Playgroud)

c# java operator-keyword

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

用户定义的简单类,带有操作符=重载代码崩溃

我正在测试一个简单的运算符重载代码,当测试它时,这段代码只是在“ nd.print()”处崩溃(将内核转储)。有什么建议吗?

崩溃发生在Ubuntu 16.04 64位上。当我尝试某些在线shell环境(例如https://www.onlinegdb.com/online_c++_compiler)时,似乎还可以。

#include <iostream>
using namespace std;

class Node
{
    int d;
    public:
    Node (int dd = 0):d(dd){}
    Node &operator=(Node &nd){ d = nd.d; }
    void print(){ cout<<d<<endl; }
};

int main()
{
    Node nd1(1), nd2(2);
    Node nd;
    nd = nd2 = nd1;
    nd.print();    //*******Crash here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望它只是打印一个值而不会崩溃。

c++ overloading operator-keyword

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