Thread: cstrings and strings

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Hello Daved, how do I assign a C style string to the C++ string?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps a little more accurately:
    Code:
    for (std::string::size_type x = 0, size = stdString.size(); x < size; ++x)
    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.

    how do I assign a C style string to the C++ string?
    There are a number of suitable constructors:
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dragongunner View Post
    Hello Daved, how do I assign a C style string to the C++ string?
    With a simple =
    Code:
    std::string hello = "Hello";
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Thanks for all the help.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Incidentally, it would be better to prefer ++it to it++ since the former is no slower than the latter and might be faster, with equal readability.
    I don't know about that. If to prefer to avoid mistakes or such, then mayhap. Generally, I don't like ++n, I always use n++ unless I need ++n, so it's a taste thing, I think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    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' Effective C++ or Sutter and Alexandrescu's C++ Coding Standards you can read explanations there.
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    I don't know about that. If to prefer to avoid mistakes or such, then mayhap. Generally, I don't like ++n, I always use n++ unless I need ++n, so it's a taste thing, I think.
    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.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    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.
    Well, one should when the issue is introduced for some reason. And what one should advise is prefix over suffix for a reasons of consistency, since on large or complex objects postfix increment or decrement has the potential to be costly.
    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.

  12. #27
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    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).

    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Last edited by Elysia; 02-20-2008 at 03:26 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating C strings
    By black_spot1984 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 10:23 AM
  2. Concatenating cstrings
    By trev456 in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 12:26 AM