I did this function:
void clean(string&s)
{
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s.erase(i,1);
}
return;
}
I'm having a strange behavior with string like these:
"Disneyland " -> Disneyland
".disneylan d." -> .disneyland"
It seems as if there is something going on with the erase function that
i don't know, because with this:
void clean(string&s)
{
string s2;
for(size_t i=0; i<s.size(); ++i)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
s2+=s[i];
}
}
s=s2;
return;
}
Everything goes OK.
What's wrong with the first code?
Thanks.
Jun 3 '06
11 1778
Daniel T. wrote: In article <dv************ ********@news.s iol.net>, dj <sm*******@lyco s.com> wrote:
In cases like this, i prefer to traverse the container in reverse order. In that case the part of the container we have already examined is shifted and presents no problem.
Wouldn't you agree?
No, still too many assignments and redundant checks when an element is removed. Better would be:
void clean( string& s ) { string::size_ty pe insert = 0; for ( string::size_ty pe index = 0; index != s.size(); ++index ) if ( isalpha( s[index] ) ) s[insert++] = tolower( s[index] ); s.erase( insert, string::npos ); }
That way, each character is only moved once. (strictly speaking that last line could be "s.erase( insert );" but that doesn't make it clear that all elements from insert to the end of the string are being erased.)
I don't believe I follow you - what redundant checks and assignments?
void clean( string& s )
{
for ( string::size_ty pe index = s.size()-1; index >= 0; --index ) {
if ( isalpha( s[index] ) )
s[index] = tolower( s[index] );
else
s.erase( index, 1 );
}
}
Of course, the code with single erase is certainly faster when multiple
erasures are needed, though it includes an additional post-increment
(which itself expands into an assignment and increment). However, if the
container stores objects that need destruction (like classes), the code
with single erase is not useful (overwriting in-place would leave
dangling objects).
In article <w2************ ********@news.s iol.net>,
dj <sm*******@lyco s.com> wrote: Daniel T. wrote: In article <dv************ ********@news.s iol.net>, dj <sm*******@lyco s.com> wrote:
In cases like this, i prefer to traverse the container in reverse order. In that case the part of the container we have already examined is shifted and presents no problem.
Wouldn't you agree? No, still too many assignments and redundant checks when an element is removed. Better would be:
void clean( string& s ) { string::size_ty pe insert = 0; for ( string::size_ty pe index = 0; index != s.size(); ++index ) if ( isalpha( s[index] ) ) s[insert++] = tolower( s[index] ); s.erase( insert, string::npos ); }
That way, each character is only moved once. (strictly speaking that last line could be "s.erase( insert );" but that doesn't make it clear that all elements from insert to the end of the string are being erased.)
I don't believe I follow you - what redundant checks and assignments?
Sorry, I was wrong about the redundant checks. You followed me on the
redundant assignments though (see below.)
void clean( string& s ) { for ( string::size_ty pe index = s.size()-1; index >= 0; --index ) { if ( isalpha( s[index] ) ) s[index] = tolower( s[index] ); else s.erase( index, 1 ); } }
Of course, the code with single erase is certainly faster when multiple erasures are needed, though it includes an additional post-increment (which itself expands into an assignment and increment). However, if the container stores objects that need destruction (like classes), the code with single erase is not useful (overwriting in-place would leave dangling objects).
True, the code I posted assumes value semantics, but that's OK because
we are dealing with a string, not an arbitrary container.
--
Bene disserere est finis logices. -- Aristotle This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: muser |
last post by:
is string converted to its integer equivalent by minusing it by 48?
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.
bool checkdigitforcustomercode( char* string )
{
|
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using namespace std;
:
: vector<string>* getTyphoon()
|
by: lenny |
last post by:
Hi,
I've been trying to use a Sub or Function in VBA to connect to a
database, make a query and return the recordset that results from the
query. The connection to the database and the query works fine, but
passing the resulting recordset back to the sub's caller is not working
out.
|
by: Ian Stanley |
last post by:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why it
shouldn't ?
Any further help as to what else I am doing wrong will be appreciated
regards
|
by: Jeff S |
last post by:
In a VB.NET code behind module, I build a string for a link that points to a
JavaScript function. The two lines of code below show what is relevant.
PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34)
& ", " & Chr(34) & CurrentEventDetails & ")"
strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
"</A><BR>"
The problem I have is that when the string variables or
contain a string with an...
| |
by: Ankit Aneja |
last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx
some .aspx pages are in root ,some in folder named admin and some in folder
named user
aspx pages which are in user folder are using this code of url rewrite
project is running completely fine on localhost
but after uploading first page
(http://emailware.net.temporary.domain.name/user/index.aspx) is fine
but as i click
123 Easy-CD Ripper
|
by: David Scemama |
last post by:
Hi,
I'm trying to read a database file written from a turbo Pascal program. I've
set a structure to map the records in the file, but
I have problem reading the file when I use VBFixedArray in my structure
instead of VBFixedString.
Here is the original code that works:
Structure CCat
|
by: Dany |
last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by Microsoft must be installed on their servers.
Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses.
This can be easily verified:
Implement the following in a web service method (just raises a SOAPException with a...
|
by: Stacey Levine |
last post by:
I have a webservice that I wanted to return an ArrayList..Well the service
compiles and runs when I have the output defined as ArrayList, but the WSDL
defines the output as an Object so I was having a problem in the calling
program. I searched online and found suggestions that I return an Array
instead so I modified my code (below) to return an Array instead of an
ArrayList. Now I get the message when I try to run just my webservice...
|
by: Rinaldo |
last post by:
Hi,
When I start my program in the debugger, there is no problem, but when not I
get an exception.
It appears in:
private void Upload(string filename, string FTnaam)
{
MessageBox.Show("in upload.");
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |