【问题标题】:How to alphabetically sort strings?如何按字母顺序对字符串进行排序?
【发布时间】:2013-09-04 09:00:03
【问题描述】:

我一直在尝试使用这个 c++ 程序按字母顺序对 5 个名称进行排序:

#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;

int main()
{
char names[5][100];
int x,y,z;

char exchange[100];

cout << "Enter five names...\n";

for(x=1;x<=5;x++)
{
    cout << x << ". ";
    cin >> names[x-1];
}
getch();

for(x=0;x<=5-2;x++)
{
    for(y=0;y<=5-2;y++)
    {
        for(z=0;z<=99;z++)
        {
            if(int(names[y][z])>int(names[y+1][z]))
            {   
                strcpy(exchange,names[y]);
                strcpy(names[y],names[y+1]);
                strcpy(names[y+1],exchange);
                break;
            }
        }   
    }
}   

for(x=0;x<=5-1;x++)
    cout << names[x];

return 0;
}

如果我分别输入 Earl、Don、Chris、Bill 和 Andy,我会得到:

AndyEarlDonChrisBill

谁能告诉我我的程序有什么问题?

【问题讨论】:

  • 您是否考虑过使用std::string 及其std::string::compare 功能?
  • @GregHewgill 更多类似查找std::string
  • 坦率地说,你的程序与任何合理的字符串排序算法都没有明显的关系。
  • 我很想知道你为什么要在 for 循环条件中从 5 中减去 2...
  • 您的代码很有趣(除了过于复杂之外):它包含不标准的conio.h,并且似乎没有在您的代码中使用;它使用cstring 库,这是一个相当低级的东西。如果您正在尝试学习 C++(而不是 C),那么学习 C++ 字符串库功能(&lt;string&gt; 和相关的 std::string 类,正如其他人所提到的)会更容易。除非您有充分的理由使用数组,否则最好使用标准容器(例如 &lt;vector&gt; 中的 std::vector)。所有这些代码是您自己编写的,还是您尝试改编在互联网上找到的东西?

标签: c++ string sorting alphabetical


【解决方案1】:

您的代码实现了bubble sort 的单次传递。基本上错过了“重复直到对数组没有更改”循环。

【讨论】:

  • 还有其他可能有用的排序类型吗?
  • 取决于您的目标。如果目标是对这些值进行排序,那么冒泡排序就足够了,直到其他因素使其不值得为止。对于像五个字符串这样的一小组值,排序所花费的时间只是整个程序运行时间的一小部分,所以冒泡排序就可以了。
【解决方案2】:

当名称已经按顺序排列时,代码不会处理。添加以下内容

else if(int(names[y][z])<int(names[y+1][z]))
            break;  

到 if 语句。

【讨论】:

    【解决方案3】:

    您可以使用字符串的 std::set 或 std::multiset(如果您允许重复项目),它会自动对项目进行排序(如果您愿意,您甚至可以更改排序标准)。

    #include <iostream>
    #include <set>
    #include <algorithm>
    
    void print(const std::string& item)
    {
        std::cout << item << std::endl;
    }
    
    int main()
    {
        std::set<std::string> sortedItems;
    
        for(int i = 1; i <= 5; ++i)
        {
            std::string name;
            std::cout << i << ". ";
            std::cin >> name;
    
            sortedItems.insert(name);
        }
    
        std::for_each(sortedItems.begin(), sortedItems.end(), &print);
        return 0;
    }
    

    输入:

    1. 杰拉尔多
    2. 卡洛斯
    3. 卡米洛
    4. 天使
    5. 博斯科

    输出:

    Angel
    Bosco
    Carlos
    Gerardo
    Kamilo
    

    【讨论】:

      【解决方案4】:

      您使用了太多不必要的循环。试试这个简单而有效的方法。当一个字符串按字母顺序比其他字符串晚时​​,您只需交换即可。

      Input
      5
      Ashadullah
      Shawon
      Shakib
      Aaaakash
      Ideone
      
      Output
      Aaaakash
      Ashadullah
      Ideone
      Shakib
      Shawon
      
      
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          string s[200],x[200],ct,dt;
          int i,j,n;
          cin>>n;
          for(i=0;i<n;i++)
          {
              cin>>s[i];
          }
      
          for(i=0;i<n;i++)
          {
              for(j=i+1;j<n;j++)
              {
      
                  if(s[i]>s[j])
                  {
      
                      ct=s[i];
                      s[i]=s[j];
                      s[j]=ct;
      
                  }
      
              }
      
          }
          cout<<"Sorted Name in Dictionary Order"<<endl;
          for(i=0;i<n;i++)
          {
              cout<<s[i]<<endl;
          }
          return 0;
      
      
      }
      

      【讨论】:

        【解决方案5】:

        可以使用排序功能:

        vector<string> s;
        sort(s.begin(),s.end());
        

        【讨论】:

          【解决方案6】:

          把它放在这里以防有人需要不同的解决方案。

          /* sorting example */
          #include <iostream>
          using namespace std;
          
          bool isSwap( string str1, string str2, int i)
          {
              if(str1[i] > str2[i])
                  return true;
              if(str1[i] == str2[i])
                  return isSwap(str1,str2,i+1); 
              return false;
          }
          
          int main()
          {   
              string str[7] = {"you","your","must","mike", "jack", "jesus","god"};
              int strlen = 7;
              string temp;
              int i = 0;
              int j = 0;
              bool changed = false;
              while(i < strlen-1)
              {
                  changed = false;
                  j = i+1;
                  while(j < strlen)
                  {
                      if(isSwap(str[i],str[j],0))
                      {
                          temp = str[i];
                          str[i] = str[j];
                          str[j] = temp;
                          changed = true;
                      }
                      j++;
                  }
                  if(changed)
                      i = 0;
                  else
                      i++;       
              }
              for(i = 0; i < strlen; i++)
                  cout << str[i] << endl;
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-23
            • 2017-10-04
            • 1970-01-01
            • 2013-10-28
            相关资源
            最近更新 更多