473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use != rather than < in for loops ?

In C++ Primer 4th, sec 3.3.2, it states that C++ programmers use !=
rather than < in a for loop.

The following small snippet erases punctuations in a string. It works
well with < used in the for loop but it breaks when != is used instead.

#include <string>
#include <iostream>
#include <exception>
#include <cctype>
using namespace std;

int main(){
string s = "hello, world!";//s: i18n
//for (string::size_t ype sz = 0; sz < s.size(); ++sz){
/*works with < */
for (string::size_t ype sz = 0; sz != s.size(); ++sz){
/*doesn't work with != */
try{
if (ispunct(s.at(s z))){
s.erase(sz, 1);
}
}catch(exceptio n &e){
cout << e.what() << endl;
}
}
cout << s << endl;
}

Jan 9 '07
32 2109
Julián Albo <JU********@ter ra.eswrote:
Daniel T. wrote:
There is no char '\xFF' in ASCII (which is what ispunct(int) and family
are designed to handle.)

Are you saying that C++ is only intended to machines with ascii compatible
charsets?
I'm saying that the is... functions in <cctypeare intended to be used
only on characters that can be represented using 7 bits. i.e., if
assert( ch & 0x7F == ch || ch == EOF ) fails, then the result returned
by ispunct and family on 'ch' is undefined. As such a cast to unsigned
char is meaningless. Again, I could be wrong on this, but that is my
understanding. If you know of an authoritative source that says the cast
is necessary, I'd love to see it.

There are a separate set of is... functions defined in <localthat can
be used for characters that are represented using more than 7 bits, and
a cast is never needed for any of those.
Jan 12 '07 #21
Daniel T. wrote:
I'm saying that the is... functions in <cctypeare intended to be used
only on characters that can be represented using 7 bits. i.e., if
Do you know of an authoritative source that says that? All I have readed is
that the int argument must be EOF or the value of an unsigned char.

--
Salu2
Jan 12 '07 #22
Julián Albo <JU********@ter ra.eswrote:
Daniel T. wrote:
I'm saying that the is... functions in <cctypeare intended to be
used only on characters that can be represented using 7 bits.
i.e., if

Do you know of an authoritative source that says that?
I don't have much in the way of authoritative sources. I was hoping
someone who had the standard could tell us one way or the other.

I did find, "In the C locale or in a locale where character type
information is not defined, characters are classified according to the
rules of the ASCII 7-bit coded character set." (Found at
http://www.mksxserver.com/docs/man3/ispunct.3.asp,
http://www-zeuthen.desy.de/apewww/AP.../ctype_8h.html,
http://ou800doc.caldera.com/en/man/h.../ctype.3C.html et al.)

I have to admit, I know very little about the locale system and fully
expect that those who ask about using ispunct here have not called
setlocale() (a function which is not even mentioned in TC++PL AFAICT.)

TC++PL does make a comment that passing a signed char into the cctype
functions can be problematic but it gives no details.

According to
http://www.dinkumware.com/manuals/?m...age=ctype.html the
only standard (i.e., not-implementation defined) characters that ispunct
will return true for are within the ascii character set. Passing a non
7-bit ASCII character to ispunct seems to be, at best, implementation
spicific.
Jan 12 '07 #23
Daniel T. wrote:
Julián Albo <JU********@ter ra.eswrote:
>Daniel T. wrote:
>>I'm saying that the is... functions in <cctypeare intended to be
used only on characters that can be represented using 7 bits.
i.e., if
Do you know of an authoritative source that says that?

I don't have much in the way of authoritative sources. I was hoping
someone who had the standard could tell us one way or the other.

I did find, "In the C locale or in a locale where character type
information is not defined, characters are classified according to the
rules of the ASCII 7-bit coded character set." (Found at
http://www.mksxserver.com/docs/man3/ispunct.3.asp,
http://www-zeuthen.desy.de/apewww/AP.../ctype_8h.html,
http://ou800doc.caldera.com/en/man/h.../ctype.3C.html et al.)
Those are manpages, specific to particular platforms.
I have to admit, I know very little about the locale system and fully
expect that those who ask about using ispunct here have not called
setlocale() (a function which is not even mentioned in TC++PL AFAICT.)

