标签: operator-keyword

为一对中的一个元素提供少于运算符

什么是最优雅的方式也修复以下代码:

#include <vector>
#include <map>
#include <set>
using namespace std;

typedef map< int, int > row_t;
typedef vector< row_t > board_t;
typedef row_t::iterator area_t;

bool operator< ( area_t const& a, area_t const& b ) {
    return( a->first < b->first );
};

int main( int argc, char* argv[] )
{
    int row_num;
    area_t it;

    set< pair< int, area_t > > queue;
    queue.insert( make_pair( row_num, it ) ); // does not compile
};
Run Code Online (Sandbox Code Playgroud)

修复它的一种方法是将less <to namespace命名为std(我知道,   你不应该这样做.)

namespace std {
    bool …
Run Code Online (Sandbox Code Playgroud)

c++ stl operators operator-keyword

6
推荐指数
1
解决办法
2401
查看次数

重载多个运算符

简而言之,我的目标是让foo [bar]返回type1,而foo [bar] =返回type2.

我正在用C++编写一个对象,它的出现很顺利,但是我只希望做一件小事,但似乎不可能.

我的对象是一个存储类,所以我使用数组下标来访问值.我还需要赋值,所以我也重载了=运算符.但是,它有点不方便,因为我的类所持有的值是第一类对象,因此对于我的数组下标重载,我不能按原样返回它们.我必须返回一个中间类来处理=运算符,但我也想要检索该值而无需额外输入.

有没有办法做到这一点?哈金的方式是可以接受的.

编辑:这是它(应该)做的一个例子

#include<cstdio>
#include<cstdlib>

class foo{
    char* a[100];
    foo(){
        for( int i = 0; i < 100; i ++)
            a[i] = 0;
    }
    char* operator[] (int location){
        return a[location];
    }
    foo& operator[]= (int location, const char* value ){
        if( a[location] == 0 )
            a[location] = (char*) malloc( strlen( value ) + 1 );
        else
            a[location] = (char*) realloc( a[location], strlen( value ) + 1 );
        strcpy( a[location], value );
    }
};
int main(){
    foo …
Run Code Online (Sandbox Code Playgroud)

c++ operator-keyword

6
推荐指数
1
解决办法
1144
查看次数

如何在不修改操作数的情况下使用运算符重载链?

假设我们有这个A类:

class A
{
public:
   int a;

   A(int b)
   {
      a = b;
   }
};
Run Code Online (Sandbox Code Playgroud)

我想创建一个+重载,这样我就可以像这样使用它

A a(1),b(2),c(3),&d;
d = a + b + c;
Run Code Online (Sandbox Code Playgroud)

不修改每个对象的内容.下一个合乎逻辑的事情是每次分配一块新的内存,如下所示:

A &operator+ (const A &b)
{
   A *c = new A(a+b.a);
   return *c;
}
Run Code Online (Sandbox Code Playgroud)

但这会产生一个新问题:中间结果丢失,导致内存泄漏.我可以通过制作一个静态函数来轻松解决这个问题,该函数需要三个A对象引用并将前两个的总和存储在第三个中,但我愿意打赌必须有一些方法来使+重载发生的方式我想要.

所以问题是:有没有什么方法可以使用一系列操作符重载,它们不会在不引起内存泄漏的情况下修改操作数?

c++ memory memory-leaks overloading operator-keyword

6
推荐指数
1
解决办法
5438
查看次数

排序中的<运算符断言无效

我正在尝试实现一个简单的比较器,用于根据数组"_vec"中的值对索引进行排序.我收到"无效<运营商"运行时错误消息.我无法理解以下代码有什么问题:

class Compare{
vector<int>& _vec;
public:
    Compare(vector<int>& vec) : _vec(vec) {}
    bool operator()(size_t i, size_t j){
        if(_vec[i] != _vec[j])
        return _vec[i] < _vec[j];
        else
        return (double)rand()/RAND_MAX < 0.5; 
    }
};
Run Code Online (Sandbox Code Playgroud)

我正在使用以下函数调用:

sort(inds.begin(),inds.end(),Compare(vals));
Run Code Online (Sandbox Code Playgroud)

其中inds只是一个包含索引从1到15(比如说)的数组,而vals是长度为15的数组,其中包含一些我希望计算其排序索引的值.总体目标是在val中的两个(或更多)条目相等时随机化排序顺序.有帮助吗?

c++ sorting assertion operator-keyword

6
推荐指数
1
解决办法
5113
查看次数

Grep OR运算符不工作

我需要帮助Unix.我试图看看两个语句(printf和fprintf)中的一个是否在文件中.我使用了命令:

search=`cat $file | grep -w "fprintf\|printf"`
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它在文件中找不到这两者中的一个存在.为什么?

unix grep operator-keyword

6
推荐指数
1
解决办法
7503
查看次数

将boost :: shared_ptr与重载下标运算符([])的类一起使用

我有一个重载下标运算符的类:

class SomeClass
{
public:

   int& operator[] (const int idx)
   {
      return someArray[idx];
   }  

private:

   int someArray[10];
};
Run Code Online (Sandbox Code Playgroud)

这当然允许我像这样访问someArray成员的数组元素:

SomeClass c;
int x = c[0];
Run Code Online (Sandbox Code Playgroud)

但是,SomeClass的某些实例将包含在boost共享指针中:

boost::shared_ptr<SomeClass> p(new SomeClass);
Run Code Online (Sandbox Code Playgroud)

但是,为了使用下标运算符,我必须使用更详细的语法,这种语法会破坏下标运算符重载的简洁性:

int x = p->operator[](0);
Run Code Online (Sandbox Code Playgroud)

有没有办法以更简单的方式访问下标运算符?

c++ boost shared-ptr subscript operator-keyword

6
推荐指数
1
解决办法
722
查看次数

operator const char*以奇怪的方式覆盖(?)我的另一个变量

#include <iostream>
#include <sstream>

class Vector
{
    double _x;
    double _y;
public:
    Vector(double x, double y) : _x(x), _y(y) {}
    double getX() { return _x; }
    double getY() { return _y; }

    operator const char*()
    {
        std::ostringstream os;
        os << "Vector(" << getX() << "," << getY() << ")";
        return os.str().c_str();
    }
};
int main()
{
    Vector w1(1.1,2.2);
    Vector w2(3.3,4.4);
    std::cout << "Vector w1(" << w1.getX() << ","<< w1.getY() << ")"<< std::endl;
    std::cout << "Vector w2(" << w2.getX() << ","<< …
Run Code Online (Sandbox Code Playgroud)

c++ const overwrite char operator-keyword

6
推荐指数
1
解决办法
424
查看次数

当类成员是引用时,无法生成默认赋值运算符?(在C++中)

这是我的代码:

class NO {
  public:
    NO(std::string& name):nameValue(name){};

  private:
    std::string& nameValue;           // this is now a reference

};

int main(){

  int b=10,c=20;
  int& d=b;
  d=c;

  std::string p="alpha", q="beta";

  NO x(p), y(q);
  x=y;

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

"non-static reference member ‘std::string& NO::nameValue’, can’t use default assignment operator"
Run Code Online (Sandbox Code Playgroud)

当我可以使用内置类型执行相同操作时,为什么不能使用引用成员重新分配对象?

谢谢

c++ default reference variable-assignment operator-keyword

6
推荐指数
1
解决办法
123
查看次数

运算符在python中为__truediv__重载

我试图在python中实现除法运算符的重载.

class Fraction:
    def __init__(self,top,bottom):
        def gcd(m, n):
            while m % n != 0:
                old_m = m
                old_n = n
                m = old_n
                n = old_m % old_n
            return n
        common = gcd(top,bottom)
        self.num = top/common
        self.den = bottom/common
    def __str__ (self):
        return str(self.num) + "/" + str(self.den)
    def get_num(self):
        return self.num
    def get_den(self):
        return self.den
    def __add__(self, other_fraction):
        new_num = self.num * other_fraction.den + self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __sub__(self, other_fraction):
        new_num …
Run Code Online (Sandbox Code Playgroud)

python overloading operator-keyword python-2.7

6
推荐指数
2
解决办法
5558
查看次数

Ruby中的$ /表示什么?

我正在阅读有关Ruby序列化的信息(http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/),并且遇到了以下代码.$ /是什么意思?我假设$指的是一个对象?

 array = []
 $/="\n\n"
 File.open("/home/alan/tmp/blah.yaml", "r").each do |object|
   array << YAML::load(object)
 end
Run Code Online (Sandbox Code Playgroud)

ruby syntax operator-keyword

6
推荐指数
1
解决办法
1859
查看次数