Find content in the file stream with std :: find MYTHMA
In general, I rarely use iteratory istream_iterator. Recently, when writing a function of reading a specific format file, find the use of iStream_Iterator and std :: Find can be very convenient to implement the file content.
Note: All of the following assumes that the text file, the binary is not tested.
Assume that something to find is a String in a file, which can be implemented:
IFStream Fin
"
Yourfile
"
);
IF
(FIN)
{Istream_iterator
As can be seen from the code above, the FIN can be seen as a container that stores const string.
Exterior:
1. When can I consider using ISTREAM_ITERATOR?
I think that when the file is the same data, ISTREAM_ITERATOR can be easily read and find an object.
1) If the content in the file is the same basic type of data, such as int, you can use iStream_iterator:
iStream_iterator
<
int
>
Intreader (FIN);
//
FILE BEGIN
iStream_iterator
<
int
>
Inteof;
//
File end
2) If it is stored in the data order of a custom CUSER object, you need to overload Operator >>, then use this:
iStream_iterator
<
CUSER
>
Intreader (FIN);
//
FILE BEGIN
iStream_iterator
<
CUSER
>
Inteof;
//
File end
If you need to find objects, you also need to overload comparison operators in a specific case.
2. Processing in the file is the processing of heterogeneous data. First, all content in the file can be used to operate with String, which does not need to be discussed. What happens when reading a file containing non-int data with iStream_Iterator
Using
Namespace
IFSTREAM FIN
"
Test.txt
"
);
IF
(FIN)
{ISTREAM_ITERATOR
--- Test file Test.txt: 1 22 3.4 Haha 44 Hello --- Output: 1 22 33, Efficiency ISTREAM_ITERATOR In addition to making the code looks relatively simple, everyone is most concerned about the efficiency problem. I have not tested compared.