我这样做是因为我学习编程的网站,我根本无法解决这个问题.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int n;
string thename;
vector <pair<string,int>> name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> thename;
name.push_back(make_pair(thename, thename.length()));
}
sort(name.begin(), name.end());
sort(name.begin(), name.end(),
[](const pair<string, int>& lhs, const pair<string, int>& rhs) {
return lhs.second < rhs.second; } );
for (int i = 0; i < n; i++)
{
cout << name[i].first<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我的老人建议我不要使用lambda排序,因为我自己仍然不太了解它.但我仍会接受任何lambda排序的答案.谁能帮我?