-1
我只是在用C++学习编程。在我的作业中,我需要通过提示比较用户给出的三个字符串。我知道,对于直接修改某些值而不是返回值的函数,我需要使用指针。我也知道字符串已经像指针一样。这是到目前为止我的代码,但我不知道我错了:如何通过函数传递一个字符串C++
#include"std_lib_facilities.h"
void switching (string x, string y){
string flag;
if (x>y){flag=y;
y=x;
x=flag;
}
}
int main(){
string s1, s2, s3;
cout<<"introduce three words: ";
cin>>s1>>s2>>s3;
switching(s1,s2);
cout<<s1<<" "<<s2<<" "<<s3;
cout<<endl;
switching(s2,s3);
cout<<s1<<" "<<s2<<" "<<s3;
cout<<endl;
switching(s1,s2);
cout<<s1<<" "<<s2<<" "<<s3;
cout<<endl;
return 0;
}
你需要传递的引用,即'无效开关(字符串&X,串&Y){' – songyuanyao
“字符串已经表现得像指针” - 什么? – aschepler
'std :: string' *包含*指针,但本身不是指针。 'string x'是一个实例,而不是一个指向实例的指针。 'string * x'是一个指向实例的指针。 –