小编Nat*_*nax的帖子

什么时候JavaScript字符串不是字符串?

可能重复:
为什么("foo"=== new String("foo"))在JavaScript中评估为false?

这里我发现了最好使用非类型强制字符串比较的建议,但在Chrome中,我发现了一些奇怪的东西:

var t1 = String("Hello world!");
var t2 = new String("Hello world!");
var b1 = (t1==t2); // true
var b2 = (t1===t2); // false
Run Code Online (Sandbox Code Playgroud)

这是标准行为吗?如果是这样,t1和t2各自的类型是什么?谢谢.

javascript

8
推荐指数
3
解决办法
2013
查看次数

C++可以像ML案例表达式那样做吗?

所以,我在C++中遇到过几次这样的事情,我真的很喜欢写这样的东西

case (a,b,c,d) of
     (true, true, _, _ )     => expr
   | (false, true, _, false) => expr
   | ...
Run Code Online (Sandbox Code Playgroud)

但是在C++中,我总是得到这样的东西:

bool c11 = color1.count(e.first)>0;
bool c21 = color2.count(e.first)>0;
bool c12 = color1.count(e.second)>0;
bool c22 = color2.count(e.second)>0;
// no vertex in this edge is colored
// requeue
if( !(c11||c21||c12||c22) )
{
    edges.push(e);
}
// endpoints already same color
// failure condition
else if( (c11&&c12)||(c21&&c22) )
{
    results.push_back("NOT BICOLORABLE.");
    return true;
}
// nothing to do: nodes are already
// colored …
Run Code Online (Sandbox Code Playgroud)

c++ pattern-matching switch-statement

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

std :: vector operator ==:值或引用比较?

所以,我试图在codegolf上为C++提供x == x + 2时的解决方案,并想出这个片段只是为了意识到我不知道它是如何工作的.我不确定为什么这两个条件都评估为真.

有人知道标记的行是否line:为真,因为x ==&x或者因为在==的左侧之前评估了x + 2?

#include <iostream>
#include <vector>

std::vector<int>& operator+ ( std::vector<int> &v, int val )
{
    v.push_back(val);
    return v;
}

int main()
{
    std::vector<int> x;
    std::vector<int> y = x + 2; // y is a copy of x, and x is [2]

    // how are both of these are true?
    std::cout << (x==y) << "\n";    // value comparison [2]==[2]
line:
    std::cout << (x==x+2) << "\n";  // reference comparison? &x == …
Run Code Online (Sandbox Code Playgroud)

c++ vector

3
推荐指数
1
解决办法
3498
查看次数