标签: conditional-operator

在同一个Perl语句中有条件运算符和后缀条件时会发生什么?

任何人都能解释这条线是如何工作的吗?

return $y < 0 ? - pip2 : pip2 if $x == 0;
Run Code Online (Sandbox Code Playgroud)

如果$y <0它返回-pip2,但它返回时$y >= 0$x != 0

这一行来自这个功能:

sub _atan {
    my( $y, $x ) = @_;

    return $y < 0 ? - pip2 : pip2 if $x == 0;
    return atan( $y / $x );
}
Run Code Online (Sandbox Code Playgroud)

syntax perl conditional-operator

6
推荐指数
3
解决办法
2616
查看次数

ruby idiom:谓词和条件运算符

我喜欢明智地使用三元条件运算符.在我看来,它非常简洁.

但是,在ruby中,我发现我经常测试谓词方法,它们已经有了自己的问号:

some_method( x.predicate? ? foo : bar )
Run Code Online (Sandbox Code Playgroud)

这两个问号彼此如此接近,我感到震惊.是否存在等效的紧凑且可读的替代方案?

ruby predicate ternary-operator conditional-operator

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

C++中的条件运算符还能为我做什么?

使用条件运算符时,我遇到了一个奇怪的编译错误.

a,bint值,以下表达式得到编译错误.

(a>b)?( std::cout << a ) : ( b=MAX );
16 (b <unknown operator> 5)' 

(a>b)?( a=MAX ) : ( std::cout<<b );
16 (&std::cout)->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](b)' 
Run Code Online (Sandbox Code Playgroud)

但这个表达效果很好,这很奇怪..

(a>b)?( std::cout << a ) : ( std::cout<<b );
Run Code Online (Sandbox Code Playgroud)

我不知道是什么造成了这样的差异,并且不知道为什么编译错误代表.这是我的gcc信息:

Reading specs from ./../lib/gcc/mingw32/3.4.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchroniz
ation --enable-libstdcxx-debug
Thread …
Run Code Online (Sandbox Code Playgroud)

c++ gcc conditional-operator

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

如果在matlab中矢量化

我有一个布尔数组调用它flag.

我有两个数字数组ifTrue,ifFalse.所有这些数组都是相同的大小.出于这个问题的目的,假设这些数组中的每个元素都是唯一的.

我想要一个g具有该属性的函数

a = g(flag, ifTrue, ifFalse)

all(flag == (a == ifTrue))
all(~flag == (a == ifFalse))
Run Code Online (Sandbox Code Playgroud)

或者用英语,我想g在if 为true ifTrue时返回元素flag,ifFalseflagfalse 时返回元素.

或者,在matlab中,我可以用循环来做到这一点:

a = zeros(size(ifTrue));
for i = 1 : numel(ifTrue);
    if flag(i)
         a(i) = ifTrue(i)
    else
         a(i) = ifFalse(i)
    end
end
Run Code Online (Sandbox Code Playgroud)

有矢量化的方法吗?

谢谢

matlab vectorization conditional-operator

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

字典上的三元操作

有没有办法在dict上执行三元运算,其中测试是has_key()而没有遇到键错误?即

variable = dict["the_key"] if dict.has_key("the_key") else "something"
Run Code Online (Sandbox Code Playgroud)

(这明显击中了关键错误)

如果没有这个,那么根据一个可能有也可能没有问题的字典来分配一堆变量值的最pythonic方法是什么?重复:

if dict.has_key('key'):
    value = dict['key']
else:
    value = "value"
Run Code Online (Sandbox Code Playgroud)

广告似乎非常不优雅.

python dictionary conditional-operator

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

在h:inputText值和h:commandButton actionListener中使用条件运算符

我想在.xhtml文件中加载两个差异面板.

<h:inputText value="#{param['from']=='TERMINAL' ? terminalsList.globalFilter : merchantsList.globalFilter}" size="50" />
<h:commandButton value="Filter" actionListener="#{param['from']=='TERMINAL' ? terminalsList.filterTerminals : merchantsList.filterMerchants}" />
<h:commandButton value="Reset" actionListener="#{param['from']=='TERMINAL' ? terminalsList.resetTerminalsFilter : merchantsList.resetMerchantsFilter}" />
Run Code Online (Sandbox Code Playgroud)

当http get request params equals'TERMINAL'我想加载'terminalsList'托管bean,否则'merchantsList'托管bean.

这段代码不起作用.

jsf el conditional-operator

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

在inline-if中返回ulong

我有什么理由不能使用以下代码吗?

ulong test(int a, int b)
{
    return a == b ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)

它告诉我:

Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)

以下将有效:

ulong test(int a, int b)
{
    return false ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)

我知道如何解决这个问题.我只是想知道原因.

谢谢.

c# ternary-operator conditional-operator ulong

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

F#条件表达式如果...则...返回单位或()

F#的Condtional表达式需要检查条件,分支为true,分支为false.例如:

let x = 
    if ("hello" = null) 
    then true
    else false //error if else branch missing
Run Code Online (Sandbox Code Playgroud)

然而,当事情会很奇怪unit,又名(),参与.

let y = 
    if ("hello" = null) 
    then raise <| new ArgumentNullException()
    else () //happy with or without else branch
Run Code Online (Sandbox Code Playgroud)

而且更简单:

let z = 
    if ("hello" = null) 
    then ()
    else () //happy with or without else branch
Run Code Online (Sandbox Code Playgroud)

返回else时为什么不需要分支unit

null f# expression conditional-operator nothing

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

没有通过gcc或clang优化函数指针的间接访问-错误或预期的?

给定以下两个功能:

int f() { return 0; }
int g() { return 1; }
Run Code Online (Sandbox Code Playgroud)

以下代码根据布尔值调用其中之一b

int t0(bool b) { return (b ? &f : &g)(); }
int t1(bool b) { return b ? f() : g(); }
int t2(bool b) { return b ? t0(true) : t0(false); }
Run Code Online (Sandbox Code Playgroud)

两者g++ (trunk)clang++ (trunk)-std=c++2a -Ofast -march=native失败来优化下面的代码:

int main(int ac, char**) { return t0(ac & 1); }
Run Code Online (Sandbox Code Playgroud)

产生以下程序集:

main:
  and edi, 1
  mov eax, OFFSET FLAT:f()
  mov …
Run Code Online (Sandbox Code Playgroud)

c++ optimization function-pointers conditional-operator compiler-optimization

6
推荐指数
0
解决办法
154
查看次数

将 std::move shared_ptr 与条件运算符一起使用时的奇怪行为

我正在使用std::moveon编写一些 C++ 代码shared_ptr,并得到了非常奇怪的输出。我简化了我的代码如下

int func(std::shared_ptr<int>&& a) {
    return 0;
}

int main() {
    std::shared_ptr<int> ptr = std::make_shared<int>(1);

    for (int i = 0; i != 10; ++i) {
        func(i == 9 ? std::move(ptr) : std::shared_ptr<int>(ptr));
    }

    if (ptr) {
        std::cout << "ptr is not null: " << *ptr << "\n";
    } else {
        std::cout << "ptr is null\n";
    }

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

我得到了输出

ptr is null
Run Code Online (Sandbox Code Playgroud)

正如我所预期的,我的ptrwill 在最后一个循环中被移动(转换为std::shared_ptr<int>&&),并且由于func从不窃取 中的内存a,所以我的 …

c++ conditional-operator shared-ptr move-constructor move-semantics

5
推荐指数
1
解决办法
153
查看次数