"C++primer" v5 the 8th Chapter IO Library reading notes Exercise answer

Source: Internet
Author: User

8.1, 8.2

This chapter is not a good thing.

IStream &read (IStream &is) {    int  A;    Auto Old_state=is. Rdstate ();      is . Clear ();     is >>A;      is . SetState (old_state);     return  is ;} int Main () {    read (CIN);     return 0 ;}

8.3

When you read the EOF or the wrong type

8.4

#include <fstream>using namespacestd;intMain () {Ifstreaminch("Ifile.txt"); Vector<int>VEC; intA;  while(inch>>a) vec.push_back (a);  for(auto I:vec) cout<<i<<Endl; return 0;}

8.5

#include <fstream>using namespacestd;intMain () {Ifstreaminch("Ifile.txt"); Vector<string>VEC; strings;  while(inch>>s) vec.push_back (s);  for(auto I:vec) cout<<i<<Endl; return 0;}

8.6, 8.7, 8.8

Temporary:

8.9

#include <sstream>using namespaceStd;istream&read (IStream & is){    strings; Auto Old_state= is. Rdstate ();  is. Clear ();  while( is>>s) cout<<s<<Endl;  is. SetState (old_state); return  is;}intMain () {strings;    Getline (cin,s); Istringstream is(s); Read ( is); return 0;}

8.10

#include <fstream>#include<sstream>using namespacestd;intMain () {FStreaminch("Ifile.txt"); strings; Vector<string>VEC;  while(Getline (inch, s))    {Vec.push_back (s); }     for(Auto &C:vec)        {Istringstream isout (c); stringWord;  while(isout>>word) cout<<word<<Endl; }    return 0;}

8.11

Using the clear and STR two member functions

#include <fstream>#include<sstream>using namespacestd;intMain () {FStreaminch("Ifile.txt"); strings; Vector<string>VEC;  while(Getline (inch, s))    {Vec.push_back (s);    } Istringstream isout;  for(intI=0; I<vec.size (); ++i) {isout.clear ();        Isout.str (Vec[i]); stringWord;  while(isout>>word) cout<<word<<Endl; }    return 0;}

8.12

?

8.13

Temporary:

"C++primer" v5 the 8th Chapter IO Library reading notes Exercise answer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.