Mail Archives: djgpp/2000/11/02/12:01:26
Steamer wrote:
>
> Rodeo Red wrote:
>
> > Now Ive got a similar problem with replace() and I don't know if this is
> > the same situation. How do I tell if my function is not supported ?
>
> I'm not sure if there's a better way than simply trying it, or looking in
> the header files (c:\djgpp\lang\cxx).
Well I'll try, but since I easily get lost I'll describe the experience.
I opened c:\djgpp\lang\cxx\string
which did not have the prototypes for compare or replace but it did have
#include <std/bastring.h>
So I opened c:\djgpp\lang\cxx\std\bastring.h and found this:
int compare (const basic_string& str, size_type pos = 0, size_type n =
npos) const;
// There is no 'strncmp' equivalent for charT pointers.
int compare (const charT* s, size_type pos, size_type n) const;
int compare (const charT* s, size_type pos = 0) const
{ return compare (s, pos, traits::length (s)); }
Only the last has an actual function. If these are the prototypes for
compare(), where are the actual functions for the first two ? In another
file I presume but I don't know where.
Anyway, you posted 6 versions of compare, and there are three here, so
does that mean these three area the ones supported by djgpp ?
>
> > I'd like to be able to know, because if it is supported, the error is
> > probably mine.
>
> I've downloaded Borland C++ 5.5, so I can always get a second opinion on
> the correctness of my code.
Good idea- I actually have it too but had trouble getting it to work.
> DJGPP is very good for C, but C++ is another
> matter - although things should change once GCC 3.0 appears and is ported
> to DJGPP. (Borland's compiler is probably buggier than GCC, but its
> C++ library is currently more complete.)
>
> > This is based on the example on page 1173 pf c++ Primer by Lippman and
> > Lajoie, a reliable text.
>
> Not so reliable if this is a real example from the book...
>
> > #include <string>
> > #include <algorithm>
>
> You should have here:
>
> #include <iostream>
> using namespace std;
>
> > int main()
> > {
> > string str = "The rain in Spain falls mainly on the plain\n";
> > cout << "string:"<< str;
> > string oldval = "rain";
> > string newval = "sleet";
> > replace (str.begin(), str.end(), oldval, newval) ;
>
> This is incorrect. The function std::replace() replaces elements,
> not sequences of elements. For example:
>
> replace(str.begin(), str.end(), 'i', 'j');
>
> This would replace every i in str by a j.
>
> > cout << "new str:"<< str;
> > }
--
I'm not sure what you mean by "sequence". Do you mean you can't use a
string class object ?
According to Lippman and Lajoie, "replace() substitutes one or more
characters within a string with one or more alternative characters ".
Stroustrup says "Once a position is itentified, the value of individual
character positions can be changed using subscripting or whole
substrings can be replaced with new characters using replace().
basically I'd like to replace a character in a string with a second
string. I guess I have to use substr(), but it seems like the long way.
In looking at the prototypes for replace(), I'm wondering what const
basic_string means.
red
- Raw text -