STL

STL:容器(模板数据结构),迭代器,算法。

-------------------------------------------------------------------------------
当把一个元素插入到容器中时,便生成了这个元素的副本,因此,元素类型应该支持拷贝构造函数和赋值操作。

-----------------------------------------------------------------------------------------------
array:固定大小的数组。
vector:动态数组,当vector已满时,会重新分配一个更大的连续内存,把原先的数据拷贝进去,
并释放原来的vector。适合在尾端增删元素。扩容时,增量太大,可能浪费,增量太小,频繁扩容,开销大。
deque:可以在deque两段增加内存,适合在两端增删元素。
array,vector,deque,底层都是数组,随机访问迭代器。
-----------------------------------------------------------------------------------
list:双向链表,任意位置插入和删除。双向迭代器。
forward_list:单链表

--------------------------------------------------------------------------------------------
push_back( ):vector deque list
push_front( ): deque list
pop_back( ) : vector deque list
pop_front( ):deque list
front( )
back( )

-------------------------------------------------------------------------------------------------------------

适配器不支持迭代器:stack,queue,priority_queue

stack实现机制:vector,deque,list。push,pop,top,empty,size

queue实现机制:deque,list。front,back,push,pop,empty,size

priority_queue实现机制:堆。top,push,pop,empty,size。高优先级的元素最先删除。

-----------------------------------------------------------------------------------------------------------

//遍历数组的三种方法
#include<iostream>
using namespace std;
int main() {
    int a[3] = { 1, 3, 44 };
    //下标
    for (int i = 0; i < 3; ++i) {
        cout << a[i] << endl;
    }
    //指针||迭代器
    for (const int *ptr = begin(a); ptr < end(a); ++ptr) {
        /*函数begin和end在<iterator>*/
        cout << *ptr << endl;
    }
    //简化版本
    for (auto &items : a) {
        items++;
    }
    for (const auto &items : a) {
        cout << items << " ";
    }
}
//vector
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
    vector<int> v;
    int a[3] = { 99, 9, 999 };
    v.push_back(1);
    v.insert(v.end(), begin(a), end(a));
    v.pop_back();
    for (int i = 0; i < v.size(); ++i) {
        cout << v[i] << endl;
    }
    for (auto items = v.rbegin(); items < v.rend(); ++items) {
        (*items)++;
    }
    v[0] = 100;
    v.erase(v.begin() + 2);//删除的对象含有非智能指针,会造成内存泄漏
    cout << v.empty() << endl;
    for (const auto &ele : v) {
        cout << ele << endl;
    }
    cout << v.size() << endl;
}
//list
#include<iostream>
#include<array>
#include<iterator>
#include<list>
using namespace std;
template<typename T> void printList(const list<T> &);
int main() {
    array<double, 3> arr = { 110, 120, 119 };
    list<double> d1;
    list<double> d2;
    d1.push_back(2.2); d1.push_back(1.1); d1.push_back(3.3);
    d2.push_back(6.6); d2.push_back(4.4); d2.push_back(5.5);
    //把arr的元素插入到d1指定位置前
    d1.insert(d1.cbegin(), arr.cbegin(), arr.cend());
    //把d2的元素全部插入到d1,但是d2的所有元素被删除
    d1.splice(d1.cbegin(), d2);
    d2.insert(d2.cbegin(), arr.cbegin(), arr.cend());
    d1.sort();
    d2.sort();
    //合并两个有序序列,d1被清空,d2保存合并后的有序序列
    d2.merge(d1);
    d2.pop_back();
    d2.pop_front();
    //去重
    d2.unique();
    //d1,d2互换所有元素
    d1.swap(d2);
    //把d2的所有元素清空,把d1的元素赋给d2
    d2.assign(d1.cbegin(),d1.cend());
    //删除所有的110
    d1.remove(110);
    printList(d2);//2.2 3.3 4.4 5.5 6.6 110 119 120
    printList(d1);//2.2 3.3 4.4 5.5 6.6 119 120
    return 0;
}
template<typename T> void printList(const list<T> &List) {
    if (List.empty()) {
        cout << "list is empty!";
    }
    else {
        ostream_iterator<T> out(cout, " ");
        copy(List.cbegin(), List.cend(), out);
    }
    cout << endl;
}
原文地址:https://www.cnblogs.com/afreeman/p/8502883.html