How to swap two variables, without using third variable ?

Answer Posted / xeon

To note on the answer about Perl, it uses a third variable
and just doesn't tell you. The C++ equivalent is:

//Our data
class exampleclass {};
exampleclass classa, classb;
int inta, intb;
float floata, floatb;

//Our swap function
template<class T>
void swap(T *a, T *b) {
T temp = *a;
a = b;
*b = temp;
}

//Our use of the swap function
swap<classexample>(classa, classb);
swap<int>(inta, intb);
swap<float>(floata, floatb);

And also note, Perl is an interpreted (Script) language, so
though it is simple and robust, it is less efficient than a
language that is compiled.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

4049


How to palindrom string in c language?

9795


Cluster head selection in Wireless Sensor Network using C programming language.

3382


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

2131


write a program for area of circumference of shapes

2228






can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

2564


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

2889


how to programme using switch statements and fuctions, a programme that will output two even numbers, two odd numbers and two prime numbers of the users chioce.

2351


Write a program to model an exploding firecracker in the xy plane using a particle system

3866


What is full form of PEPSI

2118


write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

4388


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

4074


Develop a routine to reflect an object about an arbitrarily selected plane

3274


Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

3246


What is data _null_? ,Explain with code when u need to use it in data step programming ?

3018