标签: operator-keyword

C++ const std :: map引用无法编译

是否有理由将参数传递给std::mapas作为导致[]运算符中断?当我使用const时,我得到这个编译器错误(gcc 4.2):

错误:'map [name]'中'operator []'不匹配

这是函数原型:

void func(const char ch, std::string &str, const std::map<std::string, std::string> &map);
Run Code Online (Sandbox Code Playgroud)

而且,我应该提一下,当我删除const前面的关键字时没有问题std::map.

如果我被正确指示,[]运算符实际上会在地图中找到一个新对,如果找不到密钥,这当然可以解释为什么会发生这种情况,但我无法想象这会是可接受的行为

如果有更好的方法,比如使用find代替[],我会很感激.我似乎无法找到工作,虽然...我收到const不匹配的迭代器错误.

c++ stdmap std find operator-keyword

15
推荐指数
2
解决办法
1万
查看次数

参考类型转换操作符:要求麻烦?

当我使用编译以下代码时 g++

class A {};

void foo(A&) {}

int main()
{
  foo(A());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

> g++ test.cpp -o test     
test.cpp: In function ‘int main()’:
test.cpp:10: error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘A’
test.cpp:6: error: in passing argument 1 of ‘void foo(A&)’
Run Code Online (Sandbox Code Playgroud)

经过一番反思,这些错误对我来说很有意义.A()只是一个临时值,而不是堆栈中的可分配位置,因此它似乎没有地址.如果它没有地址,那么我就不能对它进行引用.好的.

可是等等!如果我将以下转换运算符添加到类中A

class A
{
public:
  operator A&() { return *this; }
};
Run Code Online (Sandbox Code Playgroud)

一切都很好!我的问题是这是否远程安全.什么时候this指向何时A()构建为临时值?

事实上,我对此充满信心

void foo(const A&) {}
Run Code Online (Sandbox Code Playgroud)

可以根据g++我使用过的所有其他编译器接受临时值.该 …

c++ reference operators type-conversion operator-keyword

14
推荐指数
2
解决办法
1万
查看次数

在C++中实现operator <

我有一个包含几个数字字段的类,例如:

class Class1 {
    int a;
    int b;
    int c;
public:
    // constructor and so on...
    bool operator<(const Class1& other) const;
};
Run Code Online (Sandbox Code Playgroud)

我需要使用这个类的对象作为一个键std::map.因此,我实施operator<.operator<在这里使用最简单的实现是什么?

编辑:<只要任何字段不相等 ,就可以假设其含义以保证唯一性.

编辑2:

一个简单的实现:

bool Class1::operator<(const Class1& other) const {
    if(a < other.a) return true;
    if(a > other.a) return false;

    if(b < other.b) return true;
    if(b > other.b) return false;

    if(c < other.c) return true;
    if(c > other.c) return false;

    return false;
}
Run Code Online (Sandbox Code Playgroud)

这篇文章背后的全部原因只是我发现上面的实现过于冗长.应该有更简单的东西.

c++ operator-overloading operators operator-keyword

14
推荐指数
3
解决办法
8006
查看次数

C++将自定义类型的转换运算符重载为std :: string

我希望有人能够回答为什么以下不起作用.虽然忍受我,我仍然是一个菜鸟......我只是无法深究为什么以下

using namespace std;
#include <string>
#include <iostream>

class testClass
{
public:
 operator char* () {return (char*)"hi";};
 operator int ()  {return 77;};
 operator std::string  () {return "hello";};
};

int main()
{
 char* c;
 int i;
 std::string s = "goodday";

 testClass t;

 c = t;
 i = t;
 s = t;

 cout<< "char: " << c << " int: " << i << " string: "<<s<<endl;

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

给我一个编译时错误:

myMain.cpp: In function ‘int main()’:
myMain.cpp:23: error: ambiguous overload for ‘operator=’ …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

14
推荐指数
3
解决办法
2万
查看次数

关于Haskell中〜和@运算符的问题

他们究竟做了什么?我知道可能使用@(在模式匹配开始时指定一个名字),但是在〜上找不到任何东西.

我在下面的代码片段中找到了它们,取自http://www.haskell.org/haskellwiki/Prime_numbers,但是本文假设您能够熟练使用Haskell语法并且不打算解释其深奥的操作符(第一部分) '混淆是筛子宣言的开始):

primesPT () = 2 : primes'
  where 
    primes' = sieve [3,5..] primes' 9
    sieve (p:xs) ps@ ~(_:t) q
       | p < q   = p : sieve xs ps q
       | True    =     sieve [x | x<-xs, rem x p /= 0] t (head t^2)
Run Code Online (Sandbox Code Playgroud)

关于这里使用的语法的任何解释(或链接到一个)将不胜感激.

primes haskell sieve operator-keyword

14
推荐指数
2
解决办法
457
查看次数

布尔运算符优先级

我想知道编程语言中的运算符优先级是否取决于实现,或者是否存在所有语言都遵循的固定规则.如果可能,您可以先订购具有最高优先级的以下运算符:AND,OR,NOT,XOR.

boolean operator-precedence operator-keyword

14
推荐指数
2
解决办法
2万
查看次数

php - 问号冒号运算符的含义

是什么?:在这条线是什么意思?

$_COOKIE['user'] ?: getusername($_COOKIE['user']);
Run Code Online (Sandbox Code Playgroud)

谢谢.

php operator-keyword

14
推荐指数
1
解决办法
2万
查看次数

C++ - 重载[]运算符

我有一个模板类Array:

template <class T=int, int SIZE=10>
class Array {
    T TheArray[SIZE];
public:
    void Initialize() {
        for (int idx=0; idx < SIZE; idx++) {
            TheArray[idx] = T();
        }
    }

    T& operator [](int idx) {
        return TheArray[idx];
    }

    T operator [](int idx) const {
        return TheArray[idx];
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一些关于运算符[]重载的问题(我在网上找到了这个例子).

我理解T& operator [](int idx)返回引用带索引的数组值idxT operator [](int idx) const返回其值.但是,我不确定在哪种情况下使用[]运算符将返回引用或值.

另外,如果我改变T operator [](int idx) const- > T operator [](int idx),编译器会抱怨.这是为什么?我可以理解编译器抱怨因为只有返回类型不同,但是为什么它在const添加时不会抱怨?这只意味着修改了类的内部,对吧? …

c++ overloading operator-overloading operator-keyword

14
推荐指数
2
解决办法
4万
查看次数

Prolog GNU - Univ运营商?解释它

所以univ运营商.我并不完全理解.

例如:

foo(PredList,[H|_]) :- bar(PredList,H).
foo(PredList,[_|T]) :- foo(PredList,T),!.

bar([H|_],Item) :- G =.. [H,Item],G.
bar([_|T],Item) :- bar(T,Item).
Run Code Online (Sandbox Code Playgroud)

这是做什么的?这样可以查看另一个谓词是否为真.我不明白".."的作用.

如果没有univ运算符,你会如何重写它?

prolog backtracking operator-keyword meta-predicate

13
推荐指数
1
解决办法
4829
查看次数

C#中的按位"〜"运算符

考虑这个单元测试代码:

    [TestMethod]
    public void RunNotTest()
    {

        // 10101100 = 128 + 32 + 8 + 4 = 172
        byte b = 172;

        // 01010011 = 64 + 16 + 2 + 1 = 83
        Assert.AreEqual(83, (byte)~b);
    }
Run Code Online (Sandbox Code Playgroud)

这个测试通过.但是,如果没有字节转换,它将失败,因为"〜"运算符返回值-173.为什么是这样?

c# binary bit-manipulation operator-keyword

13
推荐指数
1
解决办法
1939
查看次数