TC++PL does make a comment that passing a signed char into the cctype
functions can be problematic but it gives no details.

According to
http://www.dinkumware.com/manuals/?m...age=ctype.html the
only standard (i.e., not-implementation defined) characters that ispunct
will return true for are within the ascii character set. Passing a non
7-bit ASCII character to ispunct seems to be, at best, implementation
spicific.
Yes, those characters are all in the ASCII standard, but that doesn't
mean that they have to be represented by the equivalent ASCII values.
here is nothing preventing a conforming C++ implementation on a platform
that uses EBCDIC for instance (which is an 8-bit encoding).
--
Clark S. Cox III
cl*******@gmail .com
Jan 12 '07 #24
"Tr0n" <ol*********@ho tmail.comwrote in message
news:eo******** **@aioe.org...
I suppose it really depends on what it is used for - but I still think <
is less error-prone than a != (as any value can be != - while you're
limiting a range using <) .
No -- it's more error-prone, not less, because it makes it easier for errors
to escape undetected.
>In other words, if you use < for index comparisons, you make it easier to
verify that the loop doesn't do anything undefined, but harder to verify
that the loop produces the correct result.
What "correct" result are you after?
The last point in the loop?
.. Surely the data effected by the code inside the loop proves if it is
"correct" or not - and not the result of an incrementing index?
If you wanted the last point using "i != N" then you're after N... Why not
just USE N?
Here is a concrete example of what I'm talking about.

Suppose I want to write a loop that calls f(n) for every value of (integer)
n such that n>=0 and n<N.

Then I might write a loop with an invariant such as

I have called f(n) for every value of n such that n >= 0 and n < i

and initially establish the invariant by writing

i = 0;

This initialization of i establishes the invariant because there are no
values of n that are both >= 0 and < i.

The loop itself might look like this

while (i != N) { /* or i < N */
// increase i while maintaining the invariant
f(i);
++i;
}

Now, suppose we want to prove that this loop does what we intend.
Regardless of whether the loop condition is i != N or i < N, we have to
prove that the loop terminates.

If the condition is i != N, we can't prove that the loop terminates unless
we prove that N >= 0, a fact that you are claiming argues that i < N is
superior.

However, if the condition is i < N, that complicates the second part of the
proof. Because in that case, when the loop does terminate, we do not know
that i == n, which means that instead of proving the original requirement:

we have called f(n) for every value of (integer) n such that n >= 0 and
n < N

we have instead proved

we have called f(n) for every value of (integer) n such that n >= 0 and
n < i, where i is an unknown value >= n

In other words, in exchange for simplifying the proof of termination, we
have complicated the proof of correctness.

I would much rather simplify the proof of correctness, because if for some
reason the program doesn't terminate, we'll surely know about it. In other
words, I'd rather have a program that goes into an infinite loop than one
that quietly produces the wrong answer.
>Personally, I would rather write my programs in a way that makes it
easier to prove correctness.
.. I really don't like that.
Reminds me of all these buffer over-run errors where something unexpected
means CONSTANT bug-fixing.
I have no idea what you're talking about here.
It also reminds me of a current position I have at work, using an un-named
application (I don't think it's my place to name-names) which produced an
endless loop because of some incorrect data.. It took them 3 days to track
this down on a production environment (the data was incorrect for 2 weeks
over the xmas and only produced errors after people started again: adding
to the difficulty).

I actually support the previous posters' paragraph which states "limiting
value N", and "if it is incorrect to execute the loop body if.. i is
greater than N".

I just thought I'd post my thoughts on this, because I'd prefer my code to
do what I program - rather than produce a program which may do 'other
things'.
Exactly. And to be confident that your code does what you program, you
should make it as easy as possible to prove that it does what you intend.
Jan 12 '07 #25
Daniel T. wrote:
I did find, "In the C locale or in a locale where character type
information is not defined, characters are classified according to the
rules of the ASCII 7-bit coded character set." (Found at
http://www.mksxserver.com/docs/man3/ispunct.3.asp,
http://www-zeuthen.desy.de/apewww/AP.../ctype_8h.html,
http://ou800doc.caldera.com/en/man/h.../ctype.3C.html et al.)
Those looks like the documentation of an implementation, non a standard
description. And is highly likely that all those implementations are
intended for machines with ascii-compatible character set.

