Like Tree4Likes

Purpose of this code

This is a discussion on Purpose of this code within the C++ Programming forums, part of the General Programming Boards category; I have this below code, that I have read: Code: int solve(vector<string> digits){ string tmp=""; //do something with tmp vector<int> ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Purpose of this code

    I have this below code, that I have read:
    Code:
    int solve(vector<string> digits){
       string tmp="";
       //do something with tmp
       vector<int> D; for(unsigned i=0;i<tmp.size();++i) D.push_back(tmp[i]-'0');
    }
    look at line:
    Code:
    D.push_back(tmp[i]-'0');
    I don't know, why tmp[i]-'0', not tmp[i]. Many people do this. But I don't know, why?
    So, who have experience, please give me.

    thanks

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    tmp[i] is a character.
    To get the number corresponding to its ascii value, you need to subtract the ascii value of 0 from it.
    AndrewHunter and hqt like this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    I don't think so. for example: 'a' have decimal ASCII code is 97. '0' have ASCII code is 48.
    So:
    Code:
        int i='a';
        int j='a'-'0';
        cout<<i<<endl;
        cout<<j<<endl;
    You will received 97 and 49, respectively.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Assuming x is a digit ('0', '1', '2', ...., '9') then x - '0' yields the corresponding value 0, 1, 2, .... , 9.

    The result is implementation defined (and probably meaningless in most programs, unless you assume particular character sets) if used on characters that are not in the set of digits.
    hqt likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You are missing the point my friend. That code takes a char number and turns it into its integer value. So for all char values 0-9, if your subtract char 0 you will get the associated integer value.

    Before you correct someone ensure you are in fact correct. It is not our fault that you do not understand the code that you have posted but do not accuse us of not understanding it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, sorry for all. Now, I know the purpose of this code. I will take character number to number. for example:
    Code:
       char i='5';
       char j='8';
       int num1=i-'0';
       int num2=j-'0';
       cout<<num1<<endl;
       cout<<num2<<endl;
    You will received 5 and 8,respectively.
    I think, this code will nicer than stringstream, but poorly, It just do for character


    <Edit>
    ah, after this post, I see 3 posts post before me. (maybe because I typed so slowly). But, I think my post is more detail than others, so It will useful for somebody else, so I will not delete it ?

    To get the number corresponding to its ascii value, you need to subtract the ascii value of 0 from it.
    Oh, before that, I don't know purpose of this code, I don't know what you say :"> (at that time, I thought you say to get ASCII code :">). And, after I understand purpose of this code, your answer's very clearly thanks for your useful help



    thanks
    Last edited by hqt; 09-24-2011 at 11:15 PM.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    I think, this code will nicer than stringstream, but poorly, It just do for character

    thanks
    Exactly, hence string stream is the correct answer for just about every solution.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    I don't know algorithm of stringstream when I convert string to integer. Does it use algorithm like character-'0'. Do you know it?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    I don't know algorithm of stringstream when I convert string to integer. Does it use algorithm like character-'0'. Do you know it?
    What are your trying to do this time? We already covered stringstream with you on another thread that you started.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Yes, I know. I know how to use stringstream But, I don't know, HOW It work. (like many people use quick sort, but cannot prove, why It's fast!). And, I ask just for learning purpose

    @: Oh, I don't know, why you remember I have posted about stringstream before

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by hqt View Post
    Yes, I know. I know how to use stringstream But, I don't know, HOW It work. (like many people use quick sort, but cannot prove, why It's fast!). And, I ask just for learning purpose

    @: Oh, I don't know, why you remember I have posted about stringstream before
    You only need to know how to apply stringstreams here, not the ins and outs of its implementation.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by AndrewHunter View Post
    Exactly, hence string stream is the correct answer for just about every solution.
    Naw, stringstreams are broken.
    boost is the correct way:
    int n = boost::lexical_cast<int>(whatever_you_want_here_be _it_a_char_string_or_a_custom_type);

    EDIT: Interesting enough, the board split my incredibly long variable name? I can clearly see a space there...
    Last edited by Elysia; 09-25-2011 at 09:59 AM.
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by Elysia
    Naw, stringstreams are broken.
    boost is the correct way:
    int n = boost::lexical_cast<int>(whatever_you_want_here_be _it_a_char_string_or_a_custom_type);
    boost::lexical_cast is broken
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by laserlight View Post
    boost::lexical_cast is broken
    The C++ standard is broken, so that's not a surprise :P
    laserlight likes this.
    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.

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by Elysia
    Naw, stringstreams are broken
    Quote Originally Posted by laserlight
    boost::lexical_cast is broken
    Err... Why ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the purpose of this whole code..
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 04-02-2009, 01:32 PM
  2. What is the purpose of C?
    By Davez69gto in forum C Programming
    Replies: 9
    Last Post: 01-27-2009, 01:09 PM
  3. My purpose, what do I do?
    By jaylc185 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2005, 11:15 AM
  4. What is the purpose of a DLL?
    By Bluesun159 in forum C++ Programming
    Replies: 6
    Last Post: 07-12-2003, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21