(C++) GetShortestStringLength

January 7, 2018 · View on GitHub

 

 

 

 

 

(C++) GetShortestStringLength

 

GetShortestStringLength is a std::string and container code snippet to obtain the length/size of the shortest/largest std::string in a container.

 

There are multiple versions of GetShortestStringLength:

 

 

 

 

 

 

C++98 GetShortestStringLength using C++98 and a for-loop

 


#include <string> #include <vector> int GetShortestStringLength(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; }

 

 

 

 

 

C++98 GetShortestStringLength using C++98 and a global functor

 


#include <algorithm> #include <string> #include <vector> bool StringLengthCompare(const std::string& lhs, const std::string& rhs) {   return lhs.size() < rhs.size(); } int GetShortestStringLength(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; }

 

 

 

 

 

Boost GetShortestStringLength using boost::bind and boost::lambda

 


#include <algorithm> #include <string> #include <vector> #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> int GetShortestStringLength(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; }

 

 

 

 

 

Boost GetShortestStringLength using BOOST_FOREACH

 


#include <string> #include <vector> #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; }

 

 

 

 

 

C++11 GetShortestStringLength using C++11

 


#include <algorithm> #include <string> #include <vector> int GetShortestStringLength(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; }

 

 

 

 

 

Testing code

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.7.2

 

 

 

 

 

Qt project file: CppGetShortestStringLength.pro

 


TEMPLATE = app CONFIG += console CONFIG -= qt SOURCES += main.cpp QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror

 

 

 

 

 

main.cpp

 


#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);   } }