STL之map和multimap(关联容器)

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。自动建立Key - value的对应,对于迭代器来说,可以修改实值,而不能修改key。 multimap允许一键(key)对应多值(value)
#include
#include
using namespace std ;
int main()  {
    map first;
    first['a']=10;
    first['b']=30;
    first['c']=50;
    first['d']=70;
    map::iterator iter = first.begin() ;
    for(; iter != first.end() ; iter++)
        cout << iter->first << " " << iter->second << endl ;
    cout << endl ;

    map second (first.begin(),first.end());
    map::iterator iter2 = second.begin() ;
    for(; iter2 != second.end() ; iter2++)
        cout << iter2->first << " " << iter2->second << endl ;
    cout << endl ;

    map third (second);
    map::iterator iter3 = third.begin() ;
    for(; iter3 != third.end() ; iter3++)
        cout << iter3->first << " " << iter3->second << endl ;
    return 0 ;
}
插入、查找、删除函数:
#include
#include
using namespace std ;
int main()  {
    multimap first;   //可以一键对应多值
    first.insert(pair('a',10));
    first.insert(pair('b',20));
    first.insert(pair('b',30));
    map::iterator iter = first.begin() ;
    for(; iter != first.end() ; iter++)
        cout << iter->first << " " << iter->second << endl ;
    cout << first.count('b') << endl ;     //键值出现的次数
    multimap::iterator iterr ;
    iterr = first.find('a') ;              // 查找关键字位置
    first.erase(iterr) ;                   // 删除该关键字
    map::iterator iter3 = first.begin() ;
    for(; iter3 != first.end() ; iter3++)
        cout << iter3->first << " " << iter3->second << endl ;
    return 0 ;
}
原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236862.html