Jump to content

Generic swap: Difference between revisions

Better D entry
(→‎{{header|Pascal}}: add example)
(Better D entry)
Line 253:
 
=={{header|D}}==
<lang d>import std.algorithm: swap; // from Phobos standard library
The solution for D is quite similar to that for C++:
 
// The D solution foruses Dtemplates isand quiteit's similar to that forthe C++ one:
<lang d>void swapmySwap(T)(ref T left, ref T right) {
auto temp = left;
left auto temp = rightleft;
right left = tempright;
right = temp;
}
 
void main() {
import std.stdio;
 
int[] a = [10, 20];
writeln(a);
 
// The std.algorithm standard library module contains a generic swap.
// contains a generic swap:
swap(a[0], a[1]);
writeln(a);
 
// Using mySwap:
mySwap(a[0], a[1]);
writeln(a);
}</lang>
{{out}}
The std.algorithm standard library module contains a generic swap.
<pre>[10, 20]
[20, 10]
[10, 20]</pre>
 
=={{header|dc}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.