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

From: dontmailme AT iname DOT com (Steamer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: compare()
Date: Thu, 02 Nov 2000 18:11:00 GMT
Organization: always disorganized
Lines: 83
Message-ID: <3a01add5.32346310@news.freeserve.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>
NNTP-Posting-Host: modem-137.dwarf-lion-fish.dialup.pol.co.uk
X-Trace: newsg2.svr.pol.co.uk 973188662 30470 62.137.4.137 (2 Nov 2000 18:11:02 GMT)
NNTP-Posting-Date: 2 Nov 2000 18:11:02 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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.  

> 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';
    }
}

- Raw text -


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