La funzione std::scambia() è una funzione incorporata nella libreria di modelli standard (STL) C++ che scambia il valore di due variabili.
Sintassi:
swap(a, b)>
parametri:
mouse e tipi di mouse
La funzione accetta due parametri obbligatori aeb che devono essere scambiati. I parametri possono essere di qualsiasi tipo di dati.
Valore di ritorno:
La funzione non restituisce nulla, scambia i valori delle due variabili. I programmi seguenti illustrano la funzione swap():
Complessità temporale: O(1)
Complessità spaziale: O(1)
Programma 1:
costante Java
CPP
// 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;> }> |
>
>Produzione
miglior sorriso del mondo
Value of a before: 10 Value of b before: 20 Value of a now: 20 Value of b now: 10>
Programma 2:
CPP
leggere il file CSV in Java
#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;> }> |
>
>Produzione
Value of a before: Geeks Value of b before: function Value of a now: function Value of b now: Geeks>