根據結構向量中所有結構的每個向量中的第一個單詞按字母順序排列結構向量的最佳方法是什麼?如何根據要分類的向量內的向量<string>對結構向量進行排序?
struct sentence{
vector<string> words;
};
vector<sentence> allSentences;
換句話說,如何根據單詞[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]); – gkraft 2013-03-27 08:40:18