我正在用c ++编写一个日志类.这个班是一个单身人士.我想以这样的方式添加日志:
Log::GetInstance() << "Error: " << err_code << ", in class foo";
Run Code Online (Sandbox Code Playgroud)
好吧,在Log对象中,我希望在最后一个参数出现时保存整行(",在本例中为"类foo").
如何检测最后一个<<参数?<< a << b << is_this_last << maybe_this_is << or_not.
我不使用任何结束标签.
我在perl和php中都看过这个(例如:$ variable - > define something),但之前我从未真正使用它.这个运算符的目的是什么 - >它是分配值还是传递参数?
谢谢
我只是尝试创建一个简单的类,让我弄清楚文件的长度:
public class Size {
long s = 0;
int a;
public static void main(String[]args){
new Size();
}
Size(){
try{
FileInputStream str = new FileInputStream("E:/Eclipse/Resources/smile.jpg");
while(a != null){
s++;
}
}catch (IOException e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了一个问题
while(a != null)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
对于参数类型int,null,运算符!=未定义
任何想法为什么阻止这种情况?
我试图让以下内容起作用,但没有成功:
我定义了自己的类型Unit(从内置类型继承float)来实现具有单位的数量的代数.它做的事情是这样的:
class Unit(float):
"""provide a simple unit converter for a given quantity"""
def __new__(cls, unit, num=1.):
return super(Unit, cls).__new__(cls, num)
def __init__(self, unit, num=1.):
"""set up base unit"""
self.unit = unit
def __str__(self,):
return '{:s} {:s}'.format(super(Unit, self).__str__(), self.unit)
def __rmul__(self, other):
print 'rmul: {:f}'.format(super(Unit, self).__rmul__(other))
return Unit(self.unit, super(Unit, self).__rmul__(other))
def to(self,target):
fun_conv = _conv(self.unit, target)
return Unit(target, num=fun_conv(self))
c = 3e8 * Unit('m/s') # this will 1) create a Unit instance with magnitude '1' and …Run Code Online (Sandbox Code Playgroud) 在下面的例子中,我创建了一个名为'Custom'的类,它实现了IComparable:
public int CompareTo(Object value)
{
// comparison logic here
}
Run Code Online (Sandbox Code Playgroud)
CompareTo(Object)的实现通常是"宽容的",因为它们会将"值"转换为更具体的类型.在这种情况下,将执行"Custom"类型的强制转换,以便进行比较.想象一下,我也重载了==运算符:
public static bool operator== (Custom lhs, Custom rhs)
{
// equivalence test here
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是在这种情况下:
Custom c = GetCustomObject();
Object o = GetOtherObject();
if(c == o)
{
// will not be true unless c and o are the same object
}
Run Code Online (Sandbox Code Playgroud)
问题是当调用==时,因为rhs是'Object'类型,它会回退到参考相等的默认测试 - 不调用重载.
这里有什么期望?我可以为==运算符添加另一个重载:
public static bool operator== (Custom lhs, Object rhs)
Run Code Online (Sandbox Code Playgroud)
但是,MSDN或其他在线示例中明显缺少这样的示例.这让我认为参考相等性的测试是预期的行为
我在Topcoder上查看问题的解决方案,并遇到了这个:
http://community.topcoder.com/stat?c=problem_solution&rm=249419&rd=9996&pm=6621&cr=309453
目前我不知道算法是如何工作的,但是代码中"<?="运算符的用法是什么?我尝试在我的机器上编译代码,也在ideone.com上编译,但令我惊讶的是,代码无法编译.运营商做了什么,为什么它不能在像ideone这样的标准编译器上运行,代码在Topcoder上通过了系统测试?
代码在这里:
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#define PB push_back
#define SZ size()
#define REP(v, hi) for (int v=0; v<(hi); v++)
#define REPD(v, hi) for (int v=((hi)-1); v>=0; v--)
#define FOR(v, lo, hi) for (int v=(lo); v<(hi); v++)
#define FORD(v, lo, hi) for (int v=((hi)-1); v>=(lo); v--)
typedef vector <int> …Run Code Online (Sandbox Code Playgroud) $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
print_r(array_filter($array1, "odd"));
function odd($var)
{
// returns whether the input integer is odd//
return($var & 1);
}
Run Code Online (Sandbox Code Playgroud)
在返回值中有什么操作&符?它如何返回奇数
我已经使用SO一段时间作为参考,但之前从未问过问题.我目前正在大学C++课程中阅读Bjarne Stroutstrup 编程:原理和实践,仅仅是为了我自己的利益,因为我在这里看到了一个真正推荐它的问题的答案.
我们现在正在课堂上覆盖操作员,而我似乎无法理解逗号操作符在语句中的工作方式.一个例子是类的在线部分的示例问题,即使我编写C程序并使用GDB获取结果,我仍然会出错.问题是:
假设在下面的表达式之前x == 16,下面的表达式的值是什么(不一定是x的值)?
x ++,++ x,x + = x
我对正确答案不感兴趣,以至于如何得到正确的答案.我已经阅读了类似问题的几个答案,例如这里的问题,但似乎我错过了当实际上没有分配运算符时这是如何适用的.这跟说的一样吗
int y = (x++, ++x, x+=x);
Run Code Online (Sandbox Code Playgroud)
要么
int y = x++, ++x, x+=x;
Run Code Online (Sandbox Code Playgroud)
还是两个?有人可以解释逗号运算符的工作原理,特别是与没有赋值的语句有关吗?
我明白"!" 要么 "?" 作品.但我不太确定与之相比的额外好处是什么!= nil checking.转移到"!?"的额外好处是什么?我觉得这只是Apple添加的东西,但与iOS现状相比,无法真正看到额外的好处.我错过了什么吗?提前致谢.
我正在发现C++,我想使用模板创建一个迷你数学Matrix librairy.
在这里,我想重载运算符*.
如果我描述了这样的矩阵:M(y, x)以M矩阵的名称,y以及x高度和宽度,矩阵乘法看起来应该:
M(a, b) * N(b, c) = R(a, c)
Run Code Online (Sandbox Code Playgroud)
目前我有这个代码:
template<unsigned int y, unsigned int x>
class Matrix
{
public:
Matrix() { }
~Matrix() { }
Matrix<y, x2>& operator*(const Matrix<y2, x2>& right)
{
// code...
}
private:
std::array<std::array<double, x>, y> m_values;
};
Run Code Online (Sandbox Code Playgroud)
所以我希望能够将两个不同的矩阵相乘:
Matrix<3, 4> m;
Matrix<4, 2> n;
// fill the matrix with values
Matrix<3, 2> o = m * n;
Run Code Online (Sandbox Code Playgroud)
我搜索了但是我没有找到这个问题的答案(也许是因为我真的不知道我必须要搜索的内容).
谢谢你的帮助 :)