But even in that case, the locale can be not 'C' and can not work as 'C' in
this regard.
I have to admit, I know very little about the locale system and fully
expect that those who ask about using ispunct here have not called
setlocale()
Very nice for your part to ignore all people in the world that use languages
with more characters than the ascii set.
TC++PL does make a comment that passing a signed char into the cctype
functions can be problematic but it gives no details.
A good indication that you should not do it if without a good reason, IMO.
Passing a non 7-bit ASCII character to ispunct seems to be, at best,
implementation spicific.
But passing it as unsigned char, the result will always be the intended by
the user who knows about his locale. Passing it as signed char, maybe not.
An unnecessary risk for no other benefit than avoid to write a few chars in
the source.

--
Salu2
Jan 12 '07 #26
Julián Albo <JU********@ter ra.eswrote:
Daniel T. wrote:
I did find, "In the C locale or in a locale where character type
information is not defined, characters are classified according to the
rules of the ASCII 7-bit coded character set." (Found at
http://www.mksxserver.com/docs/man3/ispunct.3.asp,
http://www-zeuthen.desy.de/apewww/AP.../ctype_8h.html,
http://ou800doc.caldera.com/en/man/h.../ctype.3C.html et al.)

Those looks like the documentation of an implementation, non a standard
description.
Which is why I have been asking someone to look it up in the standard.
TC++PL says that implementation documentation is the authoritative
source.
But even in that case, the locale can be not 'C' and can not work as 'C' in
this regard.
Odd, the documentation I can find for setlocale says that the default
local is "C", "A minimal environment for C-language translation"
I have to admit, I know very little about the locale system and fully
expect that those who ask about using ispunct here have not called
setlocale()

Very nice for your part to ignore all people in the world that use languages
with more characters than the ascii set.
Please, no need to be combative. If one wants to handle languages with
more characters than the ascii set, one is probably better off using
some form of unicode which means all of the functions in cctype are
useless.

I've said more than once that I'm not sure of the correct answer here.
I'm asking what it is. You seem to be sure of the correct answer, but
you haven't produced any authoritative source to back your answer up.
TC++PL does make a comment that passing a signed char into the cctype
functions can be problematic but it gives no details.

A good indication that you should not do it if without a good reason, IMO.
But I'd like to understand the details. Do you know of any source that
explains them? TC++PL says, "discussion of [local conversion between
codesets] is beyond the scope of this book. Please consult your
implementation' s documentation." Well, I did that above for several
implementations . Is this something that is purely implementation defined?
Jan 12 '07 #27
Daniel T. wrote:
>Those looks like the documentation of an implementation, non a standard
description.
Which is why I have been asking someone to look it up in the standard.
TC++PL says that implementation documentation is the authoritative
source.
The standard says nothing about the working of the C-inherited functions,
other than some minor variants, and refering to the appropriate C-standard
document.
Please, no need to be combative. If one wants to handle languages with
more characters than the ascii set, one is probably better off using
some form of unicode which means all of the functions in cctype are
useless.
Probably today is a better approach, but there are still a lot of programs
that use 8-bit national charsets. And in may cases a program can be quickly
adapted with a change of locale and rewriting it with unicode support is
not an option. And even using uft-8 encoded unicode some locales can
correctly support several of the is... functions.
>A good indication that you should not do it if without a good reason,
IMO.
But I'd like to understand the details. Do you know of any source that
explains them?
I think the base point is clear: ispunct and family takes and int and
expects it contains the EOF value or the int value of an unsigned char,
passing it a plain char does not grant this expectation. You can check the
man page of some unix system, they are usually less ascii-centric than
other sources, see for example:

http://www.die.net/doc/linux/man/man3/ispunct.3.html

Or refer to the C-standard documents. I don't have it at hand.

And, as others have pointed, the fact that some documents talks about
character presents in the ascii charset, does not necessarily mean that the
ascii encoding is used.
TC++PL says, "discussion of [local conversion between
codesets] is beyond the scope of this book. Please consult your
implementation' s documentation." Well, I did that above for several
implementations . Is this something that is purely implementation defined?
I think this quote refers to more elaborated things than character
classification functions. Chapter and point?

