#include <algorithm>
#include <string>
#include <vector>
int GetShortestStringLengthCpp11(const std::vector<std::string>& v)
{
const std::vector<std::string>::const_iterator i
= std::min_element(
v.begin(),
v.end(),
[](const std::string& lhs, const std::string& rhs)
{
return lhs.size() < rhs.size();
}
);
if (i!=v.end()) return static_cast<int>(i->size());
return 0;
}
int GetShortestStringLengthCpp98ForLoop(const std::vector<std::string>& v)
{
if (v.empty()) return 0;
int shortest = std::numeric_limits<int>::max();
const int sz = v.size();
for (int i=0; i!=sz; ++i)
{
shortest = std::min(static_cast<int>(v[i].size()),shortest);
}
return shortest;
}
#ifdef IF_I_ONLY_KNEW_HOW_TO_GET_THIS_WORKING_7737346578649782927896
#include <functional>
int GetShortestStringLengthCpp98Functor(const std::vector<std::string>& v)
{
const std::vector<std::string>::const_iterator i
= std::min_element(
v.begin(),
v.end(),
std::mem_fun_ref(&std::string::size)
);
if (i!=v.end()) return static_cast<int>(i->size());
return 0;
}
#endif
bool StringLengthCompare(const std::string& lhs, const std::string& rhs)
{
return lhs.size() < rhs.size();
}
int GetShortestStringLengthCpp98CustomFunctor(const std::vector<std::string>& v)
{
const std::vector<std::string>::const_iterator i
= std::min_element(
v.begin(),
v.end(),
StringLengthCompare
);
if (i!=v.end()) return static_cast<int>(i->size());
return 0;
}
#include <boost/foreach.hpp>
int GetShortestStringLengthBoostForeach(const std::vector<std::string>& v)
{
if (v.empty()) return 0;
int shortest = std::numeric_limits<int>::max();
BOOST_FOREACH(const std::string& s,v)
{
shortest = std::min(static_cast<int>(s.size()),shortest);
}
return shortest;
}
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
int GetShortestStringLengthBoostBind(const std::vector<std::string>& v)
{
const std::vector<std::string>::const_iterator i
= std::min_element(
v.begin(),
v.end(),
boost::bind(&std::string::size,boost::lambda::_1) < boost::bind(&std::string::size,boost::lambda::_2)
);
if (i!=v.end()) return static_cast<int>(i->size());
return 0;
}
#include <cassert>
int main()
{
{
const std::vector<std::string> v = { "12","123","123456","12345678","123456789" };
const int expected = 2;
assert(GetShortestStringLengthCpp11(v) == expected);
assert(GetShortestStringLengthBoostBind(v) == expected);
assert(GetShortestStringLengthBoostForeach(v) == expected);
assert(GetShortestStringLengthCpp98CustomFunctor(v) == expected);
assert(GetShortestStringLengthCpp98ForLoop(v) == expected);
}
{
const std::vector<std::string> v = { "1","23","456","78","123456789","01","23","456","78","9" };
const int expected = 1;
assert(GetShortestStringLengthCpp11(v) == expected);
assert(GetShortestStringLengthBoostBind(v) == expected);
assert(GetShortestStringLengthBoostForeach(v) == expected);
assert(GetShortestStringLengthCpp98CustomFunctor(v) == expected);
assert(GetShortestStringLengthCpp98ForLoop(v) == expected);
}
{
const std::vector<std::string> v = { "x" };
const int expected = 1;
assert(GetShortestStringLengthCpp11(v) == expected);
assert(GetShortestStringLengthBoostBind(v) == expected);
assert(GetShortestStringLengthBoostForeach(v) == expected);
assert(GetShortestStringLengthCpp98CustomFunctor(v) == expected);
assert(GetShortestStringLengthCpp98ForLoop(v) == expected);
}
{
const std::vector<std::string> v = { };
const int expected = 0;
assert(GetShortestStringLengthCpp11(v) == expected);
assert(GetShortestStringLengthBoostBind(v) == expected);
assert(GetShortestStringLengthBoostForeach(v) == expected);
assert(GetShortestStringLengthCpp98CustomFunctor(v) == expected);
assert(GetShortestStringLengthCpp98ForLoop(v) == expected);
}
}
|