Topic: string (stdc++) Vs String (libg++)


Author: Ralf Stoffels <stoffels@faho.rwth-aachen.de>
Date: 1998/04/16
Raw View
> ---------------------------------------------------------------------------
> libg++                libstdc++
> ---------------------------------------------------------------------------
> String::upcase()      No equivalent - write one from scratch using toupper()
>                       on each individual character.
>
> String::downcase()    No equivalent - write one from scratch using tolower()
>                       on each individual character.

These functions are localization specific.
Default localization:
use_facet<ctype<char> >(locale()).toupper(str.begin(), str.end());


Ralf
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Bjorn Fahller <Bjorn.Fahller@ebc.ericsson.se>
Date: 1998/04/16
Raw View
Jamie M Scuglia wrote:

> Having converted some code from using the old libg++ String class into using
> the stdc++ string template class, these are some things I really do miss
> from the old String class.  If anyone knows any easy way to do the things I
> list below, please let me know.

3 out of 4 are reasonably easy (although perhaps not as easy as one would like.)

> String::upcase()      No equivalent - write one from scratch using toupper()
>                       on each individual character.

No need to write one from scratch. Use
std::transform(s.begin(),s.end(),s.begin(),toupper);If you're in an
international setting, you might want to use a specific locale, in which case
you will need std::bind2nd(toupper,loc) instead of just toupper, where "loc" is
the locale object you use.

> String::downcase()    No equivalent - write one from scratch using tolower()
>                       on each individual character

Equivalent..

> String::gsub()        No equivalent - use a mixture of string::find() and
>                       string::replace() in a loop to locate and then replace
>                       each occurence of what we want to change.

True, no good substitute.

> String::freq()        No equivalent - must somehow step thru the string and
>                       count the number of occurences of the string or
>                       character we are searching for.

Again a standard algorithm, std::count will do fine.
std::count(s.begin(),s.end(),c);
   _
/Bjorn.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/04/16
Raw View
Bjorn Fahller wrote:
>
> Jamie M Scuglia wrote:

[...]

> > String::upcase()      No equivalent - write one from scratch using toupper()
> >                       on each individual character.
>
> No need to write one from scratch. Use
> std::transform(s.begin(),s.end(),s.begin(),toupper);If you're in an
> international setting, you might want to use a specific locale, in which case
> you will need std::bind2nd(toupper,loc) instead of just toupper, where "loc" is
> the locale object you use.
>
> > String::downcase()    No equivalent - write one from scratch using tolower()
> >                       on each individual character
>
> Equivalent..
>

If transformations of one container are quite frequent, you might
consider an extra template like:

template<class Container, class Function>
 void change(Container& c, Function f)
{
  std::transform(c.begin(), c.end(), c.begin(), f);
}

This allows simpler code like:

string s;
// ...
change(s, toupper);
change(s, tolower);

vector<int> v;
// ...
change(v, negate<int>());

[...]

> > String::freq()        No equivalent - must somehow step thru the string and
> >                       count the number of occurences of the string or
> >                       character we are searching for.
>
> Again a standard algorithm, std::count will do fine.
> std::count(s.begin(),s.end(),c);

This is only true for single characters. For substrings, it's not that
trivial. For example:

String s="How many times is does this question contain 'time' this
time?";
cout << s.freq("time"); // gives 3
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/04/16
Raw View
In article <3535A453.637D892@ebc.ericsson.se>,
  Bjorn Fahller <Bjorn.Fahller@ebc.ericsson.se> wrote:
>
> Jamie M Scuglia wrote:
>
> > Having converted some code from using the old libg++ String class into
using
> > the stdc++ string template class, these are some things I really do miss
> > from the old String class.  If anyone knows any easy way to do the things
I
> > list below, please let me know.
>
> 3 out of 4 are reasonably easy (although perhaps not as easy as one would
like.)
>
> > String::upcase()      No equivalent - write one from scratch using
toupper()
> >                       on each individual character.
>
> No need to write one from scratch. Use
> std::transform(s.begin(),s.end(),s.begin(),toupper);If you're in an
> international setting, you might want to use a specific locale, in which
case
> you will need std::bind2nd(toupper,loc) instead of just toupper, where
"loc" is
> the locale object you use.

The non-localized version results in undefined behavior on machines
where char is signed (most of them, I think).  You must use the
localized form in order for the code to work correctly.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jamies@indy15.cs.monash.edu.au (Jamie M Scuglia)
Date: 1998/04/15
Raw View
Having converted some code from using the old libg++ String class into using
the stdc++ string template class, these are some things I really do miss
from the old String class.  If anyone knows any easy way to do the things I
list below, please let me know.

---------------------------------------------------------------------------
libg++                libstdc++
---------------------------------------------------------------------------
String::upcase()      No equivalent - write one from scratch using toupper()
                      on each individual character.

String::downcase()    No equivalent - write one from scratch using tolower()
                      on each individual character.

String::gsub()        No equivalent - use a mixture of string::find() and
        string::replace() in a loop to locate and then replace
        each occurence of what we want to change.

String::freq()        No equivalent - must somehow step thru the string and
        count the number of occurences of the string or
        character we are searching for.
---------------------------------------------------------------------------



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]