【发布时间】:2016-04-28 17:29:58
【问题描述】:
如果有结构体:
#include <algorithm>
#include <vector>
#include <iomanip>
#include <string>
using namespace std;
bool pred(string *a, string *b){
return *a < *b;
}
struct Student {
int ID;
int age;
double gpa;
string firstname;
string lastname;
};
int main () {
vector<Student*>v;
vector<Student*>v_sortedFirstName;
//both filled with same information
// sort v_sortedFirstName by first name
sort(v_sortedFirstName.begin(), v_sortedFirstName.end(), pred);
}
现在假设向量v 填充了信息,v_sortedFirstName 填充了相同的信息(指向与v 相同的点)。我将如何(使用 STL 排序功能,按 firstname 对 v_sortedFirstName 进行排序?
我在想这条线:sort(v_sortedFirstName.begin(), v_sortedFirstName.end(), pred);
应该是类似于sort(v_sortedFirstName->firstname.begin(), v_sortedFirstName->firstname.end(), pred); 的东西,但这不起作用。
另外,如果你们不介意,我想坚持我上面的谓词函数,不要使用 Lambda,因为我还没有学过。
【问题讨论】: