如何在 C++ 中重载双下标运算符 [][] ?
我尝试了很多方法..没有任何具体的答案..
提前致谢..
我已经尝试过这个..但我知道它不正确
class Matrix {
int row;
int col;
int ** values;
int ptr;
public:
Matrix(const int r, const int c) {
ptr = -1;
row = r;
col = c;
values = new int*[row];
for(int i=0; i<row; i++) {
values[i] = new int[col];
}
}
int & operator[](int p) {
if(ptr == -1)
ptr = p;
return values[ptr][p];
}
};
Run Code Online (Sandbox Code Playgroud) 我很确定你们中有人可以帮助我解决这个问题。我没有找到也不理解解决方案。
if (hero.IsAlive || creature.IsAlive == false)
{
if (hero.IsAlive == true)
{
Console.ForegroundColor = ConsoleColor.Red;
EnemyHitInterface();
Console.ForegroundColor = ConsoleColor.White;
Thread.Sleep(1500);
hero.Exp += 150;
baseSetting.YouWin();
hero.UpdateLvL();
hero.CharacterInterface(hero);
hero.PrintStatsHero();
Console.ReadKey();
CheckForItem();
hero.PrintStatsHero();
Console.ReadKey();
}
else if (creature.IsAlive == true)
{
Console.Clear();
CenterText("You are dead. See you next time...", 15);
Console.ReadKey();
Environment.Exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,如果操作很简单,一旦英雄或生物死亡,它就会被触发。如果生物死了,一切都很好,但如果英雄死了,就不会被触发。我认为什么是我不明白的?
对我来说就像:
当英雄或生物死亡时,进入 如果英雄还活着,则执行此操作 如果生物还活着,则执行此操作
但就像我说的,对于生物来说这是行不通的
我们正在讨论我们在编码课中看到的东西.根据我们的记忆,教师使用+ =和C#以某种方式实例化/创建变量.
我知道它不是处理订阅事件或将y添加到x但是不知道是否有人可以对我们可能看到的内容有所了解?
在python语言中,'>>'运算符和'/'运算符在执行时间方面有什么区别,当我们要将任何整数除以'2'的倍数时,为什么?
我在我的程序中遇到了重载运算符"+"," - ","/"和"*"的问题.基本上我有一个对象,它是动态分配的double值数组.我有整个程序,但我无法克服那些重载.
我的构造函数看起来像这样:
table::table(int size) {
this->size = size;
tab = new double[size];
count++;
}
Run Code Online (Sandbox Code Playgroud)
我写过类似的东西:
table & table::operator-(const table &tab3 )
{
table * tab_oper2 = new table(size);
for(int i=0; i< tab3.size; i++)
{
(*this).tab[i] -= tab3.tab[i];
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
这通常是有效的,但通常不是这样做的好方法.我的导师告诉我尝试将(*this)切换到*tab_oper2,将其作为参考返回,但它不起作用.拜托,有人能告诉我怎么做得好吗?
我希望R更加pythonic.转换它的步骤之一是使用'not'而不是'!'.我希望这不需要编写C++和编译等,或者它呢?
功能:"!TRUE"应该等于"不为真".
如果你知道从哪里开始,请指点我.
所以,我只想知道是否可以在for循环的终止位内的任何代码或三元运算符中滑动.如果可能的话,你能在for循环中提供一个三元运算符的例子吗?谢谢!
for (initialization; termination; increment) {
statement(s)
}
Run Code Online (Sandbox Code Playgroud) 第11,12,15和16行正在获得错误:"int类型的无效操作数和const char [2]到二元运算符<<"(我删除了"`",因此它不会显示代码格式).
#include<iostream>
using namespace std;
int main(){
int md,dg,dd,mg,m,d;
cin >> md >> dg >> dd >> mg;
if (dd+dg==md+mg){
cout << (mg>dg) ? 0 : 1 << " ";
cout << (dg>mg) ? 0 : 1 << endl;
}
else{
cout << (mg+md>dd+dg) ? 0 : (dd+dg-mg-md) << " ";
cout << (dg+dd>md+mg) ? 0 : (md+mg-dg-dd) << endl;
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码
public class Test {
public static void main(String... strings) {
System.out.println("String, " + false);
System.out.println("String, " + getFalse());
System.out.println("String, " + new TestClass());
System.out.println("String, " + (new TestClass() == null));
System.out.println("String, " + new TestClass() == null);
}
private static class TestClass {
public String toString() {
return "false";
}
}
private static boolean getFalse() {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么最后一次System.out.println打印的输出与其他输出不同?我不知道,这是一个错误吗?是否与plus运算符有关?StringBuilder?
我想在ruby中做这样的事情:
x = 0
y = 1
z = "!="
if x #{z} y
puts "True"
end
#True
#=> nil
x = 1
if x #{z} y
puts "True"
end
#True
#=> nil
Run Code Online (Sandbox Code Playgroud)
使用运算符作为变量不会计算表达式.任何方式来做到这一点.
operator-keyword ×10
c++ ×3
c# ×2
java ×2
overloading ×2
ternary ×2
behavior ×1
conditional ×1
double ×1
if-statement ×1
python ×1
r ×1
ruby ×1
string ×1
termination ×1
variables ×1