Mail Archives: djgpp/2000/11/02/15:01:37
Steamer wrote:
>
> Rodeo Red wrote:
>
> > 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.
>
> In a library file I assume (libstdcxx.a I think). So you can't look
> at the code for them unless you want to download the entire GCC source.
> But the prototypes should be enough to see what is supported.
>
yes I really don't need the functions right now, but I always wondered
where they were. Thanks for clearing that up.
> > Anyway, you posted 6 versions of compare, and there are three here, so
> > does that mean these three area the ones supported by djgpp ?
>
> Actually, I only posted 5 versions of compare - that's all there are
> in the standard. The three above are evidently the only ones that
> DJGPP supports - and they are incompatible with the standard ones
> if used with more than one argument.
>
> > > > 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 ?
>
> I mean that std::replace() works on containers, and replaces individual
> elements of the container - which is not what you were trying to do.
> But see below.
>
> > 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().
>
> These appear to be referring to the replace() member function
> of std::basic_string. You were using std::replace(), which is
> something quite different - it's a template function that can
> work on all sorts of containers.
>
> > 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.
>
> You can use the replace() member function. Here's an example:
>
> #include <iostream>
> #include <string>
> using std::cout;
> using std::string;
>
> int main()
> {
> string str = "The rain in Spain falls mainly on the plain.";
> string oldval = "rain";
> string newval = "sleet";
>
> cout << str << '\n';
> string::size_type pos = str.find(oldval);
> if (pos != string::npos) {
> str.replace(pos, oldval.size(), newval);
> cout << str << '\n';
> }
> }
That clear up alot- thanks.
int compare(size_type pos1, size_type n1, const basic_string& str)
const;
It appears I misunderstood the text of Lippman and Lajoie - I just want
to emphasize that as far as I can tell it is a good reliable book. I've
had my share of bad book so I appreciate it.
they used
replace( vec.begin(), vec.end(), oldval, newval);
whre old val and new val are strings, in a vector. I mistakenly used
replace (str.begin(), str.end(), oldval, newval) ;
Which doesn't work because old val and new val are strings in a
string.
I'm still a little confused with the terminology-
A string is a container but each character is an element in itself -
right ?
Red
- Raw text -