STL第二课

①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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值