conjunto vs unordered_set en C++ STL

Requisito previo: establecido en C++ , unordered_set en C++ Diferencias: 

                |     set             | unordered_set
---------------------------------------------------------
Ordering        | increasing  order   | no ordering
                | (by default)        |

Implementation  | Self balancing BST  | Hash Table
                | like Red-Black Tree |  

search time     | log(n)              | O(1) -> Average 
                |                     | O(n) -> Worst Case

Insertion time  | log(n) + Rebalance  | Same as search
                      
Deletion time   | log(n) + Rebalance  | Same as search

Utilice establecer cuando

CPP

// Program to print elements of set
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    set<int> s;
    s.insert(5);
    s.insert(1);
    s.insert(6);
    s.insert(3);
    s.insert(7);
    s.insert(2);
 
    cout << "Elements of set in sorted order: \n";
    for (auto it : s)
        cout << it << " ";
 
    return 0;
}

CPP

// Program to print elements of set
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    unordered_set<int> s;
    s.insert(5);
    s.insert(1);
    s.insert(6);
    s.insert(3);
    s.insert(7);
    s.insert(2);
 
    cout << "Elements of unordered_set: \n";
    for (auto it : s)
        cout << it << " ";
 
    return 0;
}

CPP

// Program to print inorder predecessor and inorder successor
#include <bits/stdc++.h>
using namespace std;
 
set<int> s;
 
void inorderPredecessor(int key)
{
    if (s.find(key) == s.end()) {
        cout << "Key doesn't exist\n";
        return;
    }
 
    set<int>::iterator it;
    it = s.find(key); // get iterator of key
 
    // If iterator is at first position
    // Then, it doesn't have predecessor
    if (it == s.begin()) {
        cout << "No predecessor\n";
        return;
    }
 
    --it; // get previous element
    cout << "predecessor of " << key << " is=";
    cout << *(it) << "\n";
}
 
void inorderSuccessor(int key)
{
    if (s.find(key) == s.end()) {
        cout << "Key doesn't exist\n";
        return;
    }
 
    set<int>::iterator it;
    it = s.find(key); // get iterator of key
    ++it; // get next element
 
    // Iterator points to NULL (Element does
    // not exist)
    if (it == s.end())
    {
        cout << "No successor\n";
        return;
    }
    cout << "successor of " << key << " is=";
    cout << *(it) << "\n";
}
 
int main()
{
    s.insert(1);
    s.insert(5);
    s.insert(2);
    s.insert(9);
    s.insert(8);
 
    inorderPredecessor(5);
    inorderPredecessor(1);
    inorderPredecessor(8);
    inorderSuccessor(5);
    inorderSuccessor(2);
    inorderSuccessor(9);
 
    return 0;
}

Publicación traducida automáticamente

Artículo escrito por Abhishek rajput y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *