C++ 使用三元运算符写入流?在C++;

C++ 使用三元运算符写入流?在C++;,c++,stream,ternary-operator,C++,Stream,Ternary Operator,我有几个要点是双重的。我只想在它们不是0时将它们写入流中。我还要说它是哪一个,像这样: << ( (x1 != 0) ? ( "x1=\"" << x1 << '"' ) : ' ') ) << ( (y1 != 0) ? ( "y1=\"" << y1 << '"' ) : ' ') ) #include <iostream> int main() { int a = 0;

我有几个要点是双重的。我只想在它们不是
0
时将它们写入流中。我还要说它是哪一个,像这样:

    << ( (x1 != 0) ?  ( "x1=\"" << x1 << '"' )  : ' ') )
    << ( (y1 != 0) ?  ( "y1=\"" << y1 << '"' )  : ' ') )
#include <iostream>

int main() 
{
    int a = 0;
    int b = 1;
    [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, a);
    [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, b) << std::endl;
}
x1=“value”

y1=“value”
等等

有没有办法做到这一点:

    << ( (x1 != 0) ?  ( "x1=\"" << x1 << '"' )  : ' ') )
    << ( (y1 != 0) ?  ( "y1=\"" << y1 << '"' )  : ' ') )
#include <iostream>

int main() 
{
    int a = 0;
    int b = 1;
    [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, a);
    [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, b) << std::endl;
}

该代码将不会如图所示工作。
?:
运算符的两个操作数必须计算为相同的数据类型。在您的示例中:


  • 左操作数甚至不是有效代码。类似于
    (“x1=\”的表达式您不能使用
    您可以调用使用lambda的流操纵器:

    #include <functional>
    #include <iostream>
    
    struct Manipulator {
        using function_type = std::function<void (std::ostream&)>;
        function_type function;
        Manipulator(function_type function) : function(function) {}
    };
    
    inline std::ostream& operator << (std::ostream& stream, const Manipulator& manipulator) {
        manipulator.function(stream);
        return stream;
    }
    
    int main() {
        int a = 0;
        int b = 1;
        std::cout
            << Manipulator([a](std::ostream& stream) { if(a) stream << "a=\"" << a << '"'; })
            << Manipulator([b](std::ostream& stream) { if(b) stream << "b=\"" << b << '"'; })
            << '\n';
    }
    
    #包括
    #包括
    结构操纵器{
    使用function_type=std::function;
    函数型函数;
    操纵器(函数类型函数):函数(函数){
    };
    
    内联std::ostream&operator这里是@Dieter Lückk方法的简化版本

    您可以像这样使用lambda:

        << ( (x1 != 0) ?  ( "x1=\"" << x1 << '"' )  : ' ') )
        << ( (y1 != 0) ?  ( "y1=\"" << y1 << '"' )  : ' ') )
    
    #include <iostream>
    
    int main() 
    {
        int a = 0;
        int b = 1;
        [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, a);
        [](std::ostream& stream, int x)->std::ostream& { if(x) stream << "b=\"" << x << '"'; return stream;}(std::cout, b) << std::endl;
    }
    
    #包括
    int main()
    {
    int a=0;
    int b=1;
    
    [](std::ostream&stream,int x)->std::ostream&{if(x)stream你试过运行这个程序并得到不受欢迎的行为吗?我觉得很好…当有疑问时,按play并看看它是否有效…实际上,我看到了一个问题:是“
    ”(“x1=\”是的,它用红色覆盖了
    “x1=\”
    部分并说“错误:表达式必须具有整型或非作用域枚举类型“@RSahu,所以我想我必须使用
    if
    else
    语句,对吗?不仅仅是
    (“x1=\”)@RSahu这个答案没有使用parenthasis,所以它更像我的答案。@assimilator,是的。我在OP的问题中谈到了代码。可能有点过火了,但你……你可以这样做……也许在某些情况下它会有用,尽管我想不出有什么异常