C++ map

本文深入探讨了C++ STL中的map和multimap容器使用方法,包括元素的插入、查找以及遍历等操作,同时提供了详细的代码示例,帮助读者理解和掌握这些容器的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

map :键值对
map<int,string> m;
mp.insert(make_pair(1,“faith1”));
mp.insert(pair<int,string>(3,“faith3”));
mp.insert(map<int,string>::value_type(2,“faith2”));
mp[4] = “faith4”;

insert是函数,其返回值可以判断是否插入成功
[]运算符重载:如果数据存在,则覆盖原有的值,如果数据不存在,则插入一条新的数据

multimap:允许键重复

#include <iostream>
#include <set>
#include <map>
#include <string>
using namespace std;
void fun1()
{
    set<int> s;
    s.insert(1);
    std::pair<set<int>::iterator,bool> ret = s.insert(3);
    if(ret.second)
        cout<<"插入成功"<<*(ret.first)<<endl;
    else
        cout<<"插入失败"<<endl;
}
void fun2()
{
    set<int> s;
    s.insert(1);
    s.insert(3);
    s.insert(5);
    s.insert(2);
    s.insert(6);
    s.insert(8);
//    set<int>::iterator it = s.find(11);
//    if(it!=s.end())
//        cout<<"找到"<<endl;
//    else
//        cout<<"未找到"<<endl;


//     set<int>::iterator it = s.lower_bound(12);
//     cout<<*it<<endl;


     set<int>::iterator it = s.upper_bound(17);
     if(it!=s.end())
         cout<<*it<<endl;
     else
         cout<<"未找到"<<endl;

}
void fun3()
{
    multiset<int> s;
    s.insert(1);
    s.insert(1);
    s.insert(1);
    s.insert(1);
    s.insert(3);
    s.insert(5);
    s.insert(2);
    s.insert(6);
    s.insert(8);
    std::pair<multiset<int>::iterator,multiset<int>::iterator> it = s.equal_range(3);
    multiset<int>::iterator it1 = it.first;
     multiset<int>::iterator it2 = it.second;

     while(it1!=it2)
     {
         cout<<*it1<<endl;
         it1++;
     }


}
void print(map<int,string> &mp)
{
    map<int,string>::iterator it = mp.begin();
    while(it!=mp.end())
    {
        cout<<"id :"<<it->first<<"name :"<<it->second<<endl;
        it++;
    }
}

void fun4()
{
    map<int,string> mp;
    mp.insert(make_pair(1,"faith1"));
    mp.insert(pair<int,string>(3,"faith3"));
    mp.insert(map<int,string>::value_type(2,"faith2"));

    mp[4] = "faith4";
    //mp.erase(mp.begin());
    print(mp);
}
void printm(multimap<string,int> &mp)
{
    multimap<string,int>::iterator it = mp.begin();
    while(it!=mp.end())
    {
        cout<<"id :"<<it->second<<"name :"<<it->first<<endl;
        it++;
    }
}

void fun5()
{
    multimap<string,int> mp;
    mp.insert(make_pair("faith1",1));
    mp.insert(make_pair("faith1",1));
    mp.insert(make_pair("faith1",1));
    mp.insert(make_pair("faith1",1));
    printm(mp);
}

int main1(int argc, char *argv[])
{
   //fun1();
   //fun2();
    //fun3();
    //fun4();
    fun5();
    return 0;
}
### C++ 中 `map` 容器的使用方法 #### 创建和初始化 `map` 在C++中,`map` 是标准模板库(STL)的一部分,用于存储键值对的数据结构。创建一个简单的整数到字符串映射可以如下所示: ```cpp #include <iostream> #include <map> using namespace std; // 初始化 map 的几种方式 void createAndInitializeMap() { // 方式一:通过下标操作符 [] map<int, string> stuMap; stuMap[1001] = "Jason"; // 方式二:使用 insert 方法配合 pair 或 make_pair 插入元素 stuMap.insert(pair<int, string>(1002, "Helen")); stuMap.insert(make_pair(1003, "Steve"))[^2]; } ``` #### 遍历 `map` 为了遍历整个 `map`,可以采用多种方式进行访问。这里展示如何利用范围for循环以及迭代器来实现这一点。 ```cpp // 使用范围 for 循环遍历 map void traverseWithRangeFor(map<int, string>& m) { for (auto& elem : m) { cout << "Key: " << elem.first << ", Value: " << elem.second << endl; } } // 使用迭代器遍历 map 并结合 STL 算法 like std::for_each void traverseWithIterator(map<int, string>& m) { auto callback = [](const pair<const int, string>& p) { cout << "Key: " << p.first << ", Value: " << p.second << endl; }; for_each(m.begin(), m.end(), callback)[^1]; } ``` #### 访问特定项 当需要获取某个具体位置上的值时,可以直接通过键名索引或者find函数查找对应的记录。 ```cpp string getValueByKey(const map<int, string>& m, const int key) { if (m.find(key) != m.cend()) { return (*m.find(key)).second; } else { throw runtime_error("Key not found"); } } ``` 上述代码片段展示了基本的操作模式,包括但不限于定义、填充、检索和遍历关联数组型容器——即 `map`。这些功能使得处理具有唯一性的键与其相关联的信息变得简单而高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值