#include<iostream>
#include<string>
template <typename T>
void swap(T a , T b)
{
T temp = a;
a = b;
b = temp;
}
template <typename T1>
void swap1(T1 a , T1 b)
{
T1 temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10 , b = 20;
std::string first = "hi" , last = "Bye";
swap(a,b);
swap(first, last);
std::cout<<"a = "<<a<<" b = "<<b<<std::endl;
std::cout<<"first = "<<first<<" last = "<<last<<std::endl;
int c = 50 , d = 100;
std::string name = "abc" , surname = "def";
swap1(c,d);
swap1(name,surname);
std::cout<<"c = "<<c<<" d = "<<d<<std::endl;
std::cout<<"name = "<<name<<" surname = "<<surname<<std::endl;
swap(c,d);
swap(name,surname);
std::cout<<"c = "<<c<<" d = "<<d<<std::endl;
std::cout<<"name = "<<name<<" surname = "<<surname<<std::endl;
return 0;
}
**Output**
a = 10 b = 20
first = Bye last = hi
c = 50 d = 100
name = abc surname = def
c = 50 d = 100
name = def surname = abc
Keduanya swap()
dan swap1()
pada dasarnya memiliki definisi fungsi yang sama lalu mengapa hanya swap()
benar-benar menukar string, sementara swap1()
tidak?
Dapatkah Anda memberi tahu saya bahwa bagaimana string stl diberikan sebagai argumen secara default yaitu apakah mereka dilewatkan oleh nilai atau dengan referensi?