Set the difference and Unordered_Set

References:
[. 1] cppreference introduced to the SET std ::: https://zh.cppreference.com/w/cpp/container/set
[2] cppreference std :: pair in the introduction unordered_set: HTTPS: // zh.cppreference.com/w/cpp/container/unordered_set
[. 3] https://blog.csdn.net/haluoluo211/article/details/82468061
[. 4] https://stackoverflow.com/questions/1349734/why- would-anyone-use-set- instead-of-unordered-set / 52203931 # 52203931

std :: set is associated with a container, comprising Key ordered set of object type. Sort by comparison function Compare. Search, removal and insertion has logarithmic complexity. typically set to achieve red-black tree, red-black tree having the function of automatic sorting, thus all the internal data set at any time, is ordered.
std :: unordered_set Key type is uniquely associated with a container containing a collection of objects, dependent on the hash table. Search, insertion and removal have an average of constant time complexity. Internally, the elements are not sorted in any particular order, but they are organized into the tub, which elements are put into the tub completely dependent on its hash value. Allows quick access to individual elements, because once the hash, we can accurately refer to the elements are placed in the barrel. Can not be modified container element (even by non-const iterator), because the amendment might change the elements of the hash, and destroy the vessel. The cost of consuming more memory, no automatic sorting function. The underlying implementation, using a relatively large range index array to store elements, the formation of many of the tub, using the hash function to the key is mapped to a different storage area.

They differ shown below, the original is [3]:

The following cases are generally used set [4]:

  1. Need ordered data (different elements).
  2. You need in order to print / access data.
  3. Required elements predecessor or successor.

The following cases are generally used unordered_set:

  1. The need to retain a different set of elements, does not require sorting.
  2. Need access to a single element, do not traverse.

For example:
the SET:

Input : 1, 8, 2, 5, 3, 9

Output : 1, 2, 3, 5, 8, 9

Unordered_set:

Input : 1, 8, 2, 5, 3, 9

Output: 9 3 1 8 2 5 (Effect of this sequence should be the hash function)

Note :( set would be more convenient in some cases than unordered_set), such as using the vector as a key (Key) time.

set<vector<int>> s;
s.insert({1, 2});
s.insert({1, 3});
s.insert({1, 2});

for(const auto& vec:s)
    cout<<vec<<endl;   // I have override << for vector
// 1 2
// 1 3 

Because vector overloaded Less than <, the vector in the set It can exist as a key.
But because there is no hash function vector, you want to use unordered_set <vector > Need to define one of the following examples:

struct VectorHash {
    size_t operator()(const std::vector<int>& v) const {
        std::hash<int> hasher;
        size_t seed = 0;
        for (int i : v) {
            seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }
        return seed;
    }
};

vector<vector<int>> two(){
    //unordered_set<vector<int>> s; // error vector<int> doesn't  have hash function
    unordered_set<vector<int>, VectorHash> s;
    s.insert({1, 2});
    s.insert({1, 3});
    s.insert({1, 2});

    for(const auto& vec:s)
        cout<<vec<<endl;
    // 1 2
    // 1 3
}

There is also a need to address the problem: set in order the elements are lexicographically, that unordered_set the hash function int / char is how to achieve? Left to look after the "STL source code analysis"

Guess you like

Origin www.cnblogs.com/llee-123/p/11265812.html