【发布时间】:2013-03-16 10:23:58
【问题描述】:
根据结构向量中所有结构的每个向量中的第一个单词按字母顺序对结构向量进行排序的最佳方法是什么?
struct sentence{
vector<string> words;
};
vector<sentence> allSentences;
也就是说,如何根据 words[0] 对所有句子进行排序?
编辑:我使用了以下解决方案:
bool cmp(const sentence& lhs, const sentence & rhs)
{
return lhs.words[0] < rhs.words[0];
}
std::sort(allSentences.begin(), allSentences.end(), cmp);
【问题讨论】:
-
感谢您的精彩回答。也许有人可以解释为什么这不起作用:return lhs.wordCombinated[0].compare(rhs.wordCombinated[0]);