c++ - Using Insert Iterators when reading from file -


Do you use inserted Iterators while reading from the file to insert the data into the STL container Can you?

For example:

  file * stream; Fread (back_inserter (std :: list), size (int), 1, stream);  

C ++ streams are not compatible with C stdio streams. In other words, you can not use C ++ iterators with FILE * or fread . However, if you use the C ++ std :: fstream features with istream_iterator , you can one C ++ Use an entry iterator to put in the container. Assume that you have the input file "input.txt" in which ASCII text numbers are different from white space, you can do this:

  #include & Lt; Iostream & gt; #include & lt; Fstream & gt; # Include & lt; Vector & gt; # Include & lt; Iterator & gt; Int main () {std :: ifstream ifs ("input.txt"); Std :: vector & lt; Integer & gt; Vec; // disk std :: copy (std :: istream_iterator & lt; int & gt; (ifs), std :: istream_iterator & lt; int & gt; (), std :: back_inserter (vec)); // Now integer std :: copy (vec.begin (), vec.end (), std :: ostream_iterator  (std :: cout, "\ n")) output output; }  

Comments