【发布时间】:2011-05-29 11:56:19
【问题描述】:
给定一个std::vector< std::string >,向量是按字符串长度排序的,我怎样才能找到一个长度相等的范围?
我期待 C++ 中的惯用解决方案。
我找到了这个解决方案:
// any idea for a better name? (English is not my mother tongue)
bool less_length( const std::string& lhs, const std::string& rhs )
{
return lhs.length() < rhs.length();
}
std::vector< std::string > words;
words.push_back("ape");
words.push_back("cat");
words.push_back("dog");
words.push_back("camel");
size_t length = 3;
// this will give a range from "ape" to "dog" (included):
std::equal_range( words.begin(), words.end(), std::string( length, 'a' ), less_length );
有没有标准的方法(漂亮地)?
【问题讨论】: