首页 > 其他 > 详细

STL基础--基本介绍

时间:2018-12-30 18:20:56      阅读:142      评论:0      收藏:0      [点我收藏+]

为什么要使用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>

STL基础--基本介绍

原文:https://www.cnblogs.com/logchen/p/10200125.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!