Castie!

正态分布, 优劣伴生

北冥有鱼,其名为鲲(kūn)。鲲之大,不知其几千里也;化而为鸟,其名为鹏。鹏之背,不知其几千里也;怒而飞,其翼若垂天之云。是鸟也,海运则将徙于南冥。南冥者,天池也。


北海若曰:“井鼃不可以语于海者,拘于虚也;夏虫不可以语于冰者,笃于时也;曲士不可以语于道者,束于教也。今尔出于崖涘,观于大海,乃知尔丑,尔将可与语大理矣。

STL 一部数据结构的使用宝典

GitHub Repo:coderZsq.github.io
Follow: coderZsq · GitHub
Resume: https://coderzsq.github.io/coderZsq.webpack.js/#/

日常扯淡

一些STL的代码片段, 记录而已.

C++ 作者的建议

  1. 在C++中几乎不需要用宏, 用const和enum定义显式的常量, 用inline避免函数调用的额外开销, 用模板去刻画一组函数或类型, 用namespace去避免命名冲突.
  2. 不要在你需要变量之前去声明, 以保证你能立即对它进行初始化.
  3. 不要用malloc, new运算会做的更好.
  4. 避免使用void*, 指针算数, 联合和强制, 大多数情况下, 强制类型转换是设计错误的指示器.
  5. 尽量少用数组和C风格的字符串, 标准库中的string和vector可以简化程序.
  6. 更加重要的是, 试着将程序考虑为一组由类和对象表示的互相作用的概念年, 而不是一堆数据结构和一些可以拨弄的二进制.

STL

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class Compare {
public:
    bool operator()(int i, int j) {
        return i < j;
    }
};

class myCompare {
public:
    bool operator()(int & v) {
        return v > 5;
    }
};

int main(int argc, const char * argv[]) {


    int arr[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
    vector<int, allocator<int>> vi;
    vi.assign(arr, arr+10);
    
    int *p;
    for (p = arr; p != arr+10; p++) {
        cout<<*p<<endl;
    }
    
    vector<int, allocator<int>>::iterator itr;
    for (itr = vi.begin(); itr != vi.end(); ++itr) {
        cout<<*itr<<endl;
    }
    
    sort(vi.begin(), vi.end(), Compare());
    for (auto & i: vi) {
        cout<<i<<endl;
    }
    
//    int count = count_if(vi.begin(), vi.end(),myCompare());
    
    int count = count_if(vi.begin(), vi.end(),bind(greater<int>(), std::placeholders::_1,5));
    cout<<"count = "<<count<<endl;
    
    return 0;
}
}

Container

class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d = 100):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
    A(const A & another) {
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    int getData() {
        return _data;
    }
    void setData(int d) {
        _data = d;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {

    A a(10);
    cout<<a.getData()<<endl;
    {
        vector<A> va;
        va.push_back(a);
        va[0].setData(20);
        cout<<va[0].getData()<<endl;
    }
    cout<<a.getData()<<endl;
    cout<<"++++++++++++++++++"<<endl;

    return 0;
}

Vector

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
#if 0
    cout<<"front "<<vi.front()<<endl;
    cout<<"end   "<<vi.back()<<endl;
    vi.push_back(100);
    vi.pop_back();
    cout<<"front "<<vi.front()<<endl;
    cout<<"end   "<<vi.back()<<endl;

    vi.insert(vi.begin(), 10000);
    cout<<"front "<<vi.front()<<endl;
    cout<<"end   "<<vi.back()<<endl;
#endif
    vi.erase(vi.end() - 1);
    cout<<"front "<<vi.front()<<endl;
    cout<<"end   "<<vi.back()<<endl;
    
    vector<int>::iterator itr;
    for (itr = vi.begin(); itr != vi.end(); ++itr) {
        cout<<*itr<<endl;
    }
    return 0;
}
int main(int argc, const char * argv[]) {

//    vector<int> vi(10, 2);
//    vector<int> vi {1, 2, 3, 4, 5, 6, 7};
    vector<int> vi = {1, 2, 3, 4, 5, 6, 7};
    vector<int> vi2(vi);
    cout<<vi2.size()<<endl;
    
    vector<int>::iterator itr;
    for (itr = vi2.begin(); itr != vi2.end(); ++itr) {
        cout<<*itr<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi;
    cout<<vi.size()<<endl;
    if (vi.empty()) {
        cout<<"is empty"<<endl;
    }
    vi.push_back(10);
    if (vi.empty()) {
        cout<<"is empty"<<endl;
    }
    cout<<vi.max_size()<<endl;

    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi.reserve(10);
    cout<<"size: "<<vi.size()<<endl;
    cout<<"capacity: "<<vi.capacity()<<endl;
    vi.push_back(1);
    cout<<"size: "<<vi.size()<<endl;
    cout<<"capacity: "<<vi.capacity()<<endl;
    vi.shrink_to_fit();
    cout<<"size: "<<vi.size()<<endl;
    cout<<"capacity: "<<vi.capacity()<<endl;
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5};
    vector<int> vi2 = {1, 2, 3, 5, 4};
    if (vi == vi2) {
        cout<<"=="<<endl;
    } else {
        cout<<"!="<<endl;
    }
    if (vi > vi2) {
        cout<<">"<<endl;
    } else {
        cout<<"<"<<endl;
    }
    
    return 0;
}
//能用容器内部的算法, 不要用std::的算法

int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
    vector<int> vi2;
    vi2 = vi;
    
    vi2.assign(5, 1);
    for (auto & i: vi2) {
        cout<<i<<endl;
    }
    
    vi2.assign(vi.begin(), vi.end());
    for (auto & i: vi2) {
        cout<<i<<endl;
    }
    
    vi2.assign({1, 2, 3,4, 5, 6, 7, 8, 9});
    for (auto & i: vi2) {
        cout<<i<<endl;
    }
    
    vi.swap(vi2);
    swap(vi, vi2);
    
    return 0;
}
}
//random access 内存连续的容器

