首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
<< *p; } // p goes out of scope here, the memory gets deallocated, the object gets // destroyed39

A unique pointer can not be copied, only moved. To have multiple shared pointers pointing at the same object, we would write:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::shared_ptr p1(new int{ 123 });
    std::shared_ptr p2 = p1;
    std::shared_ptr p3 = p1;
} // when the last shared pointer goes out of scope, the memory gets // deallocated39

Shared pointers can be copied. It is said they share ownership of the object. When the last shared pointer gets out of scope, the pointed-to object gets destroyed, and the memory gets deallocated.gV0拜客生活常识网

11.9 std::unordered_set

std::set使用红黑树为底层数据结构,std::unordered_set以哈希映射实现物理存储。gV0拜客生活常识网

The std::unordered_set is a container that allows for constant time insertion, searching, and removal of elements. This container is implemented as an array of buckets of linked lists. The hash value of each element is calculated (hashed), and the object is placed into an appropriate bucket based on the hash value. The object themselves are not sorted in any particular order. To define an unordered set, we need to include the header. Example:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
    for (auto el : myunorderedset)
    {
        std::cout << el << '
';
    }
}

The values are not sorted but are unique. To insert single or multiple values into an unordered_set, we use the .insert() member function:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
    myunorderedset.insert(6); // insert a single value
    myunorderedset.insert({ 8, 15, 20 }); // insert multiple values
    for (auto el : myunorderedset)
    {
        std::cout << el << '
';
    }
}

To delete a value from an unordered set, we use the .erase() member function:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
    myunorderedset.erase(-4); // erase a single value
    for (auto el : myunorderedset)
    {
        std::cout << el << '
';
    }
}

11.10 std::unordered_map

std::map使用红黑树为底层数据结构,std::unordered_map以哈希映射实现物理存储。gV0拜客生活常识网

Similar to std::unordered_set, there is also an std::unordered_map , an unordered container of key-value pairs with unique keys. This container also allows for fast insertion, searching, and removal of elements. The container is also data is also implemented as buckets. What element goes into what bucket depends on the element’s key hash value. To define an unordered map, we include the header. Example:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::unordered_map myunorderedmap = { {'a', 1}, {'b', 2}, {'c', 5} };
    for (auto el : myunorderedmap)
    {
        std::cout << el.first << ' '<< el.second << '
';
    }
}

Here we initialize an unordered map with key-value pairs. In the range-based for loop, we print both the key and the value. Map elements are pairs. Pairs have member functions .first for accessing a key and .second for accessing a value. To insert an element into a map we can use the member function .insert() member function:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::unordered_map myunorderedmap = { {'a', 1}, {'b', 2}, {'c', 5} };
    myunorderedmap.insert({ 'd', 10 });
    for (auto el : myunorderedmap)
    {
        std::cout << el.first << ' '<< el.second << '
';
    }
}

We can also use the map’s operator [] to insert an element. Normally, this operator is used to access an element value by key. However, if the key does not exist, the operator inserts a new element into the map:

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。