Effective STL 29 istreambuf

xiaoxiao2021-02-28  67

ifstream inputFile("interestingData.txt"); inputFile.unset(ios::skipws); string fileData((istream_iterator<char>(inputFile)), istream_iterator<char>());

istream_iterators use operator>> functions to do the actual reading and, by default, operator>> functions skip whitespace. The operator>> functions on which istream_iterators depend perform formatted input. and that means they have to create and destory sentry objects (special iostream objects that perform setup and cleanup activities for each call to operator>>). they have to check stram flags that might effect their behavior, they have to perform comprehensive checking for read errors. and if they encounter a problem, they have to check the stream’s exception mask to determine whether an exception should be thrown. More efficient approach: istreambuf_iterators

ifstream inputFile("interestingData.txt"); string fileData((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());

more specifically, an istreambuf_iterator <char> object reading form an istream s will call s.rdbuf()->sgetc() to read s’s next character.

转载请注明原文地址: https://www.6miu.com/read-69404.html

最新回复(0)