int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
    for (int i = 0; i < vi.size(); i++) {
        cout<<vi.at(i)<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
    for (auto itr = vi.crbegin(); itr != vi.crend(); ++itr) {
        cout<<*itr<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
    vi.push_back(100);
    for (auto & i: vi) {
        cout<<i<<endl;
    }
    while (!vi.empty()) {
        cout<<vi.back()<<endl;
        vi.pop_back();
    }
    cout<<vi.size()<<endl;
    
    return 0;
}
void printv(vector<int> & v) {
    for (auto & i: v) {
        cout<<i<<" ";
    }
    cout<<endl<<"================="<<endl;
}

int main(int argc, const char * argv[]) {

    int arr[3] = {888, 888, 888};
    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
//    vi.insert(vi.begin() + 1, 1000);
//    vi.insert(vi.begin(), 5, 999);
//    vi.insert(vi.begin(), arr, arr+3);
    vi.insert(vi.begin(), {777, 777, 777});
//    vi.erase(vi.begin(), vi.begin()+3);
    vi.resize(19, 66666);
    
    printv(vi);
    
    return 0;
}
void printv(vector<int> & v) {
    for (auto & i: v) {
        cout<<i<<" ";
    }
    cout<<endl<<"================="<<endl;
}

int main(int argc, const char * argv[]) {

    vector<int> vi;
    vi = {1, 2, 3, 4, 5, 6};
    auto itr = find(vi.begin(), vi.end(), 5);
    if (itr == vi.end()) {
        cout<<"find none"<<endl;
    } else {
        cout<<"find"<<endl;
        vi.erase(itr);
    }
    printv(vi);
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi;
    for (int i = 0; i < 10; i++) {
        vi.push_back(i);
        cout<<"size: "<<vi.size()<<endl;
        cout<<"capacity: "<<vi.capacity()<<endl;
    }
    vi.shrink_to_fit();
    cout<<"size: "<<vi.size()<<endl;
    cout<<"capacity: "<<vi.capacity()<<endl;
    
    return 0;
}
//capacity >= size;
//resize size的大小, capacity 中会变大不会变小
//capacity > size => capacity = size

//预留空间, 而不是申请空间

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5};
    cout<<vi.capacity()<<endl;
    vi.resize(10);
    vi.resize(1);
    vi.shrink_to_fit();
    cout<<vi.capacity()<<endl;

    return 0;
}
//resize > cursize push_back
//resize < cursize pop_back

//预留空间, 而不是申请空间
//reserve 预留空间, 而不构造, resize 一定会发生构造和析构行为

