unordered_map(2)

unordered_map 重载了[ ] 运算符。

mapped_type& operator[] ( const key_type& k );
mapped_type& operator[] ( key_type&& k );

访问元素:

如果k匹配到了容器中的元素,则该该函数返回其引用值的引用。

如果k与容器中任何元素都不匹配,则该函数使用该键插入一个新元素,并返回其引用值的引用。请注意,即使没有为元素指定映射值(使用其默认构造函数构造元素),这也会将容器大小增加1。

类似的成员函数unordered_map :: at在具有键的元素存在时具有相同的行为,但在不存在时抛出异常。

返回值:

对具有等于k键值元素映射值的引用。

如果插入了新元素,则使用allocator_traits <allocator_type> :: construct()分配其存储,这可能会在失败时抛出异常(对于默认分配器,如果分配请求不成功则抛出bad_alloc)。

例子:

#include<unordered_map>
#include<string>
#include<iostream>
int main()
{
    std::unordered_map<std::string,std::string> mymap;

    mymap["Bakery"]="Barbara";  // new element inserted
    mymap["Seafood"]="Lisa";  // new element inserted
    mymap["Produce"]="John";
    for(auto i=0;i<mymap.bucket_cout;i++){
        std::cout<<"bucket #"<<i<<":";
        for(auto local_it=mymap.begin(i);local_it!=mymap_end(i);local_it++)
            std::cout<<local_it->first()<<" :"<<local_it->second();
        std::cout<<std::endl;
        }
    std::string name=myamp["Bakery"]; // existing element accessed (read)
    mymap["Produce"]=name;// existing element accessed (written)

     mymap["Bakery"] = mymap["Produce"];   // existing elements accessed (read/written)

    name=mymap["Deli"]; // non-existing element: new element "Deli" inserted!
    
    return 0;
}

注意:unordered_set没有重载 [ ] 运算符,unordered_map提供 [ ]并不是用于顺序访问,而是根据const key_type& k 找到对应的value值。

unordered_map::operator=

copy (1)
unordered_map& operator= ( const unordered_map& ump );
move (2)
unordered_map& operator= ( unordered_map&& ump );
initializer list (3)
unordered_map& operator= ( intitializer_list<value_type> il );

unordered_map 重载=运算符,用于指定容器新的内容。

第一个执行复制赋值,它将ump的所有元素复制到容器对象中(ump保留其内容)。

第二个执行移动分配,其将ump的内容的所有权转移到对象。没有副本出现:内容被ump丢失。

第三个将初始化列表il的内容指定为容器对象的元素。

例子:

#include<iostream>
#include<string>
#include<unordered_map>
typedef std::unordered_map<std::string,std::string> stringmap;
stringmap merge (stringmap a,stringmap b) {
  stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}
int main()
{
    stringmap first,second,third;
    first={{"AAPL","APPLE"},{"MSFT","Microsoft"}};//list init
    second={{"good", "google"},{"orcl","oral"}};
    third=merge(first,second);//move
    first=third;//copy
    std::cout << "first contains:";
    for(auto& x:first)
    std::cout<<" " <<x.first()<< " "<<x.second();
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_set和unordered_map是C++标准库中的容器,用于存储和管理不重复的元素集合和键值对集合。 unordered_set是一个无序的集合容器,其中的元素是唯一的且无序的。它基于哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间O(1)。使用unordered_set可以快速判断一个元素是否存在于集合中。 unordered_map是一个无序的键值对容器,其中的键是唯一的且无序的。它也基于哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间O(1)。使用unordered_map可以根据键快速查找对应的值。 使用unordered_set和unordered_map时,需要包含头文件<unordered_set>和<unordered_map>。以下是它们的基本用法: 1. 创建容器: unordered_set<int> mySet; // 创建一个空的unordered_set unordered_map<string, int> myMap; // 创建一个空的unordered_map 2. 插入元素或键值对: mySet.insert(10); // 插入元素10到unordered_set中 myMap["apple"] = 5; // 插入键值对("apple", 5)到unordered_map中 3. 删除元素或键值对: mySet.erase(10); // 从unordered_set中删除元素10 myMap.erase("apple"); // 从unordered_map中删除键为"apple"的键值对 4. 查找元素或键值对: auto it = mySet.find(10); // 在unordered_set中查找元素10,返回迭代器 if (it != mySet.end()) { // 找到了元素 } auto it = myMap.find("apple"); // 在unordered_map中查找键为"apple"的键值对,返回迭代器 if (it != myMap.end()) { // 找到了键值对 int value = it->second; // 获取对应的值 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值