使用IOSTREAM在同一行输出和输入 [英] Output and Input at Same Line using IOSTREAM

查看:111
本文介绍了使用IOSTREAM在同一行输出和输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一个语句中使用 iostream 输入和输出运算符,不是为了用户更好,而是我尝试不成功的外观。

I want to use the iostream input and output operators in the same statement, not to be nicer to the user but the look I was trying not successfully obtained.

代码片段:

int value = 0;
std::cout << "Number 1: " << std::cin >> value << std::endl;

有没有办法只使用cin cout?

Is there any way to do this using only cin cout?

推荐答案

struct IO {
    template <typename T>
    const IO & operator << (const T & t) const {
        std :: cout << t;
        return *this;
    }

    template <typename T>
    const IO & operator >> (T & t) const {
        std :: cin >> t;
        return *this;
    }
};

IO () << "Number 1: " >> value;

这篇关于使用IOSTREAM在同一行输出和输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