class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
    A(const A & another) {
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    int getData() {
        return _data;
    }
    void setData(int d) {
        _data = d;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {
    
    vector<A> va;
    va.reserve(100);
    A a;
    va.push_back(a);
    va.push_back(a);
    va.push_back(a);
    {
        va.resize(2); //push_back() | pop_back()
    }
    
    return 0;
}
class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
    A(const A & another) {
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    int getData() {
        return _data;
    }
    void setData(int d) {
        _data = d;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {
    
    vector<A*> vap;
    vap.reserve(10);
    A *pa = new A;
    vap.push_back(pa);
    delete pa;
    
    return 0;
}
//怎么来看迭代器, 就是一个智能指针, 重载了 operator* operator->

int main(int argc, const char * argv[]) {
    
    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int>::iterator itr;
    for (itr = vi.begin(); itr != vi.end(); ) {
        if (!((*itr) % 2)) {
            vi.erase(itr);
        } else {
            cout<<*itr<<endl;
            ++itr;
        }
    }
    return 0;
}
int main(int argc, const char * argv[]) {
    
    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//    auto itr = vi.insert(vi.begin(), 100);
//    cout<<*itr<<endl;
    
    vector<int>::iterator itr;
    for (itr = vi.begin(); itr != vi.end();) {
        if (*itr % 2) {
            itr = vi.insert(itr, 100);
            itr += 2;
        } else {
            ++itr;
        }
    }
    for (auto & i: vi) {
        cout<<i<<endl;
    }
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> t(5);
    vector<vector<int>> tt(5, t);
    
    cout<<tt.size()<<endl;
    cout<<tt[0].size()<<endl;
    cout<<tt[1].size()<<endl;
    cout<<tt[2].size()<<endl;
    cout<<tt[3].size()<<endl;
    cout<<tt[4].size()<<endl;

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cout<<(tt[i][j] = 100)<<" ";
        }
        cout<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {

    srand(time(nullptr));
    
    vector<int> t;
    vector<vector<int>> tt(5, t);
 
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < rand()%20; j++) {
            tt[i].push_back(j);
        }
    }
    
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < tt[i].size(); j++) {
            cout<<tt[i][j]<<" ";
        }
        cout<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 3, 5, 6, 7};
    
//    vector<int>::iterator itr;
//    vector<int>::reverse_iterator itr;
//    vector<int>::const_iterator itr;
    vector<int>::const_reverse_iterator itr;
    
    for (itr = vi.crbegin(); itr != vi.crend(); ++itr) {
        cout<<*itr<<endl;
    }
    
    return 0;
}

// .erase(std::remove) vector

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 5 , 9, 10};
    auto itr = vi.begin();
//    for (; itr != vi.end();) {
//        if (*itr == 5) {
//            itr = vi.erase(itr);
//        } else {
//            ++itr;
//        }
//    }
    cout<<vi.size()<<endl;
    dump(vi);
    vi.erase(remove(vi.begin(), vi.end(), 5), vi.end());
    cout<<vi.size()<<endl;
    dump(vi);

    return 0;
}

List

void printl(list<int> & li) {
    for (auto & i : li) {
        cout<<i<<" ";
    }
    cout<<endl;
}

