尝试使用模板函数交换两个字符串
Trying to use templatised fuctions to swap two strings
#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
swap()
和 swap1()
基本上都有相同的函数定义,那么为什么只有 swap()
实际交换了字符串,而 swap1()
却没有?
您还能告诉我 stl 字符串在默认情况下是如何作为参数传递的,即它们是按值传递还是按引用传递?
我明白为什么人们现在对 ADL 皱眉了...
你看到的是Argument Dependent Lookup的效果。如果您在 swap
实现中添加打印,您会注意到 而不是 std::string
,仅 int
。
std::swap
优于您的版本,因为 std::basic_string
类型存在 explicit specialization。如果它不存在,调用可能会模棱两可。
对于 int
,在查找过程中不考虑命名空间 std
,因此您的版本是唯一可接受的。
Also can you tell me that how are stl strings passed as arguements by default i.e are they passed by value or by reference?
C++ 中的所有内容都是按值传递的,除非您明确将其标记为按引用传递。
您正在按值传递参数。您需要通过引用传递它们:
template <typename T> void myswap(T& a , T& b);
或者 - 更一般地 - 通过全局(右值)参考:
template <typename T> void myswap(T&& a , T&& b);
#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
swap()
和 swap1()
基本上都有相同的函数定义,那么为什么只有 swap()
实际交换了字符串,而 swap1()
却没有?
您还能告诉我 stl 字符串在默认情况下是如何作为参数传递的,即它们是按值传递还是按引用传递?
我明白为什么人们现在对 ADL 皱眉了...
你看到的是Argument Dependent Lookup的效果。如果您在 swap
实现中添加打印,您会注意到 而不是 std::string
,仅 int
。
std::swap
优于您的版本,因为 std::basic_string
类型存在 explicit specialization。如果它不存在,调用可能会模棱两可。
对于 int
,在查找过程中不考虑命名空间 std
,因此您的版本是唯一可接受的。
Also can you tell me that how are stl strings passed as arguements by default i.e are they passed by value or by reference?
C++ 中的所有内容都是按值传递的,除非您明确将其标记为按引用传递。
您正在按值传递参数。您需要通过引用传递它们:
template <typename T> void myswap(T& a , T& b);
或者 - 更一般地 - 通过全局(右值)参考:
template <typename T> void myswap(T&& a , T&& b);