c++ map unordered_map

本文详细介绍了C++中map与unordered_map的数据结构特点、操作方法及性能对比,包括增删查改、查找、遍历、大小获取、空判断等常用操作,并分析了它们在不同场景下的适用性。

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

map

operator<的重载一定要定义成const。因为map内部实现时调用operator<的函数好像是const。

#include<string>
#include<iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<map>

using namespace std;

struct person
{
    string name;
    int age;

    person(string name, int age)
    {
        this->name =  name;
        this->age = age;
    }

    bool operator < (const person& p) const
    {
        return this->age < p.age;
    }
};

map<person,int> m;
int main()
{
    person p1("Tom1",20);
    person p2("Tom2",29);
    person p3("Tom3",22);
    person p4("Tom4",23);
    person p5("Tom5",24);
    m.insert(pair<person, int>(p3, 100));
    m.insert(pair<person, int>(p4, 100));
    m.insert(make_pair(p5, 100));
    m.insert(make_pair(p1, 100));

    m[p2] = 100;


    for(map<person, int>::iterator iter = m.begin(); iter != m.end(); iter++)
    {
        cout<<iter->first.name<<"\t"<<iter->first.age<<endl;
    }

    getchar();
    return 0;
}

 

unordered_map

哈希map使用上和map区别不大,差别主要在性能上。

map采用红黑树的方式,而hash_map采用哈希的方法,

插入::   所以map的插入和删除速率要比hash_map高,hash_map要做冲突处理。

查找::   但是查找上hash_map就要比map的性能高很多,因为是哈希,所以可以直接按照内容找到,即查找的复杂度是O(1)。

#include<string> 
#include<iostream>
#include<unordered_map> #include <algorithm> using namespace std; void myfunc( const pair< int, int >& obj) { std::cout << "first=" << obj.first << "second=" << obj.second <<std::endl; } unordered_map<int,int> m; int main() { m.insert(pair<int, int>(1, 100)); m.insert(pair<int, int>(9, 100)); m.insert(make_pair(3, 100)); m.insert(make_pair(4, 100)); m[8] = 100; for(auto iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first<<"\t"<<iter->second<<endl; } for_each(m.begin(), m.end(), myfunc); getchar(); return 0; }

 

增  insert     数组方式object[key_value] = value;
创建map的成员::
方法一、插入value_type         
map<int,string>::value_type   value(1,"billchen");
object.insert(value);
也可以用   value.insert(map<int,string>::value_type(1,"billchen"));作用是一样的。  
方法二、插入pair或make_pair数据
map<int,string>   object;
object.insert(pair<int,string>(1,"billchen"));
或者
object.insert(make_pair(1,"billchen"));                   
/*
      其中pair是一个模板pair<T1,T2>  pvalue(v1,v2);表示创建一个pair对象,
       v1和v2的类型是T1和T2,值是v1和v2。
       可以取出v1和v2的值,
        分别是pvalue.first  为  v1
        pvalue.second   为   v2
*/
/*
        其中make_pair(v1,v2);表示以v1,v2创建一个新的pair对象,类型是v1和v2的类型。
         一般用在map的insert方法中,创建临时对象。
*/
 
insert有可能因为主键冲突,插入不了,可以通过返回值判断:
std::pair<std::tr1::unordered_map<std::string, std::string>::iterator,bool> ret  =                       ssmap.insert(std::make_pair("cccc","ccccc1"));
if(ret.second){
       std::cout << "insert succ" << std::endl;
}   
else{
       std::cout << "insert failed" << std::endl;
}   
方法三、数组方式
map<int,string>    object;
object[1]="billchen";         //object[key_value]=value;               (在key值对应的空间填写数据)
 
删除map中的一个元素,使用erase
方法一、通过迭代器删除         
map<int,string>::iterator   iter = object.find(key);                    
if(iter != object.end())
{
      object.erase(iter);                        
}
方法二、通过key值删除
object.erase(key);                   //删除成功,返回1,删除失败,返回0
删除map中的一段,如迭代器iter_begin到iter_end的元素:
iter_begin = object.find(key_value1);
iter_end = object.find(key_value2);
if(iter_begin != object.end() && iter_end != object.end())
{
      object.erase(iter_begin,iter_end);
}
删除所有元素使用erase:
方法一、使用迭代器,遍历删除         
map<int,string>::iterator iter=object.begin();
while(iter!=object.end())
{
        object.erase(iter++);    //这样做,是为了防止迭代器失效。
}
方法二、调用clear接口
object.clear();
                                           
修改map中成员的值(迭代器):
map<int,string>::iterator   iter = object.find(key);
iter->second="wang";
修改map中成员的值(数组方式):
map[key] = "wang";
例如:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(int argc,char *argv[])
{
        int i;
        map<char,string> obj;
        map<char,string>::value_type mem1('a',"billchen");
        obj.insert(mem1);
        map<char,string>::iterator iter = obj.begin();
        while(iter != obj.end())        //修改
        {   
                iter->second="wang";
                iter++;
        }   
        iter=obj.begin();
        while(iter != obj.end())        //遍历
        {   
                cout << iter->first << endl;
                cout << iter->second << endl;
                iter++;
        }   
        return 0;
}
 
查   find
查找使用find(主键);
object.find(主键);
返回值为iterator(迭代器),
if(object.find(主键) == object.end())
{
       cout << "没有找到" <<endl;
}
 
遍历
遍历方法1:    通过迭代器
map<int,string>::iterator iter=object.begin();
while(iter!=object.end())
{
       cout<<iter->first<<iter->second等等<<endl;
       iter++;
}
适当的情况下,可以使用数组的方式来进行map的遍历。
 
获取map中成员的个数  size()
获取map中元素的个数:
map<int,string> obj;
map<int,string>::value_type mem1(1,"billchen");
map<int,string>::value_type mem2(2,"chenbaihu");
obj.insert(mem1);
obj.insert(mem2);
cout << obj.size() <<endl;
 
判断map是否为空,使用empty                                                 
为空,返回true,不为空,返回false;

转载于:https://www.cnblogs.com/kaishan1990/p/5113927.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值