12、stl-map

map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对。它提供基于key的快速检索能力。
map中key值是唯一的。集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。
map的具体实现采用红黑树变体的平衡二叉树的数据结构。在插入操作和删除操作上比vector快。
map可以直接存取key所对应的value,支持[]操作符,如map[key]=value。
multimap与map的区别:map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符。
#include


#include <iostream>
using namespace std;
#include "map"
#include "string"


//map元素的添加/遍历/删除基本操作
void main1101()
{
    map<int, string> map1;

    //方法1
    map1.insert(pair<int, string>(1,"teacher01") );
    map1.insert(pair<int, string>(2,"teacher02") );

    //方法2 
    map1.insert(make_pair(3, "teacher04") );
    map1.insert(make_pair(4, "teacher05") );

    //方法3 
    map1.insert(map<int, string>::value_type(5, "teacher05") );
    map1.insert(map<int, string>::value_type(6, "teacher06") );

    //方法4
    map1[7] = "teacher07";
    map1[8] = "teacher08";

    //map1['z'] = "teacher08";

    //容器的遍历
    for (map<int, string>::iterator it = map1.begin(); it!=map1.end(); it++ )
    {
        cout << it->first << "\t" << it->second << endl;
    }
    cout << "遍历结束" << endl;

    //容器元素的删除
    while (!map1.empty())
    {
        map<int, string>::iterator it = map1.begin();
        cout << it->first << "\t" << it->second << endl;
        map1.erase(it);
    }
}

//插入的四种方法 异同
//前三种方法 返回值为pair<iterator,bool> 若key已经存在 则报错
//  方法四                                 若key已经存在,则修改                                    
void main1102()
{
    map<int, string> map1;

    //typedef pair<iterator, bool> _Pairib;

    //方法1
    pair<map<int, string>::iterator, bool>  mypair1 =  map1.insert(pair<int, string>(1,"teacher01") );
    map1.insert(pair<int, string>(2,"teacher02") );

    //方法2 
    pair<map<int, string>::iterator, bool>  mypair3 = map1.insert(make_pair(3, "teacher04") );
    map1.insert(make_pair(4, "teacher05") );

    //方法3 
    pair<map<int, string>::iterator, bool>  mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05") );
    if (mypair5.second != true)
    {
        cout << "key 5 插入失败" << endl;
    }
    else
    {
        cout << mypair5.first->first << "\t" <<  mypair5.first->second <<endl;
    }


    pair<map<int, string>::iterator, bool>  mypair6 =  map1.insert(map<int, string>::value_type(5, "teacher55") );
    if (mypair6.second != true)
    {
        cout << "key 5 插入失败" << endl;
    }
    else
    {
        cout << mypair6.first->first << "\t" <<  mypair6.first->second <<endl;
    }

    //方法4
    map1[7] = "teacher07";
    map1[7] = "teacher77";

    //容器的遍历
    for (map<int, string>::iterator it = map1.begin(); it!=map1.end(); it++ )
    {
        cout << it->first << "\t" << it->second << endl;
    }
    cout << "遍历结束" << endl;

}