--
Salu2
Jan 12 '07 #28
Julián Albo <JU********@ter ra.eswrote:
Daniel T. wrote:

I think the base point is clear: ispunct and family takes and int and
expects it contains the EOF value or the int value of an unsigned char,
passing it a plain char does not grant this expectation.
Unfortunately, I don't have the C standard either.
>
TC++PL says, "discussion of [local conversion between
codesets] is beyond the scope of this book. Please consult your
implementation' s documentation." Well, I did that above for several
implementations . Is this something that is purely implementation defined?

I think this quote refers to more elaborated things than character
classification functions. Chapter and point?
Section 21.7, the last paragraph.

It seems though, that you are right in that if the programer has any
reason to expect that the 8th bit will be on for any character, he
should cast to unsigned char before passing the char to the cctype
functions.
Jan 12 '07 #29
Daniel T. wrote:
TC++PL says, "discussion of [local conversion between
codesets] is beyond the scope of this book. Please consult your
implementation' s documentation." Well, I did that above for several
implementations . Is this something that is purely implementation
defined?
I think this quote refers to more elaborated things than character
classificati on functions. Chapter and point?
Section 21.7, the last paragraph.
This paragraph looks confusing to me (I have a edition in Spanish, but I
think the translation is very good, and the paragraph you quoted confirms
it). Maybe it means "consult (the C++ standard or more specialized books
and) your implementation' s documentation".

--
Salu2
Jan 12 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
10984
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working on having a "previous" record and a "next" record link on the detail page. This code below works, for the "next" record, by searching the values in the array $myarray for the variable $ref. It then returns the key value and the key value, as a...
6
2513
by: Some Clown | last post by:
Greetings, I'm trying to figure out how to loop through a vector of strings, searching each item as I go for either a boolean condition or a "contains" test. So if my vector is called 'v' I need to test v.0 for a boolean condition, then test v.1 and put the results in a new string, etc. I've tried several methods, none of which have worked. I've also been looking through my shiny "The C++ Programming Language" guide, but that's not...
7
3001
by: Jose Cuthberto | last post by:
WRAPPER CODE FOR A SET OF HTML FILES, AND BUTTON BAR LOOKS LIKE BELOW: |< < <> > >| => <= O' (magnifying glass for small gif text) I have a number of isolated html files for a 1000 page html document. The .htm files have associated gifs, anchors, using NAME and HREF attributes. The htmls are all flat, ie in the same directory but they are named like chapsec_subsec_page.htm, specifically, a1_1_page_20.htm. The gifs are in a separate...
7
2603
by: John Smith | last post by:
Why in the printout of the following code s, t, and u only go up to 9 but w and x 10? I would think they would at least go up to the same number, be it 9 or 10. The last line of the printout is: column 9 row 9 u 9 v 0 w 10 x 10 y 1 z 48652 ----------------------------------------- for(s = 0; s < 9; s++) for(t = 0; t < 9; t++)
5
2203
by: zaemin | last post by:
From a web-site that teaches optimization for c, I saw bellow optimization rule. int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++) fact *= i; return (fact);
4
2546
by: Jim Bancroft | last post by:
Hi everyone, I've been working with ASP.Net for about a month, and in all the samples I've seen the authors know ahead of time how many DropDownLists or Labels they're going to need. My problem is that I don't always know at design time how many (for instance) DropDownLists I'll need. As an example, one of my old asp pages queries a table and loops through the resulting recordset, adding one DropDownList per entry and populating it...
6
2263
by: Gonzalo Monzón | last post by:
Hi all! I have been translating some Python custom C extension code into Python, as I need these modules to be portable and run on a PocketPC without the need of compile (for the purpose its a must 2.4 as it is the last PythonCE release with great improvements). But I've been stuck with a script wich does not work as expected once translated to python, 2.4
32
4020
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
4
11948
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report> </myXml> I there a way that the XSLT transformation can render the content as html rather than text?
0
8921
marktang
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...
0
8763
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,...
0
9427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9148
tracyyun
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...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4528
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...
1
3238
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
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.