-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'是一個指向實例的指針。 –