C++ 按大小和字母表对字符串进行排序
C++ Sorting strings by size and alphabet
所以我必须主要按大小对字符串数组进行排序,这没有任何问题。
然后我尝试以类似的方式按字母顺序对相同大小的那些进行排序,简单地说,它完全是一团糟。
代码部分:
#include <iostream>
#include <string>
using namespace std;
struct strs{
string str;
int sz; //stores size of the string
};
bool compare(string a, string b, int s){ //comparing by characters
for(int i = 0; i < s; i++){
if(a[i] > b[i]) return true;
}
return false;
}
int main(){
int n, chk;
bool ctr;
cin>>n;
strs tab[n];
for(int i = 0; i < n; i++){
cin>>tab[i].str;
tab[i].sz = tab[i].str.size();
}
//Comparing lengths
for(int i = 0; i < n; i++){
chk = i;
for(int j = i + 1; j < n; j++){
if(tab[chk].sz > tab[j].sz){
chk = j;
}
}
if(chk != i){
swap(tab[i].str, tab[chk].str);
swap(tab[i].sz, tab[chk].sz);
}
}
//Comparing characters
for(int i = 0; i < n; i++){
chk = i;
for(int j = i + 1; j < n; j++){
if(tab[chk].sz == tab[j].sz){
ctr = compare(tab[chk].str, tab[j].str, tab[chk].sz);
if(ctr) chk = j;
}
if(tab[i].sz < tab[j].sz) break;
}
if(chk != i){
swap(tab[i].str, tab[chk].str);
swap(tab[i].sz, tab[chk].sz);
}
}
//output
for(int i = 0; i < n; i++){
cout<<tab[i].str<<endl;
}
return 0;
}
并显示我所说的 "mess"(从控制台复制)输入的意思:
- 椅子
- 鼠标
- 角度
- 沉没
- 天使
输出如下所示:
- 天使
- 椅子
- 鼠标
- 角度
- 沉没
所以它离排序还很远,我不知道我可以尝试用什么不同的方式让它工作。
你的比较函数应该是这样的:
bool compare(const std::string& a, const std::string& b, int s){ //comparing by characters
for(int i = 0; i < s; i++){
if(a[i] != b[i]) return a[i] > b[i];
}
return false;
}
但更简单的是使用 std::sort
:
auto proj = [](const std::string& s){ return std::make_tuple(s.size(), std::ref(s)); };
auto compare = [](const std::string& lhs, const std::string& rhs)
{
return proj(lhs) < proj(rhs);
};
std::sort(strings.begin(), strings.end(), compare);
所以我必须主要按大小对字符串数组进行排序,这没有任何问题。 然后我尝试以类似的方式按字母顺序对相同大小的那些进行排序,简单地说,它完全是一团糟。
代码部分:
#include <iostream>
#include <string>
using namespace std;
struct strs{
string str;
int sz; //stores size of the string
};
bool compare(string a, string b, int s){ //comparing by characters
for(int i = 0; i < s; i++){
if(a[i] > b[i]) return true;
}
return false;
}
int main(){
int n, chk;
bool ctr;
cin>>n;
strs tab[n];
for(int i = 0; i < n; i++){
cin>>tab[i].str;
tab[i].sz = tab[i].str.size();
}
//Comparing lengths
for(int i = 0; i < n; i++){
chk = i;
for(int j = i + 1; j < n; j++){
if(tab[chk].sz > tab[j].sz){
chk = j;
}
}
if(chk != i){
swap(tab[i].str, tab[chk].str);
swap(tab[i].sz, tab[chk].sz);
}
}
//Comparing characters
for(int i = 0; i < n; i++){
chk = i;
for(int j = i + 1; j < n; j++){
if(tab[chk].sz == tab[j].sz){
ctr = compare(tab[chk].str, tab[j].str, tab[chk].sz);
if(ctr) chk = j;
}
if(tab[i].sz < tab[j].sz) break;
}
if(chk != i){
swap(tab[i].str, tab[chk].str);
swap(tab[i].sz, tab[chk].sz);
}
}
//output
for(int i = 0; i < n; i++){
cout<<tab[i].str<<endl;
}
return 0;
}
并显示我所说的 "mess"(从控制台复制)输入的意思:
- 椅子
- 鼠标
- 角度
- 沉没
- 天使
输出如下所示:
- 天使
- 椅子
- 鼠标
- 角度
- 沉没
所以它离排序还很远,我不知道我可以尝试用什么不同的方式让它工作。
你的比较函数应该是这样的:
bool compare(const std::string& a, const std::string& b, int s){ //comparing by characters
for(int i = 0; i < s; i++){
if(a[i] != b[i]) return a[i] > b[i];
}
return false;
}
但更简单的是使用 std::sort
:
auto proj = [](const std::string& s){ return std::make_tuple(s.size(), std::ref(s)); };
auto compare = [](const std::string& lhs, const std::string& rhs)
{
return proj(lhs) < proj(rhs);
};
std::sort(strings.begin(), strings.end(), compare);