logo

HASHSET EN C++

An col·lecció no ordenada que consta d'elements únics s'anomena a hashset en c++ . La col·lecció d'operacions estàndard, com remove, conté està continguda en c++. La intersecció, la diferència simètrica i la unió són l'operació estàndard basada en conjunts constituïda per c++. Per a la identificació i cerca d'elements, la funció hash a hashset és molt útil en c++. El hashset té un paper important en la identificació de duplicats a la llista consistent. Amb aquesta funció hash, podem obtenir els valors diferents i fins i tot els valors duplicats també. El llista_desordenada (hashset) triga un temps, és a dir o (1) que és constant per naturalesa. En l'altre cas, el temps trigat pot ser o que és el temps lineal. En aquest context, aprendríem tot sobre el hashset en c++.

memòria de registre

Sintaxi:

La sintaxi per a la inserció de hashset o conjunt no ordenat en c++, que és un tipus de cadena, és la següent:

 int main() { unordered_set CBA ; CBA.insert('') ; CBA.insert('') ; .................. } 

Alguns exemples de hashset C++ amb el seu mecanisme de treball:

An conjunt_desordenat o HashSet és un conjunt en què la clau s'emmagatzema en qualsevol ordre. Per a un HashSet, s'utilitzen moltes funcions. Però les funcions més utilitzades s'indiquen a continuació:

  1. La funció de mida s'utilitza per a la capacitat.
  2. La funció buida també s'utilitza per a la capacitat.
  3. find s'utilitza per cercar una clau.
  4. La funció d'esborrar s'utilitza per modificar-hi.
  5. La funció d'inserció també s'utilitza per a la modificació.

An conjunt_desordenat només permet les claus úniques, i an multiconjunt_desordenat només permet passar-hi les claus duplicades.

Exemples:

Amb els diferents tipus d'exemples, tot el mecanisme de treball de C++ HashSet s'ha explicat de la següent manera:

1) Exemple de hashset de c++ utilitzant {…...} Aquesta és una llista inicialitzada:

Utilitzant HashSet en C++, es dóna l'exemple bàsic en què hem inicialitzat el conjunt amb l'ajuda de la llista d'inicialitzadors {…..}.

Codi:

 #include #include int main() { std::unordered_set P { 2017, 2016, 2015 }; for (auto Q: P) std::cout << Q << '
'; return 0; } 

Sortida:

 2015 2016 2017 

2) Ús del predicat binari per passar l'objecte de comparació:

ex del nom d'usuari

Utilitzant un conjunt de predicats binaris, els objectes de comparació es passen a l'exemple següent. L'ordenació del conjunt es defineix utilitzant dos mateixos tipus d'elements.

Codi:

 #include #include struct JAVATPOINT { template bool operator()(const X& n, const X& p) const { return n > p; } }; int main() { std::set values = { 120, 80, 250 }; for (auto S: values) std::cout << S << '
'; return 0; } 

Sortida:

 250 120 80 

3) Exemple de hashset en C++ utilitzant inserció, iteració, cerca i declaració:

En l'exemple que es mostra a continuació, es pren un temps constant de mitjana per a l'operació d'inserció, esborrat i cerca. La funció de cerca es dóna a l'exemple quan al conjunt, la clau no està present. Torna el Iterador fins al final() . I d'altra banda, l'Iterator torna fàcilment a la posició de la clau quan al conjunt, la clau està present. Per als valors de clau com a punter, Iterator s'utilitza per rebre la clau, i la clau es pot recuperar mitjançant operador de desreferenciació * .

Codi:

llista de matrius en java
 #include using namespace std; int main() { unordered_set CBA ; CBA.insert('Developer') ; CBA.insert('Programmer') ; CBA.insert('tester') ; CBA.insert('HR') ; CBA.insert('Coder') ; string key = 'JAVATPOINT' ; if (CBA.find(key) == CBA.end()) cout << key << ' one of the best company.' << endl << endl ; else cout << 'retrieved' << key << endl << endl ; key = 'Programmer'; if (CBA.find(key) == CBA.end()) cout << key << 'can not retrieve
' ; else cout << 'retrieved ' << key << endl ; cout << '
here is the designations : &apos; &lt;<endl; unordered_set :: iterator itr; for (itr="CBA.begin();" itr !="CBA.end();" itr++) cout << (*itr) endl; } < pre> <p> <strong>Output:</strong> </p> <pre> JAVATPOINT one of the best company. retrieved Programmer here is the designations : HR tester Programmer Coder Developer When the key data is not found in the order list: JAVATPOINT one of the best company Program can not retrieve here is the designations : HR tester Programmer Coder Developer </pre> <p> <strong>4) Using an unordered set searching for duplicate content:</strong> </p> <p>In the given below example as the input, the set of integers is provided, and in the set, the duplicates have been found and displayed in the output.</p> <p> <strong>Code example:</strong> </p> <pre> #include using namespace std; void printDuplicates(int deepak[], int M) { unordered_set JAVATPOINT; unordered_set similar; for (int P = 0; P <m; p++) { if (javatpoint.find(deepak[p])="=" javatpoint.end()) javatpoint.insert(deepak[p]); else similar.insert(deepak[p]); } cout << 'similar contents are : '; unordered_set :: iterator start; for (start="similar.begin();" start !="similar.end();" start++) *start ' int main() deepak[]="{9," 3, 6, 1, 2, 4, 9, 5, 7, 0, 8}; m="sizeof(Deepak)" sizeof(int); printduplicates(deepak, m); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> similar contents are : 9 6 </pre> <h2>Conclusion:</h2> <p>In the above context, we have learned about HashSet in C++ and its working mechanism of it. In this article, we have also learned the various applications of C++ has set with the help of different examples in which they are working. In finding duplicate content and desired content C++ HashSet plays a vital role in it.</p> <hr></m;></pre></endl;>

4) Ús d'un conjunt no ordenat per cercar contingut duplicat:

A l'exemple següent com a entrada, es proporciona el conjunt d'enters, i al conjunt, els duplicats s'han trobat i es mostren a la sortida.

Exemple de codi:

 #include using namespace std; void printDuplicates(int deepak[], int M) { unordered_set JAVATPOINT; unordered_set similar; for (int P = 0; P <m; p++) { if (javatpoint.find(deepak[p])="=" javatpoint.end()) javatpoint.insert(deepak[p]); else similar.insert(deepak[p]); } cout << \'similar contents are : \'; unordered_set :: iterator start; for (start="similar.begin();" start !="similar.end();" start++) *start \' int main() deepak[]="{9," 3, 6, 1, 2, 4, 9, 5, 7, 0, 8}; m="sizeof(Deepak)" sizeof(int); printduplicates(deepak, m); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> similar contents are : 9 6 </pre> <h2>Conclusion:</h2> <p>In the above context, we have learned about HashSet in C++ and its working mechanism of it. In this article, we have also learned the various applications of C++ has set with the help of different examples in which they are working. In finding duplicate content and desired content C++ HashSet plays a vital role in it.</p> <hr></m;>

Conclusió:

En el context anterior, hem après sobre HashSet en C++ i el seu mecanisme de funcionament. En aquest article, també hem après les diferents aplicacions de C++ que s'ha establert amb l'ajuda de diferents exemples en què estan treballant. A l'hora de trobar contingut duplicat i contingut desitjat, C++ HashSet hi juga un paper vital.