The C++ Programming Language第三章

1、一般来说,将一个名字空间中的所有名字统统倾倒进全局名字空间里,并不是一种好的做法。P41

2、迭代器和I/O P53

  要做出一个ostream_iterator,我们需要描述被使用的将是哪个流,还要描述写入其中的对象的类型。例如,我们可以定义一个引用了标准输出流cout的迭代器:ostream_iterator<string> oo(cout);

  一个istream_iterator就是某种东西,它使我们可以像从容器独处一样从输入流中读出:

istream_iterator<string> ii(cin);

 

举例:P54

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include <fstream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <iterator>
 8 #include <vector>
 9 
10 using namespace std;
11 int _tmain(int argc, _TCHAR* argv[])
12 {
13 
14     string from;
15     string to;
16     cin >> from >> to;
17 
18     ifstream is(from.c_str());
19     istream_iterator<string> ii(is);
20     istream_iterator<string> eos;
21 
22     vector<string> b(ii, eos);
23     sort(b.begin(), b.end());
24 
25     ofstream os(to.c_str());
26     ostream_iterator<string> oo(os, "\n");
27 
28     unique_copy(b.begin(), b.end(), oo);
29 
30 
31 
32     return !is.eof() | !os;
33 }

3、忠告

  当你遇到一个选择时,应该优先选择标准库而不是其他的库。

  不要认为标准库对于任何事情都是最理想的。

  如果向一个容器中添加一个元素,用push_back()或back_inserter()。

  在main()中捕捉公共的异常。

~~end~~

!!欢迎添加!!

 

 

原文地址:https://www.cnblogs.com/ziyoudefeng/p/2454350.html