STL第二课

来源:互联网 发布:java 命令模式 实例 编辑:程序博客网 时间:2024/06/05 06:06

①map和unorder_map : map采用平衡二叉树,里面的数据是排序的。unorder_map采用hash,数据没有排序,里面的数据是散列的。

②map可以通过first来访问key,通过second来访问value。

③remove_if的用法

template <class ForwardIterator, class UnaryPredicate>  ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,                             UnaryPredicate pred);

#include <iostream>#include <map>#include <unordered_map>#include <string>#include <vector>#include <algorithm>#include <functional>using namespace std;void foo(const map<int, string>& m){    auto i = m.find(3);        if (i == m.end()) {        cout<< "Not Found" <<endl;    } else {        cout << i->second<<endl;    }}int main(){    map<int, string> m;    //unordered_map<int , string> m1;    //采用hash的存储方式,没有排序,查找的时候是常说时间    m[0] = "one";    m[1] = "two";    m[2] = "three";    m[3] = "four";        for (auto i = m.cbegin(); i != m.cend(); ++i) {        cout<<"(" <<i->first <<","<< i->second <<")"<<endl;    }            foo(m);        vector<int> vt;    vt.push_back(11);    vt.push_back(22);    vt.push_back(33);    vt.push_back(44);    vt.push_back(55);        vt.erase(remove_if(vt.begin(), vt.end(), [](int e){ return (e % 2 == 1);}), vt.end());                for (auto i = vt.begin(); i != vt.end(); ++i) {        cout << *i<<endl;    }        return 0;}


0 0
原创粉丝点击