# 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. ## 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. 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.

3. 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. 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.

5. 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.

6. 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

7. Originally Posted by hqt
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.

8. 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. Originally Posted by hqt
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.

10. 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. Originally Posted by hqt
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.

12. Originally Posted by AndrewHunter
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...

13. 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

14. Originally Posted by laserlight
boost::lexical_cast is broken
The C++ standard is broken, so that's not a surprise :P

15. Originally Posted by Elysia
Naw, stringstreams are broken
Originally Posted by laserlight
boost::lexical_cast is broken
Err... Why ?

Page 1 of 2 12 Last