C++ 輸入字符串按字典順序排序

本題爲《C++程序設計原理與實踐》Chapter3 習題7
參考鏈接:
C++中輸入字符串的幾種方法
C++ 字符串與字符數組 詳解

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void PrintF(string& StringPrint) {
    cout << StringPrint << endl;
}
int main() {
    vector<string> studentName;
    vector<string>::iterator studentIterator;
    string str1, str2, str3;
    getline(cin, str1);
    getline(cin, str2);
    getline(cin, str3);
    studentName.push_back(str1);
    studentName.push_back(str2);
    studentName.push_back(str3);
    //輸出未排序的名字
    cout << "排序前的名字:" << endl;
    for_each(studentName.begin(), studentName.end(), PrintF);
    sort(studentName.begin(), studentName.end());  //排序函數
    cout << "排序後的名字:" << endl;
    for_each(studentName.begin(), studentName.end(), PrintF);
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章