使用STL的理由:
/+
* Reasons to use C++ standard library :
* 1. Code reuse, no need to re- invent the wheel.
* 2. Efficiency (fast and use less resources)。Modern C++ compiler are usually tuned to optimize for C++ s tandard library code .
* 3. Accurate, less buggy .
* 4. Terse, readable code; reduced control f1ow .
* 5. Standardization, guarenteed availability
* 6. A role model of writing library .
* 7. Good knowledge of data structures and algori thms .
*/
概述
STL全称为Standard Templates Library,即标准模板库,它是C++标准库的一个子集,主要包含三大部分:
Containers (容器)和Algorithms(算法)以及 Iterators(迭代器);
迭代器的出现是为了让两者更好地结合,
下面我们来看一个简单的STL示例:
示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
//Container:vector
vector<int> vec;//vector使用了template类,因此必须写明<具体类型>
vec.push_back(57);//push_back()为后插函数,插在vector尾部
vec.push_back(9);
vec.push_back(12);
//Iterator

本文介绍了C++ Standard Template Library (STL),包括其主要组成部分:容器、迭代器和算法。通过示例展示了如何使用容器vector,并详细解释了迭代器的特性。此外,还提到了sort等算法的使用方法。
最低0.47元/天 解锁文章
111

被折叠的 条评论
为什么被折叠?



