ifstream如何知道定界符在哪里?

How does ifstream know where is the delimeter

本文关键字:在哪里 定界符 何知道 ifstream      更新时间:2023-10-16

所以当我读取file("animal。txt")时它显示的是

zebra
baboon
orangutan
gorilla
aardvark
lion
tiger
cougar
ocelot
panther
rat
mouse
gerbil
hamster
elephant
rhinoceros
hippopotamus

我想知道ist >> s如何识别分隔符并将长字符串分隔成单独的单词。我在下面提供了一个文本和我的实现。

animal.txt

zebrababoonorangutangorillaaardvarkliontigercougarocelotpantherratmousegerbilhamsterelephantrhinoceroshippopotamus

SortedList readFile(string infile)
{
      SortedList result;
      string s;
      ifstream ist(infile.c_str()); // open file
      // Check if file opened correctly
      if(ist.fail()) throw runtime_error("file not found");
      // Read file into list
      while(ist >> s){
          cout<< s << endl;
          cout << ist << endl;
            result.insert(s);
      }
      return result;
}

operator>>在左边应用于流,右边应用于字符串。

将从流中读取一个以"空格"分隔的单词到字符串中。

  1. 读取并忽略字符,直到issapce()为假。
  2. 读取并存储字符串字符,直到isspace()为真。