Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++向量的三元条件算子_C++ - Fatal编程技术网

C++向量的三元条件算子

C++向量的三元条件算子,c++,C++,我有一个使用三元运算符的代码。我希望向量只在c>d时执行push_back。否则,什么也不应该发生。我如何实现这一点。但我不应该使用if-else条件。下面是我的代码: int c=30,d=10; std::vector<int> first; (c>d) ? first.push_back(c) : "What should be here"; vector的push_back返回void。如果条件运算符的最后两个操作数之一为void类型且不是抛出

我有一个使用三元运算符的代码。我希望向量只在c>d时执行push_back。否则,什么也不应该发生。我如何实现这一点。但我不应该使用if-else条件。下面是我的代码:

    int c=30,d=10;
    std::vector<int> first;
    (c>d) ? first.push_back(c) : "What should be here";
vector的push_back返回void。如果条件运算符的最后两个操作数之一为void类型且不是抛出表达式,则两者都必须具有void类型。因此:

当然,这远不如它的可读性,实际上比它更冗长

if (c > d) first.push_back(c);

并且不应该在实际代码中使用。

c>d?第一,推回c:无效;用if更好我知道。。。但我的要求是,使用void使程序stuck@vinodchelladurai不,它不是:,你没有展示给我们的程序的其余部分卡住了。我的意思是程序在运行时停止了。我同意。IMHO条件运算符只能用于根据条件再现值,而不能用于任何副作用。
if (c > d) first.push_back(c);