首页 > 代码库 > C++primer 10.4.2节练习

C++primer 10.4.2节练习

练习10.29

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <list>
 8 #include <functional>
 9 #include <iterator>
10 #include <fstream>
11 using namespace std;
12 using namespace placeholders;
13 
14 
15 int main()
16 {
17     vector<string> vec;
18     ifstream in("title.txt");
19     istream_iterator<string> inFile(in);
20     istream_iterator<string> eof;
21     ostream_iterator<string> out_file(cout, " ");
22     while (inFile != eof)
23     {
24         vec.push_back(*(inFile++));
25     }
26     for (auto c : vec)
27         out_file = c;
28     cout << endl;
29     system("pause");
30     return 0;
31 }

练习10.30

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <list>
 8 #include <functional>
 9 #include <iterator>
10 #include <fstream>
11 using namespace std;
12 using namespace placeholders;
13 
14 
15 int main()
16 {
17     vector<int> num;
18     istream_iterator<int> num_cin(cin);
19     istream_iterator<int> eof;
20     ostream_iterator<int> out_file(cout, " ");
21     while (num_cin != eof)
22     {
23         num.push_back(*num_cin++);
24     }
25     sort(num.begin(), num.end());
26     copy(num.begin(), num.end(), out_file);
27     cout << endl;
28     system("pause");
29     return 0;
30 }

练习10.31

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <list>
 8 #include <functional>
 9 #include <iterator>
10 #include <fstream>
11 using namespace std;
12 using namespace placeholders;
13 
14 
15 int main()
16 {
17     vector<int> num;
18     vector<int> num1;
19     istream_iterator<int> num_cin(cin);
20     istream_iterator<int> eof;
21     ostream_iterator<int> out_file(cout, " ");
22     while (num_cin != eof)
23     {
24         num.push_back(*num_cin++);
25     }
26     sort(num.begin(), num.end());
27     unique_copy(num.begin(), num.end(), back_inserter(num1));
28     copy(num1.begin(), num1.end(), out_file);
29     cout << endl;
30     system("pause");
31     return 0;
32 }

 

练习10.32

自己写的程序有错,需要修改,稍后上传

练习10.33

 1 #include<iostream>
 2 #include<string>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <list>
 8 #include <functional>
 9 #include <iterator>
10 #include <fstream>
11 using namespace std;
12 using namespace placeholders;
13 
14 void unraval(ifstream &in, ofstream &out1, ofstream &out2);
15 
16 int main()
17 {
18     ifstream in("test1.txt");
19     ofstream out1("test2.txt", ofstream::app);
20     ofstream out2("test3.txt", ofstream::app);
21     unraval(in, out1, out2);
22     system("pause");
23     return 0;
24 }
25 
26 void unraval(ifstream &in, ofstream &out1, ofstream &out2)
27 {
28     istream_iterator<int> inFile(in), eof;
29     ostream_iterator<int> out_file1(out1, "  ");
30     ostream_iterator<int> out_file2(out2, "\n");
31     while (inFile != eof)
32     {
33         if ((*inFile) % 2 != 0)
34             out_file1 = *inFile++;
35         else
36             out_file2 = *inFile++;
37     }
38 }

 

C++primer 10.4.2节练习