Funkcija std::swap() yra C++ standartinės šablonų bibliotekos (STL) įtaisyta funkcija, kuri keičia dviejų kintamųjų reikšmes.
Sintaksė:
swap(a, b)>
Parametrai:
trečia normali forma
Funkcija priima du privalomus parametrus a ir b, kurie turi būti sukeisti. Parametrai gali būti bet kokio tipo duomenų.
Grąžinimo vertė:
Funkcija nieko negrąžina, ji sukeičia dviejų kintamųjų reikšmes. Toliau pateiktos programos iliustruoja swap() funkciją:
Laiko sudėtingumas: O(1)
Erdvės sudėtingumas: O(1)
1 programa:
CPP
styginių metodai
// C++ program for illustration of swap() function> #include> using> namespace> std;> int> main()> {> >int> a = 10;> >int> b = 20;> >cout <<>'Value of a before: '> << a << endl;> >cout <<>'Value of b before: '> << b << endl;> >// swap values of the variables> >swap(a, b);> >cout <<>'Value of a now: '> << a << endl;> >cout <<>'Value of b now: '> << b << endl;> >return> 0;> }> |
>
>Išvestis
Value of a before: 10 Value of b before: 20 Value of a now: 20 Value of b now: 10>
2 programa:
CPP
veikimo testavimas
#include> using> namespace> std;> int> main()> {> >string a =>'Geeks'>;> >string b =>'function'>;> >cout <<>'Value of a before: '> << a << endl;> >cout <<>'Value of b before: '> << b << endl;> >swap(a, b);> >cout <<>'Value of a now: '> << a << endl;> >cout <<>'Value of b now: '> << b << endl;> >return> 0;> }> |
>
kaip konvertuoti eilutę į sveikąjį java
>Išvestis
Value of a before: Geeks Value of b before: function Value of a now: function Value of b now: Geeks>