【发布时间】:2014-11-26 02:09:52
【问题描述】:
使用来自cplusplus.com 的代码,我试图在vector<std::string> 中找到最大int 值。
向量是 std::string 格式,这里没有选择。
bool myfn(int i, int j)
{
return i < j;
}
vector<std::string> dat;
dat.push_back(2.1);
dat.push_back(5.3);
for (int l = 0; l < dat.size(); ++l)
{
std::cout << *std::max_element(dat.begin(), dat.end(), myfn) << '\n';
}
预期输出:
3
错误:
.cpp:76:93: error: no matching function for call to 'max_element(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >::size_type, bool (&)(int, int))'
如何找到vector<std::string> 中的最大值?
【问题讨论】:
-
myfn完全没有必要。默认是做不到。 -
你的循环没用。
max_element将覆盖整个范围。