覆盖运算符>>对于 Strings 类

发布于 2024-10-01 14:15:42 字数 798 浏览 5 评论 0原文

我有一个简单的问题。我需要重写运算符>>对于自定义 String 类,我不太清楚该怎么做。

我知道这段代码是有效的,因为这是我解决问题的原始方法:

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];  //BUFF_INC is predefined
  is >> data;
  delete &s;
  s = data;
  return s;
}

但是,根据规范(这是一项家庭作业),我需要一次读入字符 1 来手动检查空格并确保该字符串对于 data[] 来说不算太大。因此,我将代码更改为以下内容:

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];
  int idx = 0;
  data[ 0 ] = is.get();
  while( (data[ idx ] != *String::WHITESPACE) && !is.ios::fail() ) {
    ++idx;
    is.get();
    data[ idx ] = s[ idx ];
  }
  return is;
}

但是,当执行此新代码时,它只会陷入用户输入的循环中。那么如何使用 is.get() 逐个字符地读入数据而不等待更多的用户输入呢?或者我应该使用 .get() 以外的其他东西?

I just have a quick question. I need to override the operator >> for a custom String class and I can't quite figure out how to do it.

I know that this code works, because it was my original method of solving the problem:

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];  //BUFF_INC is predefined
  is >> data;
  delete &s;
  s = data;
  return s;
}

However, according to the spec (this is a homework assignment), I need to read in the characters 1 at a time to manually check for whitespace and ensure that the string isn't too big for data[]. So I changed my code to the following:

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];
  int idx = 0;
  data[ 0 ] = is.get();
  while( (data[ idx ] != *String::WHITESPACE) && !is.ios::fail() ) {
    ++idx;
    is.get();
    data[ idx ] = s[ idx ];
  }
  return is;
}

When this new code is executed however it just gets stuck in a loop of user input. So how do I use is.get() to read in the data character by character but not wait for more user input? Or should I perhaps be using something other than .get()?

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

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

发布评论

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

评论(2

涙—继续流 2024-10-08 14:15:42

您似乎没有对从流中获取的字符执行任何操作,

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];
  int idx = 0;
  data[ 0 ] = is.get();
  while( (data[ idx ] != *String::WHITESPACE) && !is.ios::fail() ) {
    ++idx;
    is.get();              // you don't do anything with this
    data[ idx ] = s[ idx ]; // you're copying the string into the buffer
  }
  return is;
}

因此它检查字符串 s 是否包含空格,而不是您是否从流中读取空格。

You don't seem to be doing anything with the character you get from the stream

istream& operator>>(istream &is, String &s) {
  char data[ String::BUFF_INC ];
  int idx = 0;
  data[ 0 ] = is.get();
  while( (data[ idx ] != *String::WHITESPACE) && !is.ios::fail() ) {
    ++idx;
    is.get();              // you don't do anything with this
    data[ idx ] = s[ idx ]; // you're copying the string into the buffer
  }
  return is;
}

So it checks whether the string s contains a whitespace, not whether you read a whitespace from the stream.

潜移默化 2024-10-08 14:15:42

尝试:

istream& operator>>(istream &is, String &s)
{
    std::string  buffer;
    is >> buffer;           // This reads 1 white space separated word.

    s.data = buffer.c_str();
    return is;
}

评论您的原始代码:

istream& operator>>(istream &is, String &s)
{
  char data[ String::BUFF_INC ];
  is >> data;   // Will work. But prone to buffer overflow.


  delete s;    // This line is definately wrong.
               // s is not a pointer so I don;t know what deleting it would do.

  s = data;    // Assume assignment operator is defined.
               // for your class that accepts a C-String
  return s;
}

使用第二个版本作为基础:

istream& operator>>(istream &is, String &s)
{
  std::vector<char> data;

  char first;
  // Must ignore all the white space before the word
  for(first = is.get(); String::isWhiteSpace(first) && is; first = is.get())
  {}

  // If we fond a non space first character
  if (is && !String::isWhiteSpace(first))
  {
      data.push_back(first);
  }


  // Now get values while white space is false
  char next;
  while( !String::isWhiteSpace(next = is.get()) && is)
  {
      // Note we test the condition of the stream in the loop
      // This is because is.get() may fail (with eof() or bad()
      // So we test it after each get.
      //
      // Normally you would use >> operator but that ignores spaces.
      data.push_back(next);
  }
  // Now assign it to your String object
  data.push_back('\0');
  s.data = data;
  return is;
}

Try:

istream& operator>>(istream &is, String &s)
{
    std::string  buffer;
    is >> buffer;           // This reads 1 white space separated word.

    s.data = buffer.c_str();
    return is;
}

Commenting on your original code:

istream& operator>>(istream &is, String &s)
{
  char data[ String::BUFF_INC ];
  is >> data;   // Will work. But prone to buffer overflow.


  delete s;    // This line is definately wrong.
               // s is not a pointer so I don;t know what deleting it would do.

  s = data;    // Assume assignment operator is defined.
               // for your class that accepts a C-String
  return s;
}

Using the second version as a base:

istream& operator>>(istream &is, String &s)
{
  std::vector<char> data;

  char first;
  // Must ignore all the white space before the word
  for(first = is.get(); String::isWhiteSpace(first) && is; first = is.get())
  {}

  // If we fond a non space first character
  if (is && !String::isWhiteSpace(first))
  {
      data.push_back(first);
  }


  // Now get values while white space is false
  char next;
  while( !String::isWhiteSpace(next = is.get()) && is)
  {
      // Note we test the condition of the stream in the loop
      // This is because is.get() may fail (with eof() or bad()
      // So we test it after each get.
      //
      // Normally you would use >> operator but that ignores spaces.
      data.push_back(next);
  }
  // Now assign it to your String object
  data.push_back('\0');
  s.data = data;
  return is;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文