logo

Pereche în C++ Standard Template Library (STL)

Perechea este folosită pentru a combina două valori care pot fi de tipuri diferite de date. Perechea oferă o modalitate de a stoca două obiecte eterogene ca o singură unitate. Practic este folosit dacă dorim să stocăm tupluri. Containerul pereche este un container simplu definit în antet format din două elemente de date sau obiecte.

  • Primul element este referit ca „primul”, iar al doilea element ca „al doilea”, iar ordinea este fixă ​​(primul, al doilea).
  • Perechea poate fi atribuită, copiată și comparată. Matricea de obiecte alocate în a Hartă sau hash_map este de tip „pereche” în mod implicit, în care toate „primele” elemente sunt chei unice asociate cu obiectele lor de „a doua” valoare.
  • Pentru a accesa elementele, folosim numele variabilei urmat de operatorul punct urmat de cuvântul cheie primul sau al doilea.

Sintaxă:



pair Pair_name>

CPP






// CPP program to illustrate Pair in STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >// defining a pair> >pair<>int>,>char>>PAIR1;> >// first part of the pair> >PAIR1.first = 100;> >// second part of the pair> >PAIR1.second =>'G'>;> >cout << PAIR1.first <<>' '>;> >cout << PAIR1.second << endl;> >return> 0;> }>



>

>

Ieșire

100 G>

Inițializarea unei perechi: De asemenea, putem inițializa o pereche.

Sintaxă:

pair Pair_name (value1, value2) ;>

Diferite moduri de a inițializa perechea:

pair g1; //default pair g2(1, 'a'); //initialized, different data type pair g3(1, 10); //initialized, same data type pair g4(g3); //copy of g3>

O altă modalitate de a inițializa o pereche este prin utilizarea funcției make_pair().

g2 = make_pair(1, 'a');>

O altă sintaxă validă pentru a declara perechea este:

g2 = {1, 'a'};>

CPP

ce este maven




// CPP program to illustrate> // Initializing of pair STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >// defining a pair> >pairdouble>PAIR2('GeeksForGeeks', 1.23); cout<< PAIR2.first << ' '; cout << PAIR2.second << endl; return 0; }>

>

opacitatea tranziției css
>

Ieșire

GeeksForGeeks 1.23>

Notă: Dacă nu este inițializată, prima valoare a perechii este inițializată automat.

C++




// CPP program to illustrate> // auto-initializing of pair STL> #include> #include> using> namespace> std;> int> main()> {> >pair<>int>,>double>>PAIR1;> >pairchar>PAIR2; // este initializat la 0 cout<< PAIR1.first; // it is initialised to 0 cout << PAIR1.second; cout << ' '; // // it prints nothing i.e NULL cout << PAIR2.first; // it prints nothing i.e NULL cout << PAIR2.second; return 0; }>

>

>

Ieșire:

00>

Funcții ale membrilor

1) make_pair() : Această funcție de șablon permite crearea unei perechi de valori fără a scrie tipurile în mod explicit.
Sintaxă:

Pair_name = make_pair (value1,value2);>

CPP




// CPP Program to demonstrate make_pair()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>int>,>char>>PAIR1;> >pairdouble>PAIR2('GeeksForGeeks', 1.23); pairdouble> PAIR3; PERECHEA1.prima = 100; PAIR1.second = 'G'; PAIR3 = make_pair('GeeksForGeeks este cel mai bun', 4.56); cout<< PAIR1.first << ' '; cout << PAIR1.second << endl; cout << PAIR2.first << ' '; cout << PAIR2.second << endl; cout << PAIR3.first << ' '; cout << PAIR3.second << endl; return 0; }>

>

>

Ieșire

100 G GeeksForGeeks 1.23 GeeksForGeeks is Best 4.56>

2) schimb: Această funcție schimbă conținutul unui obiect pereche cu conținutul altui obiect pereche. Perechile trebuie să fie de același tip.
Sintaxă:

aes vs des
pair1.swap(pair2) ;>

Pentru două perechi date, să spunem perechea1 și perechea2 de același tip, funcția de schimb va schimba perechea1.primul cu perechea2.primul și perechea1.secunda cu perechea2.secunda.

CPP




// CPP Program to demonstrate swap()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>char>,>int>>pair1 = make_pair(>'A'>, 1);> >pair<>char>,>int>>pair2 = make_pair(>'B'>, 2);> >cout <<>'Before swapping: '>;> >cout <<>'Contents of pair1 = '> << pair1.first <<>' '> ><< pair1.second;> >cout <<>'Contents of pair2 = '> << pair2.first <<>' '> ><< pair2.second;> >pair1.swap(pair2);> >cout <<>' After swapping: '>;> >cout <<>'Contents of pair1 = '> << pair1.first <<>' '> ><< pair1.second;> >cout <<>'Contents of pair2 = '> << pair2.first <<>' '> ><< pair2.second;> >return> 0;> }>

>

>

Ieșire

Before swapping: Contents of pair1 = A 1Contents of pair2 = B 2 After swapping: Contents of pair1 = B 2Contents of pair2 = A 1>

