<<运算符重写为 cout int 和 double 值

发布于 2024-10-06 19:32:01 字数 1049 浏览 0 评论 0 原文

我需要重写<<运算符,以便它可以计算小时(int)和温度(double)的值。

我想我已经包含了所有必要的部分。提前致谢。

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }
    bool operator<(const Reading &r) const;
};

================

ostream& operator<<(ostream& ost, const Reading &r)
{
    // unsure what to enter here

    return ost;
}

vector<Reading> get_temps()
{
// stub version                                                                 
    cout << "Please enter name of input file name: ";
    string name;
    cin >> name;
    ifstream ist(name.c_str());
    if(!ist) error("can't open input file ", name);

    vector<Reading> temps;
    int hour;
    double temperature;
    while (ist >> hour >> temperature){
        if (hour <0 || 23 <hour) error("hour out of range");
        temps.push_back( Reading(hour,temperature));
    }

}

I need to rewrite the << operator so that it can cout values for hour (int) and temperature (double).

I think I've included all necessary sections. Thanks in advance.

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }
    bool operator<(const Reading &r) const;
};

========

ostream& operator<<(ostream& ost, const Reading &r)
{
    // unsure what to enter here

    return ost;
}

========

vector<Reading> get_temps()
{
// stub version                                                                 
    cout << "Please enter name of input file name: ";
    string name;
    cin >> name;
    ifstream ist(name.c_str());
    if(!ist) error("can't open input file ", name);

    vector<Reading> temps;
    int hour;
    double temperature;
    while (ist >> hour >> temperature){
        if (hour <0 || 23 <hour) error("hour out of range");
        temps.push_back( Reading(hour,temperature));
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

耳钉梦 2024-10-13 19:32:01

例如这样:

bool operator <(Reading const& left, Reading const& right)
{
    return left.temperature < right.temperature;
}

并且它应该是一个全局函数(或与 Reading 位于同一命名空间中),而不是成员或 Reading ,它应该声明为 friend 如果您打算拥有任何受保护或私人成员。这可以像这样完成:

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }

    friend bool operator <(Reading const& left, Reading const& right);
};

For example like this:

bool operator <(Reading const& left, Reading const& right)
{
    return left.temperature < right.temperature;
}

And it should be a global function (or in the same namespace as Reading), not a member or Reading, it should be declared as a friend if you going to have any protected or private members. This could be done like so:

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }

    friend bool operator <(Reading const& left, Reading const& right);
};
我不吻晚风 2024-10-13 19:32:01

你可能想要这样的东西

ost << r.hour << ' ' << r.temperature;

,尽管这是非常简单的东西,如果它没有意义,你真的应该和某人谈谈或买一本书。

如果它仍然没有意义或者你不想被打扰,请考虑选择另一个爱好/职业。

You probably want something like

ost << r.hour << ' ' << r.temperature;

This is pretty simple stuff though, and if it doesn't make sense you should really talk to someone or get a book.

And if it still doesn't make sense or you can't be bothered, consider choosing another hobby/career.

北风几吹夏 2024-10-13 19:32:01

IIRC,您可以通过两种方式之一执行此操作...

// overload operator<
bool operator< ( const Reading & lhs, const Reading & rhs )
{
  return lhs.temperature < rhs.temperature;
}

或者,您可以将运算符添加到您的结构中...

struct Reading {
  int hour;
  double temperature;
  Reading ( int h, double t ) : hour ( h ), temperature ( t ) { }
  bool operator< ( const Reading & other ) { return temperature < other.temperature; }
}

IIRC, you can do it one of two ways ...

// overload operator<
bool operator< ( const Reading & lhs, const Reading & rhs )
{
  return lhs.temperature < rhs.temperature;
}

or, you can add the operator to your struct ...

struct Reading {
  int hour;
  double temperature;
  Reading ( int h, double t ) : hour ( h ), temperature ( t ) { }
  bool operator< ( const Reading & other ) { return temperature < other.temperature; }
}
メ斷腸人バ 2024-10-13 19:32:01

在运算符 << 中使用 ost 参数,如 std::cout。

Use ost parameter like std::cout in operator<<.

浅唱ヾ落雨殇 2024-10-13 19:32:01
r.hour()
r.temperature()

您已将 hourTemperature 声明为 Reading 的成员字段,而不是成员方法 >。因此,它们只是 r.hourr.Temperature(没有 ())。

r.hour()
r.temperature()

You've declared hour and temperature as member fields of Reading, not member methods. Thus they are simply r.hour and r.temperature (no ()).

苍白女子 2024-10-13 19:32:01

由于小时和温度是变量而不是函数,因此只需从 operator<< 函数中删除尾随的 () 即可。

As hour and temperature are variables rather than functions, just remove the trailing () from the operator<< functions.

贱贱哒 2024-10-13 19:32:01

您可以在 C++ 中重载这样的运算符。

struct Reading {
     int hour;
     double temperature;
     Reading(int h, double t): hour(h), temperature(t) { }
     bool operator<(struct Reading &other) {
         //do your comparisons between this and other and return a value
     }
}

You can overload an operator like this in c++.

struct Reading {
     int hour;
     double temperature;
     Reading(int h, double t): hour(h), temperature(t) { }
     bool operator<(struct Reading &other) {
         //do your comparisons between this and other and return a value
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文