我最近正在研究一些代码,并决定在c ++中运行我的运算符重载,因为我以前从未真正实现它.所以我使用比较函数重载了我的矩阵类的比较运算符,如果LHS小于RHS则返回0,如果LHS大于RHS则返回1,如果它们相等则返回2.然后我在整数上利用了逻辑而不是c ++的属性,以便在一行中获得我的所有比较:
inline bool Matrix::operator<(Matrix &RHS){
return ! (compare(*this,RHS));
}
inline bool Matrix::operator>(Matrix &RHS){
return ! (compare((*this),RHS)-1);
}
inline bool Matrix::operator>=(Matrix &RHS){
return compare((*this),RHS);
}
inline bool Matrix::operator<=(Matrix &RHS){
return compare((*this),RHS)-1;
}
inline bool Matrix::operator!=(Matrix &RHS){
return compare((*this),RHS)-2;
}
inline bool Matrix::operator==(Matrix &RHS){
return !(compare((*this),RHS)-2);
}
Run Code Online (Sandbox Code Playgroud)
显然我应该将RHS作为const传递,我可能不会再次使用这个矩阵类而且我不想编写另一个不是仅仅为比较器操作获取数组索引值的引用的函数.
根据建议,这里是代码,如果Compare返回-1表示less,0表示相等,1表示正数.
inline bool Matrix::operator<(Matrix &RHS){
return ! (compare(*this,RHS)+1);
}
inline bool Matrix::operator>(Matrix &RHS){
return ! (compare((*this),RHS)-1);
}
inline bool Matrix::operator>=(Matrix &RHS){
return compare((*this),RHS)+1;
}
inline bool Matrix::operator<=(Matrix &RHS){
return compare((*this),RHS)-1;
}
inline …Run Code Online (Sandbox Code Playgroud) 我最近为我正在研究的一个研究项目编写了一些代码,效率非常重要.我一直在考虑使用一些常规方法来处理并使用按位XOR代替.我想知道的是,如果有差异(如果我执行此操作说数百万次),或者如果我使用03 in g ++后它是相同的话.
想到的两个例子:
我有一个实例(我正在使用纯正的整数)如果n是奇数,我需要将n改为n-1或者如果n是偶数,则需要将n改为n +(n + 1).我想我有几个选择:
if(n%2) // or (n%2==0) and flip the order
n=n-1
else
n=n+1
Run Code Online (Sandbox Code Playgroud)
要么
n=n+2*n%2-1; //This of course was silly, but was the only non-bitwise 1 line I could come up with
Run Code Online (Sandbox Code Playgroud)
最后:
n=n^1;
Run Code Online (Sandbox Code Playgroud)
所有这些方法显然都做同样的事情,但我的感觉是第三种方法效率最高.
下一个例子是更一般的说明.假设我正在比较两个正整数,其中一个会比其他整数表现更好.或者即使我执行此操作数百万次,差异也不会明显:
if(n_1==n_2)
if(! (n_1 ^ n_2) )
if( n_1 ^ n_2) else \do work here
Run Code Online (Sandbox Code Playgroud)
编译器是否会在所有这些实例中执行相同的操作?我只是好奇是否有一个实例我应该使用按位操作而不相信编译器为我做的工作.
修正:正确的问题陈述.
在最近回答涉及Ackerman函数的问题之后,其中一部分涉及计算数字的分解的函数.这让我思考是否有更有效的方法来做到这一点.我自己做了一些测试,但我主要受限于这样一个事实,即5 ^^ 3 = 5 ^ 3125给出5 ^ 3的数字大约是10 ^ 2,意味着5 ^ 3125~ = 10 ^(3125*2/3)大约2000位数.
由于取幂的性质,该函数不适用于划分和征服方法,即:
2 ^^ 5 = 2 ^(2 ^(2 ^(2 ^ 2))))= 2 ^(2 ^(2 ^ 4))= 2 ^(2 ^ 16)= 2 ^ 65536〜= 10 ^( 65536*3/10)所以大约20k位......
这个问题的本质,因为它从功率树的顶部开始并且向下工作,这使我感觉像是阶乘的.可以使用快速功率算法来进行取幂运算,但是我还没有看到缩小取幂运算次数的方法.
如果有人不清楚我在说什么是wiki文章,基本上虽然tetration是:
a ^^ b = a ^ a ^ a ... ^ a,b次,然后在幂树的顶部元素处开始取幂并向下运算.
我目前正在使用的算法是(虽然如果我真的想要值,我使用的是ruby版本):
long int Tetration(int number, int tetrate)
{
long int product=1;
if(tetrate==0)
return product;
product=number;
while(tetrate>1)
{
product=FastPower(number,product);
tetrate--;
}
return product; …Run Code Online (Sandbox Code Playgroud) 我试图编写一个STL方法来获取向量的日志:
for_each(vec.begin(),vec.end(),log);
Run Code Online (Sandbox Code Playgroud)
但我明白了
no matching function for call to ‘for_each(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, <unresolved overloaded function type>)’
Run Code Online (Sandbox Code Playgroud)
我收集的是由于日志功能的多个版本.显然我可以在日志函数周围编写一个简单的包装器并用它调用它.有没有更简单的方法来指定我想要内联的日志功能?
我不完全确定这个问题是否属于stackoverflow或超级用户(是否有emacs堆栈交换?).基于meta.stackoverflow帖子,我会假设它确实如此.
我的emacs默认头文件(.h种类)到c模式.我可以很容易地输入Mx c ++ - mode并重新突出显示,但是因为我在c ++中比c更频繁地编程.我想知道我需要改变什么才能将.h添加到c ++组.