3) cravată(): Această funcție funcționează la fel ca în tupluri . Acesta creează un tuplu de referințe lvalue la argumentele sale, adică pentru a despacheta valorile tuplului (sau aici perechea) în variabile separate. La fel ca la tupluri, aici sunt și două variante de cravată, cu și fără ignorare. ignore cuvântul cheie ignoră un anumit element tuplu de la despachetare.
Cu toate acestea, tuplurile pot avea mai multe argumente, dar perechile au doar două argumente. Deci, în cazul unei perechi de perechi, dezambalarea trebuie gestionată în mod explicit.
Sintaxă:

tie(int &, int &) = pair1;>

CPP




// CPP code to illustrate tie() in Pair> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>int>,>int>>pereche1 = { 1, 2 };> >int> a, b;> >tie(a, b) = pair1;> >cout << a <<>' '> << b <<>' '>;> >pair<>int>,>int>>perechea2 = { 3, 4 };> >tie(a, ignore) = pair2;> >// prints old value of b> >cout << a <<>' '> << b <<>' '>;> >// Illustrating pair of pairs> >pair<>int>, pair<>int>,>char>>> pereche3 = { 3, { 4,>>> } };> >int> x, y;> >char> z;> >// tie(x,y,z) = pair3; Gives compilation error> >// tie(x, tie(y,z)) = pair3; Gives compilation error> >// Each pair needs to be explicitly handled> >tie(x,ignore) = pair3;> >tie(y, z) = pair3.second;> >cout << x <<>' '> << y <<>' '> << z <<>' '>;> }> // contributed by sarthak_eddy.>

>

>

Ieșire

1 2 3 2 3 4 a>

Cod pentru a ilustra funcțiile în pereche:

CPP




// CPP program to illustrate pair in STL> #include> #include> #include> using> namespace> std;> int> main()> {> >pairint>g1; pairint> g2('Quiz', 3); pairint> g3(g2); pereche g4(5, 10); g1 = make_pair(string('Geeks'), 1); g2.first = '.com'; g2.secunda = 2; cout<< 'This is pair g' << g1.second << ' with ' << 'value ' << g1.first << '.' << endl << endl; cout << 'This is pair g' << g3.second << ' with value ' << g3.first << 'This pair was initialized as a copy of ' << 'pair g2' << endl << endl; cout << 'This is pair g' << g2.second << ' with value ' << g2.first << ' The values of this pair were' << ' changed after initialization.' << endl << endl; cout << 'This is pair g4 with values ' << g4.first << ' and ' << g4.second << ' made for showing addition. The ' << 'sum of the values in this pair is ' << g4.first + g4.second << '.' << endl << endl; cout << 'We can concatenate the values of' << ' the pairs g1, g2 and g3 : ' << g1.first + g3.first + g2.first << endl << endl; cout << 'We can also swap pairs ' << '(but type of pairs should be same) : ' << endl; cout << 'Before swapping, ' << 'g1 has ' << g1.first << ' and g2 has ' << g2.first << endl; swap(g1, g2); cout << 'After swapping, ' << 'g1 has ' << g1.first << ' and g2 has ' << g2.first; return 0; }>

>

>

Ieșire

This is pair g1 with value Geeks. This is pair g3 with value QuizThis pair was initialized as a copy of pair g2 This is pair g2 with value .com The values of this pair were changed after initialization. This is pair g4 with values 5 and 10 made for showing addition. The sum of the values in this pair is 15. We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com We can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .com After swapping, g1 has .com and g2 has Geeks>

Complexitatea timpului: O(1).

sql ordine după dată

Spatiu auxiliar: O(1).

operatori (=, ==, !=,>=, <=) în Pereche

Putem folosi și operatori cu perechi.

1) folosind egal(=): Acesta atribuie un obiect nou pentru un obiect pereche. Sintaxă:

pair& operator= (const pair& pr);>

Aceasta Asignează pr ca noul conținut pentru obiectul pereche. Primei valori i se atribuie prima valoare a lui pr, iar celei de-a doua valori i se atribuie a doua valoare a lui pr.

2) Operator de comparare (==) cu pereche: Pentru cele două perechi date, să spunem perechea1 și perechea2, operatorul de comparație compară prima valoare și a doua valoare a acelor două perechi, adică dacă perechea1.primul este egal cu perechea2.primul sau nu și dacă perechea1.secunda este egal cu perechea2.secunda sau nu. .

adică dacă ( (pari1.first ==pair2.first) && (pair1.second==pair2.second) )

Dacă oricare dintre cele două condiții este falsă, atunci returnează false, altfel adevărat.

3) Operator neegal (!=) cu pereche: Pentru cele două perechi date, să spunem perechea1 și perechea2, operatorul != compară primele valori ale acelor două perechi, adică dacă perechea1.primul este egal cu perechea2.primul sau nu, dacă sunt egale, atunci verifică a doua valoare a ambelor.

4) Operatori logici(>=, <= ) cu pereche: Pentru cele două perechi date, să spunem perechea1 și perechea2, =,>, pot fi folosite și cu perechi. Returnează 0 sau 1 doar comparând prima valoare a perechii. Pentru perechi precum p1=(1,20) și p2=(1,10) p2sau

&list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB