标签: conditional-operator

哪个运算符更快:?:或&&

在开发ASP.NET应用程序时,我经常需要解析以字符串形式给出的布尔值,例如来自查询字符串 ?visible=true

我发现了两种解决方案来实现解析:

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) && b;
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) ? b : false;
    }
}
Run Code Online (Sandbox Code Playgroud)

您如何看待首选哪种方式?可能更快?

PS这不是微观选择,我只是想知道

PPS我不熟悉IL所以决定在这里问

.net c# asp.net boolean conditional-operator

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

是否有更短的方式编写`StringPtr?StringPtr:"null"`?

我有这个代码:

 std::wstringstream outstream;
 outstream << (prop.m_pwszOriginalVolumeName 
             ? prop.m_pwszOriginalVolumeName 
             : L"null") << L";"
           << (prop.m_pwszSnapshotDeviceObject 
             ? prop.m_pwszSnapshotDeviceObject 
             : L"null") << L";"
           << (prop.m_pwszOriginatingMachine 
             ? prop.m_pwszOriginatingMachine
             : L"null") << L";"
           << ... // some more strings here
Run Code Online (Sandbox Code Playgroud)

有没有办法避免代码重复,仍然有简洁的代码?

c++ conditional-operator

4
推荐指数
1
解决办法
578
查看次数

为什么以及如何通过编译器对这两个条件进行不同的处理?

以下两个代码示例表示相同的逻辑.检查字符串是否为null并根据该检查进行分支.第一个样本安全编译.第二个产生与Java泛型相关的类型不匹配错误.我的问题似乎很简单,但它让我望而却步.为什么编译器对这两个语句的处理方式不同?我怎样才能更好地了解这里发生了什么?

/* compiles cleanly */
protected Collection<String> getUserRoles(Object context,
        Set<String> mappableRoles) {
    String cookieValue = extractCookieValue(context);
    if (cookieValue != null) {
        return securityService.getRolesForUser(cookieValue);
    } else {
        return Collections.emptySet();
    }
}


/* produces a compiler error */
protected Collection<String> getUserRoles(Object context,
            Set<String> mappableRoles) {
    String cookieValue = extractCookieValue(context);
    return cookieValue == null ? Collections.emptySet()
            : securityService.getRolesForUser(cookieValue);
}
Run Code Online (Sandbox Code Playgroud)

来自Eclipse的编译器错误.

Type mismatch: cannot convert from Set<capture#1-of ? extends Object> to Collection<String>

根据要求,这是SecurityService接口的相关部分.

public interface SecurityService {
    public Set<String> getRolesForUser(String userId);
}
Run Code Online (Sandbox Code Playgroud)

java generics type-inference conditional-operator type-bounds

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

Perl三元条件运算符

我正在尝试在脚本中编写更高效的代码,并且有时会实现三元条件运算符.在循环中使用三元条件运算符时,我无法理解为什么会得到额外的结果:

#!/usr/bin/perl

use strict;
use warnings;

my @array = ('Serial = "123"', 'Serial = "456"', 'Serial = "789"');
my ($test1,$test2);
foreach my $a (@array){
        !$test1 ? $test1 = $a  : $test1 .= " AND " . $a;
}
foreach my $b (@array){
        if (!$test2) {
                $test2 = $b
        } else {
                $test2 .= " AND " . $b;
        }
}
print "Test1: $test1\n";
print "Test2: $test2\n";
Run Code Online (Sandbox Code Playgroud)

输出:

~/bin/test.pl
Test1: Serial = "123" AND Serial = "123" AND Serial = "456" …
Run Code Online (Sandbox Code Playgroud)

perl conditional-operator operator-precedence

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

?:LINQ中的条件运算符未按预期工作

我遇到了一个LINQ查询问题,所以我在LINQPad中为它做了一个简化版本来帮助我.问题是,我不明白为什么它仍然没有做我认为应该做的事情......

var list = "1 2 3 4".Split();
var result = list.FirstOrDefault(x =>
                    x == "3"
                    && true);
result.Dump();
Run Code Online (Sandbox Code Playgroud)

3就像人们想象的那样回馈.
但是,当我运行这个时:

var list = "1 2 3 4".Split();
var result = list.FirstOrDefault(x =>
                    x == "3"
                    && false ? false : true);
Run Code Online (Sandbox Code Playgroud)

1回来了.最后一行是实际代码的简化.这两个例子都应该给出true最后一行,它会返回3,但是带有条件运算符的查询会在那里引发一个扭结.

我错过了什么?

c# linq conditional conditional-operator

4
推荐指数
1
解决办法
1639
查看次数

条件运算符可以返回引用?

我遇到了一行代码,从未想过它可能会运行良好.我认为条件运算符返回值并且不能与引用一起使用.

一些伪代码:

#include <iostream>

using namespace std;

class A {
public:
  A(int input) : v(input) {};
  void print() const { cout << "print: " << v  << " @ " << this << endl; }
  int v;
private:
  //A A(const A&);
  //A operator=(const A&);
};

class C {
public:
  C(int in1, int in2): a(in1), b(in2) {}
  const A& getA() { return a;}
  const A& getB() { return b;}
  A a;
  A b;
};

int main() {
  bool test = false; …
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator

4
推荐指数
1
解决办法
154
查看次数

三元C算子如何解释空的第一个分支?为什么?

这似乎是在没有警告的情况下编译和运行gcc -Wall(4.9.2)

#include <stdio.h>
int main(int argc, char **argv){
    printf("%d\n", argc-1?:42);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我运行它

  • 用0 args(制作argc-1评价false),它打印42;
  • 当n> = 1 args(进行argc-1评估true)时,它打印n-1.

如果我认为x?:y在这种情况下相当于x?1:y?我是对的吗?这是标准的,预期的行为,还是只是GCC的怪癖?

c gcc ternary-operator conditional-operator

4
推荐指数
1
解决办法
140
查看次数

条件运算符"?:"

我昨天在C完成了编程考试.有一个问题我无法回答,即使我今天研究过,我也无法想出一个解决方案.

所以我们有这个:

int A= -1 , B= -2, C= -3, X=1;
X = B != C ? A=(~C) - A-- : ++C + (~A);
printf("A= %d  B= %d  C =%d  X=%d \n", A,B,C,X);
Run Code Online (Sandbox Code Playgroud)

我知道这个运算符函数if if X = B != C为true然后A=(~C) - A--执行.如果它是假的,++C + (~A)则执行.

任何人都可以告诉我并解释A,B,C和X的值是printf什么?

这包含在一个问题中,要求对整个程序进行"跟踪":

     #include <stdio.h>
            void main(){
            int A= -1 , B= -2, C= -3, X=1;

        X = B != C ? A=(~C) - A-- : ++C + (~A); …
Run Code Online (Sandbox Code Playgroud)

c conditional-operator

4
推荐指数
1
解决办法
904
查看次数

PHP短if语句带有继续关键字并省略else部分

我有一个for循环

for ($x=1; $x<=5; $x++){
    ($x == 3)? continue : true;
    //some code here
}
Run Code Online (Sandbox Code Playgroud)

现在执行我得到错误

PHP解析错误:语法错误,第21行/var/www/html/all.php中的意外"继续"(T_CONTINUE)

现在,这给我留下两个问题:

  1. 我可以在short if语句中使用continue key word吗?

  2. 对于short if的else部分,可以使用像true或false这样的二进制值,如果没有,那么如果我对else部分无关,我怎么能使用short if语句.

php conditional-operator

4
推荐指数
1
解决办法
219
查看次数

为什么"真实或真实与虚假"似乎同时真实和错误?

我得到以下内容:

puts true or true and false
# >> true
Run Code Online (Sandbox Code Playgroud)

而我也得到:

if true or true and false
  puts "that's true!"
else
  puts "that's false!"
end
# >> that's false!
Run Code Online (Sandbox Code Playgroud)

为什么true or true and false两者都truefalse(像薛定谔的猫一样)?

ruby conditional-operator operator-precedence

4
推荐指数
1
解决办法
606
查看次数