所以当我发现这个有趣的事实时,我正在乱写一个矢量类.
>>> 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) 在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) 以下代码显示的输出不同于预期.'我'应该是ans.
码:
$var = 'i';
$var++;
print $var;
$var--;
print " * ",$var;
Run Code Online (Sandbox Code Playgroud)
结果:
j ***** -1
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下行为吗?我知道我错过了一件非常愚蠢的事情.
谢谢
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 = 15和x = 1?
我很好奇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) 在一项任务中,我被告知要bool operator()(const T&, const T&)为某些课程实施.重载bool operator意味着允许隐式转换对象bool.用两个参数重载它是什么意思?这些参数如何在主叫方面传递?这是怎么用的?
我们正在为课堂上的项目进行线性回归.我必须写一个函数.我已经尝试过静态转换和其他将"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) 在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上研究了大约一个小时如何解决错误,但没有一个解决方案能够修复错误.
我的输出应该是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) 我正在测试一个简单的运算符重载代码,当测试它时,这段代码只是在“ 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)
我希望它只是打印一个值而不会崩溃。
operator-keyword ×10
c++ ×5
c# ×2
java ×2
overloading ×2
binary ×1
casting ×1
coding-style ×1
conditional ×1
increment ×1
loops ×1
nested ×1
operands ×1
perl ×1
python ×1
types ×1