c – 如何根据要排序的向量中的向量对结构的向量进行排序?
发布时间:2020-12-16 10:24:00 所属栏目:百科 来源:网络整理
导读:基于结构向量中所有结构的每个向量中的第一个单词按字母顺序对结构向量进行排序的最佳方法是什么? struct sentence{ vectorstring words;};vectorsentence allSentences; 换句话说,如何基于单词[0]对allSentence进行排序? 编辑:我使用以下解决方案: bool
基于结构向量中所有结构的每个向量中的第一个单词按字母顺序对结构向量进行排序的最佳方法是什么?
struct sentence{ vector<string> words; }; vector<sentence> allSentences; 换句话说,如何基于单词[0]对allSentence进行排序? 编辑:我使用以下解决方案: bool cmp(const sentence& lhs,const sentence & rhs) { return lhs.words[0] < rhs.words[0]; } std::sort(allSentences.begin(),allSentences.end(),cmp); 解决方法
提供合适的比较二进制函数并将其传递给std :: sort.例如
bool cmp(const sentence& lhs,const sentence & rhs) { return lhs.words[0] < rhs.words[0]; } 然后 std::sort(allSentences.begin(),cmp); 或者,在C 11中,您可以使用lambda匿名函数 std::sort(allSentences.begin(),[](const sentence& lhs,const sentence & rhs) { return lhs.words[0] < rhs.words[0];} ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |