本题为《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;
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。