C++ 如何按字母顺序对字符串进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18553097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to alphabetically sort strings?
提问by JayKay
I have been trying to use this c++ program to sort 5 names alphabetically:
我一直在尝试使用这个 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;
}
If I enter Earl, Don, Chris, Bill, and Andy respectively, I get this:
如果我分别输入 Earl、Don、Chris、Bill 和 Andy,我会得到:
AndyEarlDonChrisBill
Could someone please tell me whats wrong with my program?
有人可以告诉我我的程序有什么问题吗?
回答by AngelCastillo
You could use std::set or std::multiset (if you will allow repeated items) of strings, and it will keep the items sorted automatically (you could even change the sorting criteria if you want).
您可以使用字符串的 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;
}
input:
输入:
- Gerardo
- Carlos
- Kamilo
- Angel
- Bosco
- 杰拉尔多
- 卡洛斯
- 卡米洛
- 天使
- 博斯科
output:
输出:
Angel
Bosco
Carlos
Gerardo
Kamilo
回答by user7968404
You can use the sort function:
您可以使用排序功能:
vector<string> s;
sort(s.begin(),s.end());
回答by shawon
You are using too much unnecessary loops. Try this simple and efficient one. You need to just swap when a string is alphabetically latter than other string.
您使用了太多不必要的循环。试试这个简单而有效的方法。当一个字符串按字母顺序比其他字符串晚时,您只需要交换。
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;
}
回答by caskey
Your code implements a single-pass of bubble sort. Essentially missing the 'repeat until no changes are made to the array' loop around the outside.
您的代码实现了一次冒泡排序。基本上缺少“重复,直到对数组没有任何更改”循环在外面。
回答by Testing
The code does not take care when the names are already in order. Add the following
当名称已按顺序排列时,代码不会处理。添加以下内容
else if(int(names[y][z])<int(names[y+1][z]))
break;
To the if statement.
到 if 语句。