Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'thinking in c plus plus (p22)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | ostream_iterator string cout n This example was suggested by Nathan Myers who invented the istreambuf_iterator and its relatives. This iterator extracts information character-by-character from a stream. Although the istreambuf_iterator template argument might suggest to you that you could extract for example ints instead of char that s not the case. The argument must be of some character type - a regular char or a wide character. After the file is open an istreambuf_iterator called p is attached to the istream so characters can be extracted from it. The set string called wordlist will be used to hold the resulting words. The while loop reads words until the end of the input stream is found. This is detected using the default constructor for istreambuf_iterator which produces the past-the-end iterator object end. Thus if you want to test to make sure you re not at the end of the stream you simply say p end. The second type of iterator that s used here is the insert_iterator which creates an iterator that knows how to insert objects into a container. Here the container is the string called word which for the purposes of insert_iterator behaves like a container. The constructor for insert_iterator requires the container and an iterator indicating where it should start inserting the characters. You could also use a back_insert_iterator which requires that the container have a push_back string does . After the while loop sets everything up it begins by looking for the first alpha character incrementing start until that character is found. Then it copies characters from one iterator to the other stopping when a non-alpha character is found. Each word assuming it is nonempty is added to wordlist. StreamTokenizer a more flexible solution The above program parses its input into strings of words containing only alpha characters but that s still a special case compared to the generality of strtok . What we d like now is an actual replacement for strtok so we re never tempted