您的位置:首页 > 其它

STL学习笔记-map/multimap容器

2015-10-21 23:02 357 查看
简介:
map是标准的关联式容器,一个map是一个键值对的序列,即(key,value)。提供基于key的快速检索能力
map中key的值是唯一的。map中的元素按照一定的顺序排列,元素插入是按照排序规则插入的,不能指定位置插入
map的具体实现是红黑树变体的平衡二叉树数据结构。插入和删除比vector快
map可直接取key对应的value,如map[key] = value
multimap和map区别在于:map中同一个key只能出现一次,而multimap中相同键可以出现多次,但是multimap不支持[]操作

头文件:
#include<map>

map/multimap的默认构造:
map<T1, T2> maptt;
multimap<T1, T2> multimaptt;

map的插入和迭代器:
map<int, string> map1;
//四种插入方式
map1.insert(pair<int, string>(1, "sudent1"));
map1.insert(make_pair(2, "student2"));
map1.insert(map<int, string>::value_type(3, "student3"));
map1[4] = "stident4";
//遍历
for(map<int, string>::iterator it = map1.begin(); it != map1.end(); it++){
     cout<<it->first<<":" << it->second<<endl;    
}
//删除元素
while(!map1.empty()){
         map<int, string>::iterator it = map1.begin();

         map1.erase(it);
}
//四种插入方法的区别:
//前三种直接用insert的方法返回的都是pair<iterator, bool>, 第一个是插入元素对应的迭代器的位置
pair<map<int, string>::iterator, bool> pair1 = map1.insert(make_pair(2, "student22"));
if(pair1.second == true)  
         cout << "insert success." << endl;
else
         cout << "insert falied." << endl; //此时会打印出错误信息,插入失败
//第四种插入方法会覆盖已经存在的键值对
map1[4] = "stident44"; 

map的查找和异常处理:
map.fine(key); // 若找到,返回迭代器,否则返回map.end()
map.count(key); //返回map中改键为key的对组的个数,对map要么是0或者1,multimap可能大于1
//map也支持lower_bound upper_bound equal_range等操作,和set一样
pair(map<int, string>::iterator, map<int, string>::iterator) mypair = map1.equal_range(5);
//第一个是>=5的位置 第二个是>5的位置
if(mypair.first != map1.end()){
     cout << mapair.first->first << ":" << mapair.first->second<<endl;
}

if(mypair.second!= map1.end()){
     cout << mapair.second->first << ":" << mapair.second->second<<endl;
}

multimap案例:
//multimap最大的特点就是一个key对应多个value ----> 可以用来对数据进行分组
// 一个公司有多个部门: sale 2人  development 1人  financial 2人
//人员信息有: 姓名 电话 工资
//通过multimap进行信息的插入 保存 显示 可以分部门显示员工的信息
class Person{
     public:
          string    name;
          int          age;
          string     tel;
          double  salary;
           Person(string name, int age){
             this->name = name;
              this->age = age; 
            }
}
 void main(){
     Person p1("zhang1", 32);    
     Person p2("zhang2", 33);    
     Person p3("wang1", 34);    
     Person p4("wang2", 35);    
     Person p5("wang3", 36);
     map<string, Person> map1;
     map1.inster(make_pair("sale", p1));   
     map1.inster(make_pair("sale", p2));   
     map1.inster(make_pair("development", p3));   
     map1.inster(make_pair("financial", p4));   
     map1.inster(make_pair("financial", p5));   

     //遍历
     for( map<string, Person>::iterator it = map1.begin(); it != map1.end; it++){
             cout<< it->first<<":"<<it->second.name<<endl;
     }
     //计数
     int num = map1.count("financial");
     cout<<"financial person counts:"<<num<<endl;
     
     //找指定键的所有值
     map<string, Person>::iterator it2 = map1.find("financial");
     int tag = 0;
     while(it2 != map1.end() && tag < num){
        cout << it2->first<<":"<< it2->second.name<<endl;
        it2++;
        tag++;
     }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL 容器 map multimap