unordered_map用法学习---------------C++数据结构

unordered_map用法学习

功能

unordered_map存储机制是哈希表,,即unordered_map内部元素是无序的。

头文件:

#include<unordered_map>

介绍:

std::unordered_map 就是以key来查找value而设计,不会根据key排序。

使用样例

样例1:


 unordered_map<int, int> map;
        for (int i=0; i<list.size(); i++){
            map[i] = list[i];
        }
        cout << map[0] << endl;
        for (unordered_map<int, int>::iterator i = map.begin(); i != map.end(); i++){
            cout << i->first << ' ' << i->second << endl;
        }
        if (map.find(3) != map.end()) {
            cout << "find key=" << map.find(3)->first << ", value=" << map.find(3)->second << endl;
        }
        if (map.count(5) > 0) {
            cout << "find 5: " << map.count(5) << endl;
        }

样例2:

#include <iostream>  
#include <unordered_map>  
#include <map>
#include <string>  
using namespace std;  
int main()  
{  
	//注意:C++11才开始支持括号初始化
    unordered_map<int, string> myMap={{ 5, "张大" },{ 6, "李五" }};//使用{}赋值
    myMap[2] = "李四";  //使用[ ]进行单个插入,若已存在键值2,则赋值修改,若无则插入。
    myMap.insert(pair<int, string>(3, "陈二"));//使用insert和pair插入
  
	//遍历输出+迭代器的使用
    auto iter = myMap.begin();//auto自动识别为迭代器类型unordered_map<int,string>::iterator
    while (iter!= myMap.end())
    {  
        cout << iter->first << "," << iter->second << endl;  
        ++iter;  
    }  
	
	//查找元素并输出+迭代器的使用
    auto iterator = myMap.find(2);//find()返回一个指向2的迭代器
    if (iterator != myMap.end())
	    cout << endl<< iterator->first << "," << iterator->second << endl;  
    system("pause");  
    return 0;  
}
————————————————
版权声明:本文为CSDN博主「别说话写代码」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21997625/article/details/84672775

遍历:

unordered_map<key,T>::iterator it;
    (*it).first;   //the key value
    (*it).second   //the mapped value
    for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
          cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;

    //也可以这样
    for(auto& v : mp)
        print v.first and v.second
————————————————
版权声明:本文为CSDN博主「zou_albert」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zou_albert/article/details/106983268

已知功能实例(可输出):

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
    vector<int> x = {2,3,4,5,6,7};
    for(vector<int>::iterator it = x.begin();it!=x.end();it++)
        cout<<(*it)<<endl;
    unordered_map<int,string> id_name={{1,"xiaorougan"},{2,"zhangyuntao"}};
    for(auto &it:id_name)
        cout<<it.second<<endl;
        for (auto it:x)
            cout<<it<<endl;
    id_name[5]="add5";
    id_name.insert(pair<int,string>(3,"wuzhijian"));
    if(id_name.find(2)!=id_name.end())
        cout<<"find the aim!"<<endl;
        auto vip = id_name.find(2);
        cout<<"find(2):"<<endl<<vip->first<<vip->second<<endl;
        vip->second="changedzhang";
        cout<<"------------------for--------------------"<<endl;
    for(int i=0;i<2;i++)
    for(unordered_map<int,string>::iterator it=id_name.begin();it!=id_name.end();it++)
        {
            cout<<it->first<<" "<<it->second<<endl;
            cout<<(*it).first<<" "<<it->second<<endl;
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_mapC++标准库中的一个关联容器,它提供了一种键值对的映射关系。unordered_map使用哈希表来实现,因此可以在常数时间内进行插入、删除和查找操作。下面是unordered_map的一些特点和用法: 1. 特点: - 键值对的存储:unordered_map中的每个元素都是一个键值对,其中键是唯一的,而值可以重复。 - 哈希表实现:unordered_map使用哈希表来存储键值对,因此插入、删除和查找操作的平均时间复杂度为O(1)。 - 无序性:unordered_map中的元素是无序的,即元素的顺序与插入的顺序无关。 2. 使用方法: - 头文件:使用unordered_map需要包含头文件<unordered_map>。 - 定义和初始化:可以使用以下方式定义和初始化unordered_map对象: ```cpp std::unordered_map<KeyType, ValueType> myMap; // 默认构造函数 std::unordered_map<KeyType, ValueType> myMap = { {key1, value1}, {key2, value2}, ... }; // 初始化列表 ``` - 插入元素:可以使用insert()函数或者下标操作符[]来插入元素: ```cpp myMap.insert(std::make_pair(key, value)); // 使用insert()函数插入元素 myMap[key] = value; // 使用下标操作符[]插入元素 ``` - 删除元素:可以使用erase()函数来删除指定键的元素: ```cpp myMap.erase(key); // 删除指定键的元素 ``` - 查找元素:可以使用find()函数来查找指定键的元素: ```cpp auto it = myMap.find(key); // 查找指定键的元素,返回一个迭代器 if (it != myMap.end()) { // 找到了指定键的元素 // 使用it->first访问键,使用it->second访问值 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值