void main1103()
{
    map<int, string> map1;

    //方法1
    map1.insert(pair<int, string>(1,"teacher01") );
    map1.insert(pair<int, string>(2,"teacher02") );

    //方法2 
    map1.insert(make_pair(3, "teacher04") );
    map1.insert(make_pair(4, "teacher05") );

    //方法3 
    map1.insert(map<int, string>::value_type(5, "teacher05") );
    map1.insert(map<int, string>::value_type(6, "teacher06") );

    //方法4
    map1[7] = "teacher07";
    map1[8] = "teacher08";

    //容器的遍历
    for (map<int, string>::iterator it = map1.begin(); it!=map1.end(); it++ )
    {
        cout << it->first << "\t" << it->second << endl;
    }
    cout << "遍历结束" << endl;

    //map的查找 //异常处理
    map<int, string>::iterator it2 = map1.find(100);
    if (it2 == map1.end())
    {
        cout << "key 100 的值 不存在" << endl;
    }
    else
    {
        cout << it2->first << "\t" << it2->second << endl;
    }

    //equal_range //异常处理
    pair<map<int, string>::iterator , map<int, string>::iterator> mypair = map1.equal_range(5); //返回两个迭代器 形成一个 pair
    //第一个迭代器 >= 5的 位置 
    //第一个迭代器 = 5的 位置 

    if (mypair.first == map1.end() )
    {
        cout << "第一个迭代器 >= 5的 位置 不存在" << endl;
    }
    else
    {
        cout << mypair.first->first << "\t" << mypair.first->second << endl;
    }

    //使用第二个迭代器
    if (mypair.second == map1.end() )
    {
        cout << "第二个迭代器 > 5的 位置 不存在" << endl;
    }
    else
    {
        cout << mypair.second->first << "\t" << mypair.second->second << endl;
    }


}
void main111111()
{
    //main1101();
    //main1102();
    main1103();
    cout<<"hello..."<<endl;
    system("pause");
    return ;
}

#include <iostream>
using namespace std;
#include "map"
#include "string"

//Multimap 案例:
//1个key值可以对应多个valude  =分组 
//公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) 
//人员信息有:姓名,年龄,电话、工资等组成
//通过 multimap进行 信息的插入、保存、显示
//分部门显示员工信息 

class Person
{
public:
    string  name;
    int     age;
    string  tel;
    double  saly;
};

void main1201()
{
    Person p1, p2, p3, p4, p5;

    p1.name = "王1";
    p1.age = 31;

    p2.name = "王2";
    p2.age = 32;

    p3.name = "张3";
    p3.age = 33;

    p4.name = "张4";
    p4.age = 34;

    p5.name = "赵5";
    p5.age = 35;

    multimap<string, Person> map2;
    //sale部门
    map2.insert(make_pair("sale", p1) );
    map2.insert(make_pair("sale", p2) );

    //development 部门
    map2.insert(make_pair("development", p3) );
    map2.insert(make_pair("development", p4) );

    //Financial 部门
    map2.insert(make_pair("Financial", p5) );


    for( multimap<string, Person>::iterator it=map2.begin(); it!=map2.end(); it++)
    {
        cout << it->first << "\t" << it->second.name << endl;
    }
    cout << "遍历结束" << endl;

    //
    int num2 = map2.count("development");
    cout << "development部门人数==>" << num2 << endl;

    cout << "development部门员工信息" << endl;
    multimap<string, Person>::iterator it2 = map2.find("development");

    int tag = 0;
    while (it2 != map2.end() && tag < num2)
    {
        cout << it2->first << "\t" << it2->second.name << endl;
        it2 ++;
        tag ++;
    }

}


//age = 32修改成 name32
void main1202()
{
    Person p1, p2, p3, p4, p5;

    p1.name = "王1";
    p1.age = 31;

    p2.name = "王2";
    p2.age = 32;

    p3.name = "张3";
    p3.age = 33;

    p4.name = "张4";
    p4.age = 34;

    p5.name = "赵5";
    p5.age = 35;

    multimap<string, Person> map2;
    //sale部门
    map2.insert(make_pair("sale", p1) );
    map2.insert(make_pair("sale", p2) );

    //development 部门
    map2.insert(make_pair("development", p3) );
    map2.insert(make_pair("development", p4) );

    //Financial 部门
    map2.insert(make_pair("Financial", p5) );


    cout << "\n按照条件 检索数据 进行修改 " << endl;
    for( multimap<string, Person>::iterator it=map2.begin(); it!=map2.end(); it++)
    {
        //cout << it->first << "\t" << it->second.name << endl;
        if (it->second.age == 32 )
        {
            it->second.name = "name32";
        }
    }


    for( multimap<string, Person>::iterator it=map2.begin(); it!=map2.end(); it++)
    {
        cout << it->first << "\t" << it->second.name << endl;
    }


}

void main1212()
{
    //main1201();
    main1202();
    system("pause");
    return ;
}

猜你喜欢

转载自blog.csdn.net/u014749668/article/details/82259249
今日推荐