Hello Daved, how do I assign a C style string to the C++ string?
Hello Daved, how do I assign a C style string to the C++ string?
Perhaps a little more accurately:
Otherwise your compiler might complain that x is an integer but it is being compared with an unsigned integer. size() also has constant complexity, so the above is just an example of avoiding multiple function calls.Code:for (std::string::size_type x = 0, size = stdString.size(); x < size; ++x)
There are a number of suitable constructors:how do I assign a C style string to the C++ string?
1. The constructor that takes a null terminated C style string, or
2. The constructor that takes a null terminated C style string and a size, or
2. The constructor that takes an iterator pair (to the beginning and one past the end of the C style string).
Last edited by laserlight; 02-20-2008 at 10:13 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for all the help.
As laserlight said, ++it is no slower and might be faster, yet is equally readable. It is an extremely minor issue, but there's no logical reason your taste should be for it++ over ++it.
>> I don't know about that.
There should be plenty of explanations on why it might be faster available on the web. If you have Meyers' More Effective C++ (Item 6) or Sutter and Alexandrescu's C++ Coding Standards (Item 28) you can read explanations there.
Note that we're referring to iterators here. Built-in types are likely never going to show a difference, so in that case perhaps it doesn't lack sense to prefer the look of post-increment. However, for consistency alone I don't see why you would.
Last edited by Daved; 02-20-2008 at 01:11 PM.
Yes, pre increment returns a reference and the post returns a new object, as per the rules on overloading.
But as for ++n or n++, well... unless you are writing time critical code, it hardly matters. I don't know what the best thing to do is... teach them it might be faster and say should try to use it as a good practice or saying it hardly matters anyway since the speed will be about equal.
But, as some others are, I'm reluctant as to teaching what someone should use. They should use what they feel is best. Though I'm not going to argue the point.
It needs to be noted though that the postfix operator does more work. The prefix operator returns an lvalue (it increments the object and returns the object itself) whereas the postfix returns a rvalue. The compiler needs to store the original value. For built-in types, pointers and simple user-based objects capable of iteration this should probably be optimized for the compiler. But for more complex objects, this storage may come at a higher cost.
The prefix operator should be preferred instead and the postfix operator used only when there is a need.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>> But, as some others are, I'm reluctant as to teaching what someone should use. They should use what they feel is best.
That makes no sense. One is clearly better. Why are you making an issue of someone suggesting that one should prefer the better choice?
Of course it is perfectly acceptable to indicate that the difference is minor if it exists at all. The two statements are not mutually exclusive. Laserlight and I both indicated this point as well.
However, you are insinuating that there isn't a preferred choice. There clearly is. Your comment only serves to confuse the issue, which is even worse in this case because the issue is off-topic.
No, my point was that people should not be "taught" to only use ++n unless n++ was necessary; the point was it's a choice and one shouldn't advise people to use one over another.
Now performance issues with ++n and n++ is an entirely different case, but I don't want to confuse on the above matter.
Anyway, I digress.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
In a sense I agree that if that was the only point of the original post, then it would be as pointless as posts that say nothing beyond void main() or indentation (I mean, if that's all one's got to say, may-be it would be better to ignore the thread).No, my point was that people should not be "taught" to only use ++n unless n++ was necessary; the point was it's a choice and one shouldn't advise people to use one over another.
However, in this case it was pointed out as a side note and indeed one option is preferable to other, so it's something that's good to know and I don't see your point. I started out as a "post-increment user" myself, but after reading a similar comment, I simply changed my habit.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Actually, I think it was my mistake.
Originally, when I read through, I was thinking that laserlight was encouraging the ++n syntax before n++ (in all cases), but now I realize laserlight was proposing the use of ++n due to the variable being an iterator.
Of course, it might be preferable to use ++n with objects and iterators.
This was all my misunderstanding. Sorry about the mess.
Habit over evaluation. By simply always using prefix increment, you avoid having to think about whether postfix incurs a performance penalty in this case. It also makes your code look more consistent: if one for-loop uses postfix and the next prefix, that's ugly.
That's the rational and objective argument speaking for always using prefix increment. (Except, of course, the places where the semantics really require postfix.) There is no such argument for preferring postfix, only a subjective "matter of taste".
Thus, always using prefix is clearly the better solution.
Hmm ... is there a way to unquote something?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law