Basic C++ String Question
-
hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.
if (chr[i] == 'u'){
count++;
}
else{
cout<<chr[i];
}
cout<<"\nIt has "<<strlen(chr) - count<<" characters.";Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.
-
hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.
if (chr[i] == 'u'){
count++;
}
else{
cout<<chr[i];
}
cout<<"\nIt has "<<strlen(chr) - count<<" characters.";Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.
Yes:
if(chr[i] != 'u' || !++count) {
cout << chr[i];
}Whether you should do this or not is another question - it's a lot harder to understand.
-
Yes:
if(chr[i] != 'u' || !++count) {
cout << chr[i];
}Whether you should do this or not is another question - it's a lot harder to understand.
Ummmm, definitely not advisable: 1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes! 2. Like you said, hardly anyone will understand, and even those that do will doubt what it does was actially intended, and might decide to 'fix' it wrongly. Apart from that - interesting thought; I was about to answer 'no' ;)
-
hello guys... I have this piece of code. Lets say my name contains 'u' one time, my code will reduce the string size by 1, exludes the 'u' from string and displays it on screen.
if (chr[i] == 'u'){
count++;
}
else{
cout<<chr[i];
}
cout<<"\nIt has "<<strlen(chr) - count<<" characters.";Now I am using if - else here...can these two tasks be done in just if ? Like incrementing count and displaying name in if without using else.
I think there's some piece of information missing: Your code doesn't show a loop, or any other construct that loops through the characters in
chr
, and there's no point in displayingcount
before the end of that loop. Then in your request you suggest displaying 'name' within yourif
block, but there's no variable name, and if this should refer tochr
, there is no point in displaying it fully for every value ofi
that you use in that if statement. Unless your stringchr
will always be exactly "u" (i. e. a string of 1 character, which is 'u'), your question, taken literally, doesn't make much sense. Besides, why do you want to get rid of thatelse
statement? -
Ummmm, definitely not advisable: 1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes! 2. Like you said, hardly anyone will understand, and even those that do will doubt what it does was actially intended, and might decide to 'fix' it wrongly. Apart from that - interesting thought; I was about to answer 'no' ;)
Stefan63 wrote:
1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes!
Erm... hold on a sec here... Short-circuiting IS mandated, and can only work if it's evaluated in order. It's always true for PODs. The only caveat is, if it is a class, and that class has an overloaded && or || operator. Then, both sides will be evaluated, so that the compiler can pass the element on the right hand side as a parameter. One (very) good reason to never, ever overload them. At least, this is the way I understand it, and I can't imagine anything undoing that, as tons of code that did this:
if( pszString != NULL && *pszString != '\0' )
would randomly crash if *pszString was evaluated first. Course, in this example the actual comparisons can be removed, but I was trying to be explicit. N'est pas? -- CraigL
-
Stefan63 wrote:
1. Whether this works or not is compiler dependend - a compiler is free to evaluate the left or right side of an || operator in whatever order it likes!
Erm... hold on a sec here... Short-circuiting IS mandated, and can only work if it's evaluated in order. It's always true for PODs. The only caveat is, if it is a class, and that class has an overloaded && or || operator. Then, both sides will be evaluated, so that the compiler can pass the element on the right hand side as a parameter. One (very) good reason to never, ever overload them. At least, this is the way I understand it, and I can't imagine anything undoing that, as tons of code that did this:
if( pszString != NULL && *pszString != '\0' )
would randomly crash if *pszString was evaluated first. Course, in this example the actual comparisons can be removed, but I was trying to be explicit. N'est pas? -- CraigL
I've learned not to rely on evaluation order, but I just looked it up and found my argument would only be true in cases where both argument sneed to be evaluated in any case, such as in an addition. In fact, Stroustrup explicitely names the exception, and that exception is exactly what you said: "The operators ' (comma), && (logical and), and || (logical or) guarantee that their left-hand operand is evaluated before their right-hand operand." (Bjarne Stroustrup, The C++ Programming Language, Third edition, page 123) So I stand corrected. Thanks for pointing this out.