c++ - 友好的 cin 和重载操作符 >>

标签 c++

好的,你好。我正在尝试比较坐标类型 (x, y, z) 的两个对象,并且我的代码编译没有错误,但输出不太正确。在我看来,我的输入没有被保存,但我不明白为什么。我只包含了相关定义。

头文件:

#ifndef COORDINATE_H  //if not defined
#define COORDINATE_H //Define

#include <iostream>
using namespace std;

    class Coordinate
    {
          friend istream& operator>>(istream &, Coordinate &);
          friend ostream& operator<<(ostream &, const Coordinate &);
    public:
            Coordinate(double = 0.0, double = 0.0, double = 0.0); //my default constructor
            Coordinate operator+(const Coordinate &);
            Coordinate operator-(const Coordinate &);
            Coordinate operator*(const Coordinate &);
            Coordinate& operator=(const Coordinate &);
            bool operator==(const Coordinate &);
            bool operator!=(const Coordinate &);
            void setCoordinate(double a, double b, double c);
    private:
            double x;
            double y;
            double z;
    };

    #endif //end definition.

定义:

    #include <iomanip>
    #include "Coordinate.h" //including the Coordinate header file
    using namespace std;

    bool Coordinate::operator==(const Coordinate & d)
    {
        return (this->x == d.x && this->y == d.y && this->z == d.z);
    }

    bool Coordinate::operator!=(const Coordinate & d)
    {
        return !(this->x == d.x && this->y == d.y && this->z == d.z);
    }

    Coordinate& Coordinate::operator=(const Coordinate & d)
    {
        if(this != &d)
        {
            x = d.x;
            y = d.y;
            z = d.z;
        }
        return *this;
    }


    ostream &operator<<(ostream & out, const Coordinate & d)
    {
        out << "(" <<d.x << "," << d.y << "," << d.z << ")" << endl;

        return out;
    }

    istream &operator>>(istream & in, Coordinate & g)
            {
        in >> g.x >> g.y >> g.z;
        return in;
    }

最佳答案

如果您希望阅读的格式与您编写的格式相同,则需要明确说明这一点。

可以使用标准 C++ 流来完成:

istream& operator>>(istream& in, Coordinate& g) {
    char c;
    in >> c;        if (c != '(') { ... }
    in >> g.x >> c; if (c != ',') { ... }
    in >> g.y >> c; if (c != ',') { ... }
    in >> g.z >> c; if (c != ')') { ... }
    return in;
}

不幸的是,这种解决方案不能正确回溯或任何无效输入。当然,您可以只设置失败位并继续。要自动检查文字,您可以为 istream& 添加重载并使用 std::ws 显式丢弃空格:

istream& operator>>(istream& in, char c) {
    in >> ws;
    if (in.peek() == c)
        in.ignore();
    else
        in.clear(ios::failbit);
    return in;
}

istream& operator>>(istream& in, Coordinate& g) {
    return in >> '(' >> g.x >> ',' >> g.y >> ',' >> g.z >> ')';
}

如果您需要比这更复杂的东西,我想它会很快变得笨拙。您基本上最终将逐个字符地手动解析输入。到那时,您应该只使用适当的解析库来省去麻烦。

关于c++ - 友好的 cin 和重载操作符 >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10304612/

相关文章:

c++ - 使用 live555 渲染 RTSP H.264 视频流

c++ - obj vector 到成员函数

c++ - 在一次调用中重置使用 iomanip : setprecision, setfill 和标志所做的所有更改的类

c++ - 将类函数与其实际功能分开声明有什么好处?

使用模板定义多个函数的 C++ 通用方法

c++ - 不同于导出的 DLL 函数名称

C++ 用多个字符串和 float 填充一个字符数组

c++ - unordered_map 线程安全

c++ - 什么样的哈希函数可以从字符串中给出 32 位值?

c++ - 段错误 : Bubble sort on link list