C + + unordered associative container __c++

Source: Internet
Author: User
Tags hash

The C + + 11 standard Adds a new four unordered associative containers, respectively
unordered_map Mapping
unordered_multimap Multiple mappings
Unordered_set Collection
Unordered_multiset Multiple Sets

The same as the basic functions of the normal map and set, but the internal implementation is completely different.
Map and set are implemented in red and black trees
The Unordered_map and Unordered_set implementations are hash functions , so unordered associative containers do not sort the stored elements according to key values. unordered_map

Insert Action:

#include <bits/stdc++.h>
using namespace std;
typedef pair<string,size_t> PSS;
int main ()
{
    Ios::sync_with_stdio (false);

    Unordered_map<string,size_t> U_map;
    PSS p1={"EFG", 2};

    String s= "abc";
    u_map[s]++;//use subscript to insert

    U_map.insert (p1);//Use the Nsert function to insert a pair

    U_map.insert ({{"www", 2},{"Wqwe", 3}}); Inserts an initialization list


    vector<pss> vp={{"Wrrr", 2},{"TTT", 3}};

    U_map.insert (Vp.begin (), Vp.end ());//Insert an iterator scope for

    (auto x:u_map)
        cout<<x.first<< "" << x.second<<endl;
    return 0;
}

Find and delete operations

Lookup function: find

As with the Find method in other containers, the parameter content can be either an iterator or a key value.
Returns the iterator and returns the end of the iterator if the lookup content does not exist.

Unordered_map<string,size_t> U_map;
vector<pss> vp={{"Wrrr", 2},{"TTT", 3},{"www", 3},{"QQ", ten};
U_map.insert (Vp.begin (), Vp.end ());
Auto It=u_map.find ("QQ");
if (It!=u_map.end ())
    cout<<it->first<< "" <<it->second<<endl;

Delete function:earse

As with the Erase method in other containers, the contents of the argument can be either iterators or key values.

Unordered_map<string,size_t> U_map;
vector<pss> vp={{"Wrrr", 2},{"TTT", 3},{"www", 3},{"QQ", ten};
U_map.insert (Vp.begin (), Vp.end ());

U_map.erase ("www")//delete the for

(auto x:u_map)
    cout<<x.first<< "" <<x.second< directly by key value <endl;
Barrel Management

Because the implementation of unordered associative container is based on hash function, it is necessary to solve the conflict problem, and the method of solving the conflict is more commonly used with open address method and zipper law. In C + + unordered_map, the use of the "bucket" method, that is, the elements of conflict are stored in a bucket. Use the Zipper method to make an example.

The hash policy used in unordered_map can be customized by the user by a function overload.

Iterate through each bucket and output the elements in the bucket

    Unordered_map<string,size_t> U_map;
    vector<pss> vp={{"Wrrr", 2},{"TTT", 3},{"www", 3},{"QQ", ten};
    U_map.insert (Vp.begin (), Vp.end ());


    for (auto I=0;i<u_map.bucket_count (), i++)
    {for
        (auto It=u_map.begin (i); it!=u_map.end (i); it++)// Add parameters to the iterator, select the bucket number
        {
            cout<< "bucket:" <<i<< "<<it->first<<" "<<it- >second<<endl;
        }

    }

How to view the hash value of the content to be saved after the hash function is processed. You can use the hash_function function.

    typedef unordered_map<string,size_t> STRING_INT_MAP
    unordered_map<string,size_t> u_map;
    vector<pss> vp={{"Wrrr", 2},{"TTT", 3},{"www", 3},{"QQ", ten};
    U_map.insert (Vp.begin (), Vp.end ());

    String_int_map::hasher fn=u_map.hash_function ();

    for (auto It=vp.begin (); It!=vp.end (); it++)
    {
        cout<<it->first<< "the hash value is:" <<fn (it-> (a) <<endl;
    }

Custom Type

In unordered_map, you need to overload the = = operator and customize the hash function to operate on the struct or class.

#include <bits/stdc++.h>
using namespace std;

struct node
{
    string name;
    int number;
    Node () {}
    bool operator== (const node& N) const//overload = = operator
    {return
        name==n.name;
    }
};

struct myhash//Custom hash function
{
    size_t operator () (const node &n) const
    {return
        n.name.size ();/ According to the length of the name as the hash value
    }
};
int main ()
{
    Ios::sync_with_stdio (false);

    Unordered_map<node,size_t,myhash> U_map;

    node n;
    N.name= "Mike";
    n.number=100;

    U_map.insert ({n,10});


    return 0;
}

If a struct or class uses a system in which the hash value is taken as a member, you can do the following
For example, a struct is defined, it has num and res two int members, and now I want to store this structure in Unordered_set, where the hash value is implemented in the Res hashing function, and I do not want to write it myself, hoping to use the system's own hash function with int.

typedef unordered_set<int> USI;

struct node
{
    int num;
    int res;
    Node (int n,int r) {num = n, res = r;}
    BOOL operator== (const node& N) const//overloaded = = operator
    {return
        res==n.res;
    }
};
struct Myhash
{
    size_t operator () (const node &n) const
    {
        usi::hasher fn= usi (). Hash_function ( );
        USI is a unordered_set<int>
        //Select its hash function as the hash function in our definition structure,
        return fn (n.res), and/or the RES value can be returned after processing
};

Unordered_set's operations and properties are roughly similar to those of unordered_map.

In terms of efficiency, the operation time complexity of the hash is O (1), but in the real case, the hash function needs to be detailed design, otherwise the efficiency and memory costs will be very large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.