map的代码实例

#include <iostream>
#include <string>
#include <map>
#include <set>
 
using namespace std;
 
int main()
{
    map<int,string> t_map1;
 
    pair<map<int,string>::iterator,bool> t_pairResult;
 
    //插入数据方式一
    t_pairResult = t_map1.insert(pair<int,string>(1,"Hello"));
 
    if(t_pairResult.second)
    {
        cout << "insert date success!" << endl;
        cout << "map1 key : " << t_pairResult.first->first << " map1 value : " << t_pairResult.first->second << endl;
    }
    else
    {
        cout << "insert date failed!" << endl;
    }
 
    //插入数据方式二
    t_pairResult = t_map1.insert(map<int,string>::value_type(2,"World"));
 
    if(t_pairResult.second)
    {
        cout << "insert date success!" << endl;
        cout << "map1 key : " << t_pairResult.first->first << " map1 value : " << t_pairResult.first->second << endl;
    }
    else
    {
        cout << "insert date failed!" << endl;
    }
 
    cout << "&&&&&&&&&&&&" << endl;
 
    //插入数据方式三
    t_map1[3] = "how are you?";
    t_map1[3] = "How are you!";
    t_map1[4] = "Fine,thank you!";
    t_map1[5] = "What is your name?";
    t_map1[9] = "OK";
    t_map1[10] = "Good";
 
    map<int,string>::iterator t_iter1;
    map<int,string>::iterator t_iter2;
 
    for(t_iter1 = t_map1.begin();t_iter1 != t_map1.end();t_iter1++)
    {
        cout << "map1 key : " << t_iter1->first << " map1 value : " << (*t_iter1).second << endl;
    }
 
    if(t_map1.count(4) == 1)
    {
        cout << "key 4 is in map1" << endl;
    }
    else
    {
        cout << "key 4 is not in map1" << endl;
    }
 
    if(t_map1.find(5) != t_map1.end())
    {
        cout << "find key 5 in map1 " << endl;
        cout << "key 5 value : " << t_map1.find(5)->second << endl;
    }
    else
    {
        cout << "not found!" << endl;
    }
 
    t_iter2 = t_map1.lower_bound(5);
 
    if(t_iter2 != t_map1.end())
    {
        cout << "t_iter2->first : " << t_iter2->first << " t_iter2->second : " << t_iter2->second << endl;
    }
    else
    {
        cout << "not found!" << endl;
    }
 
    cout << "%%%%%%%%%%%%%%" << endl;
 
    t_iter2 = t_map1.upper_bound(5);
 
    if(t_iter2 != t_map1.end())
    {
        cout << "t_iter2->first : " << t_iter2->first << " t_iter2->second : " << t_iter2->second << endl;
    }
    else
    {
        cout << "not found!" << endl;
    }
 
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
 
    pair<map<int,string>::iterator,map<int,string>::iterator> t_pair2;
 
    t_pair2 = t_map1.equal_range(6);
 
    cout << "t_pair2->first->first key : " << t_pair2.first->first << " t_pair2->first->second value : " << t_pair2.first->second << endl;
    cout << "t_pair2->second->first key : " << t_pair2.second->first << " t_pair2->second->second value : " << t_pair2.second->second << endl;

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值