为什么要使用C++标准库

/*
 * 为什么使用C++标准库:
 * 1. 代码重用,不用重新造轮子
 * 2. 效率(快速,且使用更少的资源). 现代C++编译器经常对C++标准库的代码有优化
 * 3. 准确,更少的bug
 * 4. 简洁,可读性好;减少控制流
 * 5. 标准化,保证可用
 * 6. 是编写库的一个很好的榜样
 * 7. 对数据结构和算法有更好的认识
 */
/*
 * STL: Standard Template Library
 *   -- 容器和算法,迭代器是容器和算法之间的桥梁,使容器和算法更容易扩展
 */


// 一个简单的例子
using namespace std;

vector<int> vec;
vec.push_back(4);
vec.push_back(1);
vec.push_back(8);  // vec: {4, 1, 8}

vector<int>::iterator itr1 = vec.begin();  // half-open:  [begin, end)
vector<int>::iterator itr2 = vec.end();

for (vector<int>::iterator itr = itr1; itr!=itr2; ++itr)
   cout << *itr << " ";  // Print out:  4 1 8

sort(itr1, itr2);  // vec: {1, 4, 8}

STL Headers

#include <vector>
#include <deque>
#include <list>
#include <set>   // set and multiset
#include <map>   // map and multimap
#include <unordered_set>  // unordered set/multiset
#include <unordered_map>  // unordered map/multimap
#include <iterator>
#include <algorithm>
#include <numeric>    // some numeric algorithm
#include <functional>

相关文章:

  • 2022-12-23
  • 2021-05-16
  • 2022-01-20
  • 2021-08-05
  • 2021-06-23
  • 2021-09-12
  • 2022-12-23
  • 2021-12-08
猜你喜欢
  • 2022-01-10
  • 2021-11-23
  • 2022-01-21
  • 2021-09-23
  • 2022-01-10
  • 2022-01-06
相关资源
相似解决方案