int main(int argc, const char * argv[]) {

    list<int> li = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
    cout<<li.front()<<endl;
    cout<<li.back()<<endl;
    
    printl(li);
//    li.push_back(999);
//    li.pop_back();
//    li.push_front(999);
//    li.pop_front();
//    li.insert(li.begin(), 999);
//    li.erase(li.begin());
    li.remove(2);
    printl(li);

    return 0;
}
int main(int argc, const char * argv[]) {

    int arr[10] = {1, 2, 3, 4};
    list<int> li(arr, arr+4);
    printl(li);
    
//    list<int> li2(li);
    list<int> li2(li.begin(), li.end());
    printl(li);
    
    cout<<li2.size()<<endl;
    while (!li2.empty()) {
        cout<<li2.back()<<" ";
        li2.pop_back();
    }
    cout<<endl;
    cout<<li2.size()<<endl;

    return 0;
}
int main(int argc, const char * argv[]) {

    list<int> li = {1, 2, 3, 4};
    list<int> li2 = {1, 3, 2, 4};
    
    if (li == li2) {
        cout<<"=="<<endl;
    } else {
        cout<<"!="<<endl;
    }
    
    return 0;
}
class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    A(const A & another) {
        this->_data = another._data;
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        if (this != &another) {
            this->_data = another._data;
        }
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    friend bool operator== (const A & one, const A & another) {
        if (one._data == another._data) {
            return true;
        } else {
            return false;
        }
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {

    list<A> li = {A(1), A(2), A(3), A(4)};
    list<A> li2 = {A(1), A(2), A(3), A(4)};
    
    if (li == li2) {
        cout<<"=="<<endl;
    } else {
        cout<<"!="<<endl;
    }
    
    return 0;
}
class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    A(const A & another) {
        this->_data = another._data;
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        if (this != &another) {
            this->_data = another._data;
        }
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    friend bool operator== (const A & one, const A & another) {
        if (one._data == another._data) {
            return true;
        } else {
            return false;
        }
    }
    void dump() {
        cout<<_data<<" ";
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {

    list<A> la;
    la.push_back(A(1));
    la.push_back(A(100));
    la.emplace(la.begin(), 200);
    la.emplace_back(300);
    la.emplace_front(400);
    
    for (auto & obj: la) {
        obj.dump();
    }
    cout<<endl;
    
    return 0;
}
int main(int argc, const char * argv[]) {

    list<int> li = {1, 3, 5, 7, 9};
//    list<int>::iterator itr = li.begin();
//    advance(itr, 1);
//    li.erase(li.begin(), itr);
//    li.erase(li.begin(), ++li.begin());
    li.erase(li.begin(), li.end());
    dump(li);
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    list<int> li = {1, 3, 5, 7, 9};
//    li.remove(5);
    li.remove_if([](int x) {
        if(x >= 5)
            return true;
        else
            return false;
    });
    dump(li);
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    greater<int>()(1, 2);
    int arr[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
    sort(arr, arr+10, greater<int>());
    for (auto & i: arr) {
        cout<<i<<endl;
    }
    vector<int> vi(arr, arr + 10);
    sort(vi.begin(), vi.end(), less<int>());
    for (auto & i: vi) {
        cout<<i<<endl;
    }
    list<int> li(arr, arr + 10);
    li.sort();
    for (auto & i: li) {
        cout<<i<<endl;
    }
    
    return 0;
}
class A {
public:
    A() {
        cout<<"无参构造函数 "<<this<<endl;
    }
    A(int d):_data(d) {
        cout<<"有参构造函数 "<<this<<endl;
    }
    A(const A & another) {
        this->_data = another._data;
        cout<<this<<" 构造拷贝自 "<<&another<<endl;
    }
    A & operator=(const A & another) {
        if (this != &another) {
            this->_data = another._data;
        }
        cout<<this<<" 赋值拷贝自 "<<&another<<endl;
        return *this;
    }
    friend bool operator== (const A & one, const A & another) {
        if (one._data == another._data) {
            return true;
        } else {
            return false;
        }
    }
    void dump() {
        cout<<_data<<" ";
    }
    friend bool operator<(const A & one, const A & another) {
        if (one._data == another._data) {
            return true;
        } else {
            return false;
        }
    }
    ~A() {
        cout<<"析构函数 "<<this<<endl;
    }
private:
    int _data;
};

int main(int argc, const char * argv[]) {

    srand(time(nullptr));
    list<A> la;
    for (int i = 0; i < 10; i++) {
        la.emplace_back(rand() % 100);
    }
    la.sort();
    for (auto & obj: la) {
        obj.dump();
    }
    cout<<endl;
    
    return 0;
}
class g {
public:
    bool operator()(int x) const {
        if (x > 5) {
            return true;
        } else {
            return false;
        }
    }
    typedef int argument_type;
};

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int count = count_if(vi.begin(), vi.end(), not1(g()));
    cout<<count<<endl;
    
    return 0;
}
using namespace std;
using namespace placeholders;

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int count = count_if(vi.begin(), vi.end(), bind(greater<int>(), _1, 5));
    cout<<count<<endl;
    
    return 0;
}
using namespace std;
using namespace std::placeholders;

struct mygreat {
    int operator()(int x, int y) {
        cout<<"x: "<<x<<endl;
        cout<<"y: "<<y<<endl;
        return x < y;
    }
};

//bind1st bind2nd

int main(int argc, const char * argv[]) {

    vector<int> vi = {1, 2, 3, 11, 22, 33, 44, 55, 66, 77};
//    int count1 = count_if(vi.begin(), vi.end(), bind1st(greater<int>(), 10));
//    int count2 = count_if(vi.begin(), vi.end(), bind2nd(greater<int>(), 10));
//    cout<<count1<<"-"<<count2<<endl;
    
    int count1 = count_if(vi.begin(), vi.end(), bind(greater<int>(), 10, _1));
    int count2 = count_if(vi.begin(), vi.end(), bind(greater<int>(), _1, 10));
    int count3 = count_if(vi.begin(), vi.end(), bind(mygreat(), _1, 10));

    cout<<count1<<"-"<<count2<<"-"<<count3<<endl;

    return 0;
}
int main(int argc, const char * argv[]) {
    
    list<int> li = {1, 2, 1, 1, 1, 3, 4, 5, 5, 6, 6, 9, 7, 9};
//    li.sort();
    li.unique([](int x, int y) {
        if (x == y && y % 2) {
            return true;
        } else {
            return false;
        }
    });
    dump(li);
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    list<int> li1 = {1, 3, 5, 9, 7};
    list<int> li2 = {2, 4, 6, 8, 10};
//    li1.splice(li1.begin(), li2);
//    li1.splice(li1.end(), li2, li2.begin());
//    auto itr = li1.begin();
//    advance(itr, 2);
//    li1.splice(itr, li2, li2.begin(), li2.end());
//    li1.reverse();
    li1.sort();
    li2.sort();
    li1.merge(li2);
    dump(li1);
    dump(li2);
    return 0;
}

Deque

int main(int argc, const char * argv[]) {
    
    deque<char> dc;
    for (char ch = 'a'; ch <= 'z'; ch++) {
        dc.push_front(ch);
    }
    while (!dc.empty()) {
        cout<<dc.front()<<" ";
        dc.pop_front();
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    deque<char> dc;
    for (char ch = 'a'; ch <= 'z'; ch++) {
        dc.push_front(ch);
    }
    while (!dc.empty()) {
        cout<<dc.back()<<" ";
        dc.pop_back();
    }
    
    return 0;
}

Stack

int main(int argc, const char * argv[]) {
    
    stack<char> si;
    for (char ch = 'a'; ch <= 'z'; ch++) {
        si.push(ch);
    }
    while (!si.empty()) {
        cout<<si.top()<<" ";
        si.pop();
    }
    
    return 0;
}

Queue

int main(int argc, const char * argv[]) {
    
    queue<char> qc;
    for (char ch = 'a'; ch <= 'z'; ch++) {
        qc.push(ch);
    }
    while (!qc.empty()) {
        cout<<qc.front()<<" ";
        qc.pop();
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    priority_queue<int, vector<int>, greater<int>> pqi;
    pqi.push(1);
    pqi.push(3);
    pqi.push(5);
    pqi.push(7);
    pqi.push(9);
    pqi.push(2);
    pqi.push(4);
    pqi.push(6);
    pqi.push(8);

    while (!pqi.empty()) {
        cout<<pqi.top()<<" ";
        pqi.pop();
    }
    
    return 0;
}

Map

int main(int argc, const char * argv[]) {
    
    map<int, string, less<int>> mis = {
        pair<int, string>(1, "aaa"),
        pair<int, string>(2, "bbb"),
        pair<int, string>(3, "ccc"),
        pair<int, string>(4, "ddd")
    };
    
    mis.insert(pair<int, string>(5, "eee"));
    mis.insert(map<int, string>::value_type(6, "fff"));
    mis.insert(make_pair(7, "ggg"));
    
    mis[4] = "xxx";
    mis[100] = "yyy";
    
    map<int, string>::iterator itr;
    for (itr = mis.begin(); itr != mis.end(); ++itr) {
        //        cout<<itr->first<<" : "<<itr->second<<endl;
        cout<<(*itr).first<<" : "<<(*itr).second<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    multimap<int, string, less<int>> mis = {
        pair<int, string>(1, "aaa"),
        pair<int, string>(2, "bbb"),
        pair<int, string>(3, "ccc"),
        pair<int, string>(4, "ddd")
    };
    
    mis.insert(pair<int, string>(4, "eee"));
    mis.insert(map<int, string>::value_type(3, "fff"));
    mis.insert(make_pair(2, "ggg"));
    
    multimap<int, string>::iterator itr;
    for (itr = mis.begin(); itr != mis.end(); ++itr) {
        //        cout<<itr->first<<" : "<<itr->second<<endl;
        cout<<(*itr).first<<" : "<<(*itr).second<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    multimap<int, string, less<int>> mis = {
        pair<int, string>(1, "aaa"),
        pair<int, string>(2, "bbb"),
        pair<int, string>(3, "ccc"),
        pair<int, string>(4, "ddd")
    };
    
    mis.insert(mis.find(100), pair<int, string>(100, "ddd"));
    mis.insert(mis.find(100), pair<int, string>(100, "ddd"));
    mis.insert(mis.find(100), pair<int, string>(100, "ddd"));
//    mis.erase(100);
    auto it = mis.find(1);
    if (it != mis.end()) {
        mis.erase(it);
    }
//    mis.erase(mis.begin());
//    mis.clear();
    
    multimap<int, string>::iterator itr;
    for (itr = mis.begin(); itr != mis.end(); ++itr) {
        //        cout<<itr->first<<" : "<<itr->second<<endl;
        cout<<(*itr).first<<" : "<<(*itr).second<<endl;
    }
    cout<<mis.count(100)<<endl;
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    map<int, string, less<int>> mis = {
        pair<int, string>(1, "a"),
        pair<int, string>(2, "ab"),
        pair<int, string>(3, "abc"),
        pair<int, string>(4, "abcd"),
        pair<int, string>(5, "abcde"),
        pair<int, string>(6, "abcdef"),
        pair<int, string>(7, "abcdefg"),
        pair<int, string>(8, "abcdefgh"),
        pair<int, string>(9, "abcdefghi"),
        pair<int, string>(10, "abcdefghij")
    };
    
    auto lowitr = mis.lower_bound(5);
    auto upperitr = mis.upper_bound(7);
    
    cout<<"low: "<<lowitr->first<<" upper: "<<upperitr->first<<endl;
    //循环删除最好不要用遍历的方式.
//    for (auto itr = lowitr; itr != upperitr;) {
//        itr = mis.erase(itr);
//    }
    mis.erase(lowitr, upperitr);
    
    map<int, string>::iterator itr;
    for (itr = mis.begin(); itr != mis.end(); ++itr) {
        cout<<itr->first<<" : "<<itr->second<<endl;
    }
    
    return 0;
}
int main(int argc, const char * argv[]) {
    
    multimap<int, string, less<int>> mis = {
        pair<int, string>(1, "a"),
        pair<int, string>(2, "ab"),
        pair<int, string>(3, "abc"),
        pair<int, string>(4, "abcd"),
        pair<int, string>(5, "a"),
        pair<int, string>(5, "b"),
        pair<int, string>(5, "c"),
        pair<int, string>(5, "d"),
        pair<int, string>(6, "abcdef"),
        pair<int, string>(7, "abcdefg"),
        pair<int, string>(8, "abcdefgh"),
        pair<int, string>(9, "abcdefghi"),
        pair<int, string>(10, "abcdefghij")
    };
    
//    auto range = mis.equal_range(5);
    pair<map<int, string>::iterator, map<int, string>::iterator> range = mis.equal_range(5);
    cout<<"low: "<<range.first->first<<" uppper: "<<range.second->first<<endl;
    cout<<"low: "<<range.first->first<<" :"<<range.first->second<<endl;
    cout<<"low: "<<range.second->first<<" :"<<range.second->second<<endl;

    return 0;
}

Set

int main(int argc, const char * argv[]) {
    
    set<string, greater<string>> ss;
    ss.insert("a");
    ss.insert("b");
    ss.insert("c");
    ss.insert("d");
    ss.insert("e");
    ss.insert("f");

    for (auto & s: ss) {
        cout<<s<<endl;
    }
    
    return 0;
}

最后

更多新鲜文章可以关注并Star, 我们一起学习.

GitHub Repo:coderZsq.github.io
Follow: coderZsq · GitHub

最近的文章

Swift 进阶 | 看得见的算法

GitHub Repo:coderZsq.target.swiftFollow: coderZsq · GitHubResume: https://coderzsq.github.io/coderZsq.webpack.js/#/日常扯淡前段日子写了篇面经, 得到了掘金的征文活动的三等奖还是非常开心, 但是被寒神说面试不过就泄题, 影响不好, 我想想也是, 马上就把大厂的名字给抹掉了, 但被转载的就无能为力了, 看到下面好多喷的, 真是背后一凉, 一首凉凉送给自己, 造成的伤害无法挽回...…

移动开发继续阅读
更早的文章

C++ 一把卍解OC底层的斩魄刀

GitHub Repo:coderZsq.github.ioFollow: coderZsq · GitHubResume: https://coderzsq.github.io/coderZsq.webpack.js/#/日常扯淡本期没什么好扯淡的, 这篇文章留着自己看.C++ 作者的建议 在C++中几乎不需要用宏, 用const和enum定义显式的常量, 用inline避免函数调用的额外开销, 用模板去刻画一组函数或类型, 用namespace去避免命名冲突. 不要在你需要变量...…

移动开发继续阅读