2014-04-06 40 views
1

我想从用户那里得到一个输入,然后对该输入进行冒泡排序然后输出结果。我的代码:如何对字符串进行冒泡排序?

#include<iostream> 
    using namespace std; 
    class bubble 
    { 
    public : 

     string arr[20]; 

     //Number of elements in array 
     int n; 

     //Function to accept array elements 
     void read() 
     { 
      while(1) 
      { 
       cout<<"\nEnter the number of elements in the array:"; 
       cin>>n; 
       if(n<=20) 

        break; 
       else 
        cout<<"\n Array can have maximum 20 elements \n"; 
      } 
      //display the header 
      cout<<"\n"; 
      cout<<"----------------------\n"; 
      cout<<"Enter array elements \n"; 
      cout<<"----------------------\n"; 

      //Get array elements 
      for(int i=0; i<n ;i++) 
      { 
       cout<<"<"<<i+1<<"> "; 
       cin>>arr[i]; 
      } 
     } 
     //Bubble sort function 
     void bubblesort() 
     { 
      for(int i=1;i<n ;i++)//for n-1 passes 
      { 
       //In pass i,compare the first n-i elements 
       //with their next elements 
       for(int j=0; j<n-1; j++) 
       { 
        if(arr[j] > arr[j+1]) 
        { 
         string temp; 
         temp = arr[j]; 
         arr[j] = arr[j+1]; 
         arr[j+1] = temp; 

        } 

       } 
      } 
     } 
     void display() 
     { 
      cout<<endl; 
      cout<<"----------------------\n"; 
      cout<<"Sorted array elements \n"; 
      cout<<"----------------------\n"; 
      for(int j=0; j<n; j++) 
       cout<<arr[j]<<endl; 
     } 
}; 
int main() 
{ 
    //Instantiate an instance of class 
    bubble list; 
    // Function call to accept array elements 
    list.read(); 
    // Function call to sort array 
    list.bubblesort(); 
    //Function call to display the sorted array 
    list.display(); 
    return 0; 
} 

的代码罚款运行,但不接受字符串作为输入空格或缩进值。有没有办法让它接受这些值?

+1

错误是相当自我解释。仔细看看第59行和'temp'变量声明。 –

+0

明白了,感谢您的时间 – user3467152

+2

@ user3467152:您修改了原始问题,以便现在的问题完全不同。请不要这样做。稍后阅读问题和答案的人会感到困惑。您应该为新问题发布新问题。 –

回答

1

您必须将temp的类型更改为std::string。由于显而易见的原因,int只能使用整数。

如果遇到此类编译器错误,请首先尝试理解错误消息。为什么程序会尝试将字符串转换为整数?

然后,看看提到的行(文件名后面的第一个数字)。请记住,如果您使用预处理器宏,则此数字可能会关闭。

如果你去那行,你会发现,它的下面:

temp = arr[j]; 

回过头来看看这个错误信息,这是很明显的是,在这条线上你想分配一个字符串值整数:

integer = string; 

既然你想要的字符串,你必须看到temp定义。往上走,你会碰到以下几行:

int temp; 

宾果!既然你知道你需要一个字符串(和变量,不使用其他任何地方),现在你只需换类型std::string,就大功告成了:

string temp; 
+0

感谢您的解释,让它得到修复,但是您是否知道如何使它接受空白作为输入? – user3467152

+0

你想跳过空格吗?因为默认情况下它应该比较空格。 – Mario

1
  1. 不要使用原始数组。使用std::vector<std::string>。那么奇怪的魔法数字限制20也会消失。

  2. 我认为使用一个类只是使事情复杂化在这里。使用独立功能并将std::vector<std::string> &作为参数。

  3. 请注意,C++已经提供了排序功能。请参阅std::sort和相关的分类功能,看看它们是否符合您的需求。

  4. 在C++中,您不会初始化变量并在稍后设置它们的起始值(您的int temp就是这种情况)。这是一回合:int temp = /* starting value */;。但是,请注意,该语句无论如何都是错误的,因为您试图将字符串设置为int。

这里有一个提示,你会如何改变你的排序功能的基础上,你已经拥有:

#include <iostream> 
#include <string> 
#include <vector> 

void bubblesort(std::vector<std::string> &strings) 
{ 
    typedef std::vector<std::string>::size_type size_type; 
    for (size_type i = 1; i < strings.size(); ++i) // for n-1 passes 
    { 
     // In pass i,compare the first n-i elements 
     // with their next elements 
     for (size_type j = 0; j < (strings.size() - 1); ++j) 
     { 
      if(strings[j] > strings[j+1]) 
      { 
       std::string const temp = strings[j]; 
       strings[j] = strings[j + 1]; 
       strings[j+1] = temp; 
      } 
     } 
    } 
} 

int main() 
{ 
    std::vector<std::string> strings; 
    strings.push_back("foo"); 
    strings.push_back("bar"); 
    strings.push_back("foobar"); 

    bubblesort(strings); 

    for (std::vector<std::string>::const_iterator iter = strings.begin(); iter != strings.end(); ++iter) 
    { 
     std::cout << *iter << "\n"; 
    } 
}