GetLongestStringLength is a std::string and container code snippet to obtain the length/size of the longest/largest std::string in a container.
There are multiple versions of GetLongestStringLength:
- GetLongestStringLength using C++98 and a for-loop
- GetLongestStringLength using C++98 and a global functor
- GetLongestStringLength using boost::bind and boost::lambda
- GetLongestStringLength using BOOST_FOREACH
- GetLongestStringLength using C++11
GetLongestStringLength using C++98 and a for-loop
#include <string> #include <vector> int GetLongestStringLength(const std::vector<std::string>& v) { int longest = 0; const int sz = v.size(); for (int i=0; i!=sz; ++i) { longest = std::max(static_cast<int>(v[i].size()),longest); } return longest; }
GetLongestStringLength 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 GetLongestStringLength(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_element( v.begin(), v.end(), StringLengthCompare ); if (i!=v.end()) return static_cast<int>(i->size()); return 0; }
GetLongestStringLength using boost::bind and boost::lambda
#include <algorithm> #include <string> #include <vector> #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> int GetLongestStringLength(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_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 <string> #include <vector> #include <boost/foreach.hpp> int GetLongestStringLength(const std::vector<std::string>& v) { int longest = 0; BOOST_FOREACH(const std::string& s,v) { longest = std::max(static_cast<int>(s.size()),longest); } return longest; }
GetLongestStringLength using C++11
#include <algorithm> #include <string> #include <vector> int GetLongestStringLength(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_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; }
Operating system(s) or programming environment(s)
- Lubuntu 12.10 (quantal)
- Qt Creator 2.5.2
- G++ 4.7.2
Libraries used:
- STL: GNU ISO C++ Library, version 4.7.2
Qt project file: CppGetLongestStringLength.pro
TEMPLATE = app CONFIG += console CONFIG -= qt SOURCES += main.cpp QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror
#include <algorithm> #include <string> #include <vector> int GetLongestStringLengthCpp11(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_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 GetLongestStringLengthCpp98ForLoop(const std::vector<std::string>& v) { int longest = 0; const int sz = v.size(); for (int i=0; i!=sz; ++i) { longest = std::max(static_cast<int>(v[i].size()),longest); } return longest; } #ifdef IF_I_ONLY_KNEW_HOW_TO_GET_THIS_WORKING_7737346578649782927896 #include <functional> int GetLongestStringLengthCpp98Functor(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_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 GetLongestStringLengthCpp98CustomFunctor(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_element( v.begin(), v.end(), StringLengthCompare ); if (i!=v.end()) return static_cast<int>(i->size()); return 0; } #include <boost/foreach.hpp> int GetLongestStringLengthBoostForeach(const std::vector<std::string>& v) { int longest = 0; BOOST_FOREACH(const std::string& s,v) { longest = std::max(static_cast<int>(s.size()),longest); } return longest; } #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> int GetLongestStringLengthBoostBind(const std::vector<std::string>& v) { const std::vector<std::string>::const_iterator i = std::max_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 = { "1","123","123456","12345678","123456789" }; const int expected = 9; assert(GetLongestStringLengthCpp11(v) == expected); assert(GetLongestStringLengthBoostBind(v) == expected); assert(GetLongestStringLengthBoostForeach(v) == expected); assert(GetLongestStringLengthCpp98CustomFunctor(v) == expected); assert(GetLongestStringLengthCpp98ForLoop(v) == expected); } { const std::vector<std::string> v = { "1","23","456","78","123456789","01","23","456","78","9" }; const int expected = 9; assert(GetLongestStringLengthCpp11(v) == expected); assert(GetLongestStringLengthBoostBind(v) == expected); assert(GetLongestStringLengthBoostForeach(v) == expected); assert(GetLongestStringLengthCpp98CustomFunctor(v) == expected); assert(GetLongestStringLengthCpp98ForLoop(v) == expected); } { const std::vector<std::string> v = { "x" }; const int expected = 1; assert(GetLongestStringLengthCpp11(v) == expected); assert(GetLongestStringLengthBoostBind(v) == expected); assert(GetLongestStringLengthBoostForeach(v) == expected); assert(GetLongestStringLengthCpp98CustomFunctor(v) == expected); assert(GetLongestStringLengthCpp98ForLoop(v) == expected); } { const std::vector<std::string> v = { }; const int expected = 0; assert(GetLongestStringLengthCpp11(v) == expected); assert(GetLongestStringLengthBoostBind(v) == expected); assert(GetLongestStringLengthBoostForeach(v) == expected); assert(GetLongestStringLengthCpp98CustomFunctor(v) == expected); assert(GetLongestStringLengthCpp98ForLoop(v) == expected); } }