|
Boost : |
From: Craig Henderson (cdm.henderson_at_[hidden])
Date: 2002-10-19 11:27:36
"Srivatsan Raghavan" <vraghavan_at_[hidden]> wrote in message
news:3DB0882B.9070604_at_cnmnetwork.com...
> Craig Henderson wrote:
> >
> >How do you propose a simplified version? Perhaps
> >template<typename T>
> >void to_upper(T &cont)
> >{
> > std::transform(cont.begin(), cont.end(), cont.begin(), ::toupper);
> >}
> >
> >or something similar ?
> >
>
> well.. to do it right would require a bit more than that :)
>
> (preface : i'm in a c++ channel on irc, and this is code on our bot, but
> this is some of the code we have for this sorta thing, and i think it
> works rather well )
>
> //------code-----
> //tolower = applies tolower on a std::string
> #include <algorithm>
> #include <functional>
> #include <string>
> #include <cctype>
> std::string&
> tolower (std::string &s)
> {
> std::transform(s.begin(), s.end(),s.begin(),
> std::ptr_fun<int, int>(std::tolower));
> return s;
> }
> //tolower = applies tolower on a char* array
> #include <algorithm>
> #include <functional>
> #include <cctype>
> #include <cstring>
> char*
> tolower (char buf[], size_t n)
> {
> std::transform(buf, buf + n,buf,
> std::ptr_fun<int, int>(std::tolower));
> return buf;
> }
> inline char*
> tolower (char buf[])
> {
> return tolower(buf, std::strlen(buf));
> }
> //-----end code -----------
Ok, but my point was to template the container, and therefore the algorithms
can be used on other sequences without numerous overloads. For example, if
one had a vector<char>, then this would need another overload, but the
signature I proposed would just work without more code.
Templating on the iterator would be more STL-like, and a much better
interface.
template<typename ItIn1, ItIn2>
inline void to_upper(ItIn1 begin1, ItIn1 end1, OutIt out)
{ ... }
Regards
-- Craig
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk