天天看點

有5個字元串,要求對他們按由小到大 string

#include <iostream>

#include <string>

using namespace std;

int main()

{

 int i;

 string str[5];

 void sort(string s[]);

 cout<<"Please input string:"<<endl;

 for(i=0;i<=5;i++)

  cin>>str[i];                     

 sort(str);

 cout<<"The sorted string is:"<<endl;

 for(i=0;i<=5;i++)

  cout<<str[i]<<"  ";             

 cout<<endl;

 return 0;

}

void sort(string s[])  

{

 int i,j;

 string temp;

 for(i=0;i<4;i++)

  for(j=0;j<4-i;j++)

   if(s[j]>s[j+1])

   {

    temp=s[j];

    s[j]=s[j+1];

    s[j+1]=temp;

   }

}

繼續閱讀