C++ primer 薄片系列之 map 三两事

map 向map中写入一个pair,如果这个pair的关键字已经存在在这个map中,则原map不受写入影响。

    std::map<int,int> am1 = {
   
   {
   
   1,2}};
    auto it = am1.insert({
   
   1,5});
    std::cout << it.first->first << " "<< it.first->second << "  " << it.second << std::endl; //insert 操作返回一个pair, pair第一个元素是map的迭代器,指向被插入的元素,第二个元素为布尔变量,true或者false
    for(auto i = am1.begin();i!=am1.end();i++)
        std::cout << i->first << "  " << i->second << std::endl; // 1 2
    am1.erase();//删除一个关键字为k的元素,返回被删除元素的数量(0 或者 1)
                 //删除迭代器p指定的元素,成功则返回p之后的元素的迭代器,否则返回 am1.end()
                 //范围型,erase(p,e), 删除左闭合区间[p,e), 返回e

multiset OR multimap 用count检测关键字数量,用find 查找关键字

        std::multimap<int,int> mmm = {
   
   {
   
   1,1},{
   
   1,2},{
   
   1,3}};
        auto cnt = mmm.count(1);
        auto iter = mmm.find(1);
        while(cnt)
        {
            std::cout << iter->second << std::endl;
            iter++;
            cnt--;
         }

无序容器
C++11定义了四种 无序容器, unordered_map, unordered_set, unordered_multimap, unordered_multiset
存储上,无序容器被组织成一组桶, 每个桶保存0或者多个元素,无序容器通过哈希函数将元素映射到桶中。访问元素,容器需要首先计算元素的哈希值,它指出元素位于哪个桶里,容器将具有一个特定哈希值的所有元素保存在同一个桶里。如果允许重复关键字,所有具有相同关 键字的元素都会在同一个桶中。其性能取决于哈希函数的质量和桶的数量及大小

 成员函数 max_bucket_count()最大的桶数量
     max_load_factor()最大负载因子 1
     bucket_count()当前桶的数量
     load_factor()当前负载因子, 每个桶的平均元素数量
     bucket_size(n)第n个桶中元素的个数
     rehash(n) 重组存储
 unordered_map<int,int> ui =  {
   
   {
   
   1,1},{
   
   2,2},{
   
   3,3}};
    cout << ui.max_bucket_count() << " "
         << ui.max_load_factor()  << endl; //输出1152921504606846975 1
    cout << ui.bucket_count() << ui.load_factor() << endl;// 当前有多少个桶  输出 7 和 0.428571
    cout << ui.bucket_size(0)  
         << "  " << ui.bucket_size(1)
         << "  " << ui.bucket_size(2)
         << "  " << ui.bucket_size(3)
         << "  " << ui.bucket_size(4)
         << "  " << ui.bucket_size(5)
         << "  " << ui.bucket_size(6) << endl; //输出 0 1 1 1 0 0 0
    ui.rehash(3); //重新存储
    cout << ui.bucket_count() << ui.load_factor() << endl; //输出5 和 0.6
    cout << ui.bucket_size(0)  
        << "  " << ui.bucket_size(1)
        << "  " << ui.bucket_size(2)
        << "  " << ui.bucket_size(3)
        << "  " << ui.bucket_size(4)
        << "  " << ui.load_factor() << endl;// 输出 0 1 1 1 0

默认情况下,无序容器用关键字类型的==操作符比较元素,用hash< key_type > 类型的对象 生个每个元素的哈希值。标准库为内置类型提供了hash模板。
对于自定义类型,可以为自定义类型提供 == 和哈希函数替换。

#include<unordered_set>
struct S
{
    S(int k):k_(k)
    {

    }
    int k_;
    bool operator==(const S&rhs) const
    {
        return k_ ==  rhs.k_;
    }
    S(){k_ = 0;}
};
struct HashFunc
{
    size_t operator()(const S &sd) const 
    {
         return std::hash<int>()(sd.k_);

    }
};
struct EqFunc
{
    bool operator()(const S &lhs, const S &rhs) 
    {
         return lhs.k_ ==  rhs.k_;

    }
};
size_t hasher(const S &sd) 
{
     return std::hash<int>()(sd.k_);

}
bool eqOp(const S &lhs, const S &rhs) 
{
     return lhs.k_ ==  rhs.k_;

}
int main()
{
    std::unordered_multiset<S, HashFunc,EqFunc> h;
    std::unordered_multiset<S, decltype(hasher)*,decltype(eqOp)*> h2(1,hasher, eqOp);//这里必须要以定义即初始化的方式创建h2, 否则不能向h2 中插入数据
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jxhaha/article/details/78416373
今日推荐