c++中对三个字符串排序的问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 19:12:02
#include <iostream>
#include <string>
using namespace std;
int main()
{
void swap(string &,string &);
string str1,str2,str3;
cin>>str1>>str2>>str3;
if (str1>str2) swap(str1,str2);
if (str1>str3) swap(str1,str3);
if (str2>str3) swap(str2,str3);
cout<<"顺序为:"<<endl;
return 0;
}
void swap(string &str1,string &str2)
{
string a;
a=str1;
str1=str2;
str2=a;
}
对三个字符串排序。错误在哪里?
脑子打了忘了输出。。汗。。。但是为什么将void swap(string &,string &);写在main函数里面却不行呢? 在VC6.0上写在里面编译错误。。。 林声飘扬那个没有编译成功。。。

VC6.0中还真是有错误的,把函数声明放在外面没错误。其他函数声明放main里面却没有错误。猜测可能是string类的问题。

#include <iostream>
#include <string>
using namespace std;
int main()
{
void swap(string &,string &);
string str1,str2,str3;
cin>>str1>>str2>>str3;
if (str1>str2) swap(str1,str2);
if (str1>str3) swap(str1,str3);
if (str2>str3) swap(str2,str3);
cout<<"顺序为:"<<str1<<" "<<str2<<" "<<str3<<endl;
return 0;
}
void swap(string &str1,string &str2)
{
string a;
a=str1;
str1=str2;
str2=a;
}
将void swap(string &,string &)写在main函数里面是可以,是函数声明,只是在乎形参类型,形参名可以忽略不计。

void swap(string &,string &);
应该写在main函数前面

#include <iostream>
#include <string>
using namespace std;
void swap(string &,string &);
int main()
{

string str1,str2,str3;