delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/11/02/15:01:37

From: Rodeo Red <rodeored AT netstep DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: compare()
Date: Thu, 02 Nov 2000 14:58:28 -0500
Organization: Church of Evangelical Environmental Extremism
Lines: 111
Message-ID: <0C52CBEA9BF6346B.AD1B03126E6F4A63.7F91F834C1B967F2@lp.airnews.net>
X-Orig-Message-ID: <3A01C763 DOT 886401EE AT netstep DOT net>
References: <1F7509A3BB20243A DOT 7FD3AC2DC483DD04 DOT 6BB6DE9721314E90 AT lp DOT airnews DOT net> <39fea8f5 DOT 9349153 AT news DOT freeserve DOT net> <2D3CEFEF9F5772C9 DOT 21D15067E7ACE130 DOT 3275BE6B77FD834E AT lp DOT airnews DOT net> <39fecce1 DOT 18547083 AT news DOT freeserve DOT net> <E80A6618268722F2 DOT 334C4577FE04F5AA DOT 3D933E68350DF73B AT lp DOT airnews DOT net> <3a017252 DOT 17107696 AT news DOT freeserve DOT net> <7AB2F68195D27780 DOT 3FC067FFF65795BB DOT 71FA8F3C8AC71107 AT lp DOT airnews DOT net> <3a01add5 DOT 32346310 AT news DOT freeserve DOT net>
Abuse-Reports-To: support at netstep.net to report improper postings
NNTP-Proxy-Relay: library2.airnews.net
NNTP-Posting-Time: Thu Nov 2 13:49:30 2000
NNTP-Posting-Host: ![dI_-@[.cL-$_n (Encoded at Airnews!)
Mime-Version: 1.0
X-Mailer: Mozilla 4.7 [en] (Win98; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019