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 }
时间: 2024-10-24 06:37:49

C++primer 10.4.2节练习的相关文章

C++primer 10.4.1节练习

练习10.26 back_inserter创建一个使用push_back的迭代器. front_inserter创建一个push_front的迭代器. Inserter创建一个使用insert的迭代器.此函数接收第二个参数,这个参数必须是一个指向给定容器的迭代器.元素将被插入到给定迭代器所表示的元素之前. 练习10.27 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include &l

C++primer 10.3.1节练习

练习10.11 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 using namespace std; 9 10 void elmDups(vector<string>

C++primer 10.2.1节练习

练习10.3 1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include <stack> 5 #include <algorithm> 6 #include <numeric> 7 #include <list> 8 9 using namespace std; 10 11 12 int main() 13 { 14 vector<int

C++primer 10.2.2节练习

练习10.6 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 using namespace std; 8 9 int main() 10 { 11 vector<int> vec{0,1,2,3,4}; 12 fill

C++primer 10.3.2节练习

练习10.14 1 [] (const int &a, const int &b) {return a + b;} 练习10.15 1 [a] (const int &b) { return a + b; } 练习10.16 1 void biggies(vector<string>& words, vector<string>::size_type sz) 2 { 3 elmDups(words); 4 stable_sort(words.begi

C++primer 10.3.3节练习

练习10.20 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 using namespace std; 9 10 void elmDups(vector<string>

C++primer 14.2.1节练习

练习14.6 1 #include <iostream> 2 #include <string> 3 #include <memory> 4 #include <vector> 5 #include <algorithm> 6 #include <numeric> 7 8 using namespace std; 9 10 class Sales_data { 11 friend istream& operator >&

当当10周年书香节,高端安全运维套装仅 168元

如今,很多人宁可一天花上几个小时刷微博微信/微博,却不愿抽出几十分钟静静读会儿书,古语有云:"万般皆下品,唯有读书高.明天就是"世界读书日",做为一名原创作者,真诚地希望我们的生活能够充满墨香的书声.在当当10周年书香节来临之际,为大家推荐三本高端Linux安全运维图书,三件套仅需168元. 以下图书均被中国科学院图书馆.国家图书馆.211.985高校图书馆收藏! Linux企业应用案例精解(第2版)读者答疑QQ群:73120574 选购地址  http://product.

C++primer 10.6节练习

练习10.42 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 #