C++ 为什么输出运算符为';操作系统<&书信电报;价值';而不是';价值>&燃气轮机;操作系统&x27;?

C++ 为什么输出运算符为';操作系统<&书信电报;价值';而不是';价值>&燃气轮机;操作系统&x27;?,c++,C++,我正在学习流媒体。标准流提供了,实际上可以定义 ostream& operator>>(CLASS& rc, ostream& os); 但你必须像这样把它拴起来: a >> (b >> (c >> str)); >操作符是左关联的,因此默认情况下: a >> b >> c >> str; 相当于: ((a >> b) >> c) >> str;

我正在学习流媒体。标准流提供了
,实际上可以定义

ostream& operator>>(CLASS& rc, ostream& os);
但你必须像这样把它拴起来:

a >> (b >> (c >> str));
>
操作符是左关联的,因此默认情况下:

a >> b >> c >> str;
相当于:

((a >> b) >> c) >> str;

这有一个错误的含义。

以下是如何不用担心关联性,通过帮助器类收集输入,然后将其发送到ostream:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

class ReversePrinter
{
    std::string acc;
public:
    template <class T>
    ReversePrinter(const T& value)
    {
        *this >> value;
    }

    template <class T>
    ReversePrinter& operator>>(const T& value)
    {
        std::stringstream ss;
        ss << value;
        acc += ss.str();
        return *this;
    }
    std::ostream& operator>>(std::ostream& os)
    {
        std::reverse(acc.begin(), acc.end());
        return os << acc;
    }
};

struct R
{
    template <class T>
    ReversePrinter operator>>(const T& value) {
        return ReversePrinter(value);
    }
};

int main()
{
    std::string name = "Ben";
    int age = 14;
    const char* hobby = "reading backwards";
    R() >> "Hello, my name is " >> name >> "\nI'm " 
        >> age >> " years old and I like " >> hobby >> std::cout;
}
#包括
#包括
#包括
#包括
类反转器
{
std::字符串acc;
公众:
模板
反向跨距(常数T和值)
{
*这个>>值;
}
模板
反向中断和运算符>>(常数T和值)
{
std::stringstream-ss;
ss>(标准::ostream&os)
{
标准::反向(根据开始(),根据结束());
返回操作系统>(常量T和值){
返回reversePrint(值);
}
};
int main()
{
std::string name=“Ben”;
年龄=14岁;
const char*hobby=“向后阅读”;
R()>>“你好,我的名字是”>>名字>>“\n我是”
>>年龄>>“岁,我喜欢”>>爱好>>性病::库特;
}

+1:的确如此。这肯定是可以做到的;它只是相当愚蠢。@Dalton:因为它是不直观的。这样做是在问bug
1>>cout;
会打印
0
@Tomalak,我是说为什么你说实现>>是非常愚蠢的。@Dalton:你有没有注意到流是如何工作的:
cout>yourName;
是提取。这是一个很好的惯例。@Dalton:因为
注意,你应该使用
类const&rc
。你在使用>>时也是指istream,不是吗?@Martin,谢谢你的加入,请看答案,如果可能的话,给我一个>>的实现,使这种链接工作起来。@Martin:再看一遍问题:)你不想做
acc=ss.str()+acc;
,然后跳过
std::reverse
?否则,它不会把所有的单词都倒排出来吗?
a >> (b >> (c >> str));
a >> b >> c >> str;
((a >> b) >> c) >> str;
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

class ReversePrinter
{
    std::string acc;
public:
    template <class T>
    ReversePrinter(const T& value)
    {
        *this >> value;
    }

    template <class T>
    ReversePrinter& operator>>(const T& value)
    {
        std::stringstream ss;
        ss << value;
        acc += ss.str();
        return *this;
    }
    std::ostream& operator>>(std::ostream& os)
    {
        std::reverse(acc.begin(), acc.end());
        return os << acc;
    }
};

struct R
{
    template <class T>
    ReversePrinter operator>>(const T& value) {
        return ReversePrinter(value);
    }
};

int main()
{
    std::string name = "Ben";
    int age = 14;
    const char* hobby = "reading backwards";
    R() >> "Hello, my name is " >> name >> "\nI'm " 
        >> age >> " years old and I like " >> hobby >> std::cout;
}