?
?
STL大体分为六大组件:分别是容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
?
STL容器就是将运用最广泛的一些数据结构实现出来
常用的数据结构:数组、链表、树、栈、队列、集合、映射表等
这些容器分为序列式容器和关联式容器:
? 序列式容器:强调值的排序,序列式容器中的每个元素均有固定位置
? 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法
算法分为质变算法和非质变算法:
质变算法:运算过程中会更改区间内的元素的内容。例如拷贝、替换、删除等
非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等
提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式
每个容器都有自己专属的迭代器
迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针
| 种类 | 功能 | 支持运算 | 
|---|---|---|
| 输入迭代器 | 对数据的只读访问 | 只读,支持++、==、!= | 
| 输出迭代器 | 对数据的只写访问 | 只写,支持++ | 
| 前向迭代器 | 读写操作,并能向前推进迭代器 | 读写,支持++、==、!= | 
| 双向迭代器 | 读写操作,并能向前和向后操作 | 读写,支持++、-- | 
| 随机访问迭代器 | 读写操作,可以以跳跃的方式访问任意数据 | 读写,支持++、--、[n]、-n、<、<=、>、>= | 
常用的容器中迭代器种类为双向迭代器,和随机访问迭代器
?
STL中最常用的容器为vector,可以理解为数组,下面学习如何向这个容器中插入数据,并遍历这个容器
容器:vector
算法:for_each
迭代器:vector<int>::iterator
#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
void myPrint(int val) {
    cout << val << endl;
}
void test01() {
    //创建一个vector容器,数组
    vector<int> v;
    //向容器中插入数据
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    ////通过迭代器访问容器中的数据
    vector<int>::iterator itBegin = v.begin(); //起始迭代器,指向容器中第一个元素
    vector<int>::iterator itEnd = v.end(); //结束迭代器,指向容器中最后一个元素的下一个位置
    //第一种遍历方式
    while (itBegin != itEnd) {
        cout << *itBegin << endl;
        itBegin++;
    }
    ////第二种遍历方式
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << endl;
    }
    //第三种遍历方式
    for_each(v.begin(), v.end(), myPrint);
}
int main() {
    test01();
    system("pause");
    return 0;
}
学习目标:vector中存放自定义的数据类型,并打印输出
#include<iostream>
#include<vector>
#include<string>
#include<algorithm> //标准算法头文件
using namespace std;
class Person {
public:
    Person(string name, int age) {
        this->m_name = name;
        this->m_age = age;
    }
    string m_name;
    int m_age;
};
void test01() {
    vector<Person> v;
    Person p1("A", 10);
    Person p2("B", 20);
    Person p3("C", 30);
    Person p4("D", 40);
    Person p5("E", 50);
    //向容器中添加数据
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    //遍历容器中数据
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "name: " << (*it).m_name << " age: " << (*it).m_age << endl;
        cout << "name: " << it->m_name << " age: " << it->m_age << endl;
    }
}
//存放自定义数据类型 指针
void test02() {
    vector<Person*> v;
    Person p1("A", 10);
    Person p2("B", 20);
    Person p3("C", 30);
    Person p4("D", 40);
    Person p5("E", 50);
    //向容器中添加数据
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);
    //遍历容器中数据
    for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
        cout << "name: " << (*it)->m_name << " age: " << (*it)->m_age << endl;
    }
}
int main() {
    test02();
    system("pause");
    return 0;
}
学习目标:容器中嵌套容器,我们将所有数据进行遍历输出
#include<iostream>
#include<vector>
using namespace std;
void test01() {
    vector<vector<int>> v;
    //创建小容器
    vector<int> v1;
    vector<int> v2;
    vector<int> v3;
    vector<int> v4;
    //向小容器中添加数据
    for (int i = 0; i < 4; i++) {
        v1.push_back(i + 1);
        v2.push_back(i + 2);
        v3.push_back(i + 3);
        v4.push_back(i + 4);
    }
    //将小容器插入到大容器中
    v.push_back(v1);
    v.push_back(v2);
    v.push_back(v3);
    v.push_back(v4);
    //通过大容器将所有数据遍历
    for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) {
        for (vector<int>::iterator t = (*it).begin(); t != (*it).end(); t++) {
            cout << *t ;
        }
        cout << endl;
    }
}
int main() {
    test01();
    system("pause");
    return 0;
}原文:https://www.cnblogs.com/maeryouyou/p/12290430.html