知秋晨气润 2021-10-06 16:03 采纳率: 100%
浏览 76
已结题

C++中如何编写sort函数

本人初学计算机C++语言,练习vector,需要用到sort函数,但是电脑中没有这个函数,请问如何编写sort函数?

img

  • 写回答

4条回答 默认 最新

  • 小公鸡卡哇伊呀~ 2021-10-06 23:43
    关注

    又试了一下,windows Ctrl + Z 是可以终止输入的:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        vector<string> words;
        for (string temp; cin >> temp;)
            words.push_back(temp);
    
        cout << words.size() << endl;
        sort(words.begin(), words.end());
        for (size_t i = 0; i < words.size(); i++)
            cout << words[i] << " ";
        // 删除重复的单词
        words.erase(unique(words.begin(), words.end()), words.end());
        cout << endl;
        cout << words.size() << endl;
        for (size_t i = 0; i < words.size(); i++)
            cout << words[i] << " ";
    
        return 0;
    }
    

    测试如下:

    this is my vector you know my vector
    ^Z
    8
    is know my my this vector vector you
    6
    is know my this vector you 请按任意键继续. . .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 已采纳回答 10月7日
  • 创建了问题 10月6日