-1

so when i readfile("animal.txt") it gave me

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

i would like to know how ist >> s identify the delimiter and separate the long string into individual words. I provided a txt and my implementation below .

animal.txt


zebrababoonorangutangorillaaardvarkliontigercougarocelotpantherratmousegerbilhamsterelephantrhinoceroshippopotamus

and

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;
}
4

1 回答 1

1

operator>>应用于左侧的流和右侧的字符串时。

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

确切地说,它将:

  1. 读取并忽略字符,直到issapce()为假。
  2. 读取并存储字符串中的字符,直到isspace()为真。
于 2013-05-21T02:13:55.937 回答