作者:手机用户2502904233 | 来源:互联网 | 2022-12-04 12:56
我有一组字符串
set strings;
如何获取集合中包含的最长字符串?在python中,我可以执行以下操作:
print max(strings, key=len)
c ++中有类似的功能吗?
1> lubgr..:
您可以将std::max_element
其与
标题一起使用并传递自定义比较谓词.
#include
#include
const auto lOngest= std::max_element(strings.cbegin(), strings.cend(),
[](const std::string& lhs, const std::string& rhs) { return lhs.size()
这显然不像python版本那么简洁,这就是范围可以解决的问题.使用range-v3投影,归结为
#include
const auto lOngest= ranges::max_element(strings, std::less<>{}, &std::string::size);