字符串排序与sort()函数

这篇博客介绍了C++中的sort函数,包括其在字符串排序中的应用和如何使用自定义比较函数进行排序。示例展示了如何对字符串数组进行字典序排列,并提供了两参数和三参数sort函数的使用方法。此外,还通过实例解释了如何通过自定义函数实现升序和降序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串排序与sort()函数

程序示例

描述
给定 n 个字符串,请对 n 个字符串按照字典序排列。
数据范围:1≤n≤1000  ,字符串长度满足1≤len≤100 

输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool comp(string a,string b){
    return a<b;//升序排列
}

int main(){
    int n;
    cin>>n;
    string str[n];
    
    for(int i=0;i<n;i++){
        cin>>str[i];
    }

    sort(str,str+n,comp);
    
    for(int j=0;j<n;j++){
        cout<<str[j]<<endl;
    }
}

关于sort()函数

  1. sort()函数位于头文件

    #include

  2. sort()函数的两种常见语法

   //对 [first, last) 区域内的元素做默认的升序排序
   void sort (RandomAccessIterator first, RandomAccessIterator last);
   //按照指定的 comp 排序规则,对 [first, last) 区域内的元素进行排序
    void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  • 两参数示例

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        int a[20] = { 21,65,21,51,85,1,24,10 };
        cout << "原数列:";
        for (int i = 0; i < 20; i++) cout << a[i] <<" ";
        cout << endl;
    
        sort(a, a + 20);//a指向数组的首地址;sort的两个参数分别指向排序内容的首尾地址;默认为升序排列
        cout << "排序后:";
        for (int i = 0; i < 20; i++) cout <<a[i] << " ";
    }
    

    输出

    原数列:21 65 21 51 85 1 24 10 0 0 0 0 0 0 0 0 0 0 0 0
    排序后:0 0 0 0 0 0 0 0 0 0 0 0 1 10 21 21 24 51 65 85
    
  • 三参数示例

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    //以普通函数的方式实现自定义排序规则
    bool myfunction(int i, int j) { return i < j; }//升序排列
    bool myfunction2(int i, int j) { return i > j; }//降序排列
    
    int main() {
        vector<int> a{ 21,65,21,51,85,1,24,10 };
    
        sort(a.begin(), a.begin() + 4,myfunction);//(21 21 51 65) 85 1 24 10
        cout << "myfunction排序后:";
        for (int i = 0; i < 8; i++) cout <<a[i] << " ";
        cout << endl;
    
        sort(a.begin()+4, a.begin() + 8, myfunction2);//21 21 51 65( 85 24 10 1)在第一次排序的基础上后4个数的排序
        cout << "myfunction2排序后:";
        for (int i = 0; i < 8; i++) cout << a[i] << " ";
        cout << endl;
    
        return 0;
    }
    
  • 参考链接

    C++ sort排序函数用法_浅然言而信的博客-优快云博客_c++ sort排序

    C++ sort()排序函数用法详解 (biancheng.net)

### C++ 中 `std::sort` 的字符串排序使用方法 在 C++ 中,标准库提供了 `std::sort` 函数来对数组或容器中的元素进行排序。对于字符串排序,可以利用其默认行为按字典序排列字符串[^3]。 #### 默认字典序排序 当需要按照字典序对一组字符串进行升序排序时,可以直接调用 `std::sort` 而无需额外指定比较函数: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<std::string> strings = {"banana", "apple", "orange", "grape"}; std::sort(strings.begin(), strings.end()); // 默认按字典序升序 for (const auto& s : strings) { std::cout << s << "\n"; } return 0; } ``` 上述代码会输出如下结果(基于字典序): ``` apple banana grape orange ``` 如果要实现降序排序,则可以通过提供自定义的比较函数完成此操作[^2]。 #### 自定义比较函数实现降序排序 通过传递一个 lambda 表达式或其他形式的比较器给 `std::sort`,能够改变默认的行为以满足特定需求,比如反向排序: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<std::string> strings = {"banana", "apple", "orange", "grape"}; auto cmp = [](const std::string& a, const std::string& b) -> bool { return a > b; // 反转比较逻辑 }; std::sort(strings.begin(), strings.end(), cmp); for (const auto& s : strings) { std::cout << s << "\n"; } return 0; } ``` 这段代码的结果将是倒序排列后的列表: ``` orange pineapple grape apple ``` 需要注意的是,在某些情况下可能还需要考虑其他因素如忽略大小写或者处理特殊字符等问题[^1]。 ### 数值型字符串排序 针对含有数字成分的字符串序列,普通的字典序可能会导致不符合预期的结果。此时可采用专门设计用来解决这类问题的方法——即 numerical string sort 技术。 例如有这样一个例子:“file1.txt”, “file10.txt”, 和“file2.txt”。如果我们单纯依赖于字母表顺序来进行分类的话,“file10.txt”会被错误地位列在“file2.txt”的前面;而运用数值敏感的方式则能正确得到期望次序:"file1.txt","file2.txt","file10.txt". 综上所述,无论是简单的词组还是复杂的带编号文件名都可以借助调整好的规则达成理想的整理效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值