effective stl 读书笔记

来源:互联网 发布:python ssh执行.sh 编辑:程序博客网 时间:2024/05/01 19:02

1、函数声明中的函数参数:

int f(double d);  等价于 int f(double (d));  等价于 int f(double);

 

int g(double (*pf)());  等价于 int g(double pf());  等价于 int g(double ());

 

2、choose carefully among erasing options

Container<int> c;

If you have a contiguous-memory container  (vector, deque, or string — see Item 1),
the best approach is the erase-remove idiom (see Item 32):

c.erase( remove(c.begin(), c.end(), 1963),      // the erase-remove idiom is
           c.end());                                              //the best way to get rid of
                                                                      // elements with a specific 
                                                                      // value when c is a vector, 
                                                                      //string, or deque(deque底层实现在sgi版本中貌似是红黑树啊)

 

c. remove(1963);                                          //the remove member function is the
                                                                     // best way to get rid of elements with
                                                                     // a specific value when c is a list

 

c.erase(1963);                                              // the erase member function is the
                                                                     // best way to get rid of elements with
                                                                     // a specific value when c is a 
                                                                     // standard associative container

 

 

3、If you're updating an existing map element, operator[] is preferable, but if you're adding a new element, insert has the edge.

 

4、Some of you, I know, are into brute-force memorization, so here's a list of the
algorithms that require the data on which they operate to be sorted:
binary_search  lower_bound
upper_bound  equal_range
set_union  Set_intersection
set_difference  set_symmetric_difference
Merge  inplace_merge
includes
 
In addition, the following algorithms are typically used with sorted ranges, though they
don't require them:
unique  unique_copy

 

5、文件操作

ifstream inputFile("exa.txt");
  inputFile.unsetf(ios::skipws);                                           //disable the skipping of whitespace in inputFile.
  string fileData((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
  cout << fileData << endl;

  ofstream outputFile("opt.txt");
  outputFile.unsetf(ios::skipws);
  ostreambuf_iterator<char> out_it(outputFile);
  copy(fileData.begin(), fileData.end(), out_it);