Conversion between C++ int and string (including source code implementation)

1, int to string I. to string function The standard of c+...

1, int to string

I. to string function

The standard of c++11 adds the global function STD:: to string:

string to_string (int val);

string to_string (long val);

string to_string (long long val);

string to_string (unsigned val);

string to_string (unsigned long val);

string to_string (unsigned long long val);

string to_string (float val);

string to_string (double val);

string to_string (long double val);

Example:

1 // to_string example 2 #include <iostream> // std::cout 3 #include <string> // std::string, std::to_string 4 5 int main () 6 { 7 std::string pi = "pi is " + std::to_string(3.1415926); 8 std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number"; 9 std::cout << pi << '\n'; 10 std::cout << perfect << '\n'; 11 return 0; 12 } 13 Output 14 pi is 3.141593 15 28 is a perfect number

Appendix: the implementation of to_string() function

1 //Realization to_string function 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 #define max 100 6 string to_String(int n) 7 { 8 int m = n; 9 char s[max]; 10 char ss[max]; 11 int i=0,j=0; 12 if (n < 0)// Handling negative numbers 13 { 14 m = 0 - m; 15 j = 1; 16 ss[0] = '-'; 17 } 18 while (m>0) 19 { 20 s[i++] = m % 10 + '0'; 21 m /= 10; 22 } 23 s[i] = '\0'; 24 i = i - 1; 25 while (i >= 0) 26 { 27 ss[j++] = s[i--]; 28 } 29 ss[j] = '\0'; 30 return ss; 31 } 32 33 int main() 34 { 35 cout << "please enter an integer:"; 36 int m; 37 cin >> m; 38 string s = to_String(m) + "abc"; 39 cout << s << endl; 40 system("pause"); 41 return 0; 42 }

II. With the help of character stream

The standard library defines three types of character stream: istringstream, ostringstream, and stringstream. It can be seen from the name that these types are very similar to iostream. They can read, write, and read and write string types respectively. They are indeed derived from the iostream type. To use them, you need to include the sstream header file.

Except for operations inherited from iostream

1. S s stream type defines a constructor with string parameter, that is: stringstream (s); creates a stringstream object to store the copy of S, and S is a string type object

2. A member named str is defined to read or set the string value manipulated by the string stream object: stream.str(); return the string type object stored in stream stream stream.str(s); copy the s of string type to stream and return void

Example:

1 int aa = 30; 2 stringstream ss; 3 ss<<aa; 4 string s1 = ss.str(); 5 cout<<s1<<endl; // 30

2, string to int

I. using the atoi function in the standard library, there are corresponding standard library functions for other types, such as floating-point atof(),long atol(), etc

Example:

1 std::string str = "123"; 2 int n = atoi(str.c_str()); 3 cout<<n; //123

Appendix: source code implementation of atoi() function

The main function of this function is to convert a string into a number. At first glance, you may think it is a very simple function, or even a function that can be implemented without many lines of code. In fact, this is a simple look, but in practice, there are still some things to pay attention to. In general, there are five situations:

1 -- NULL pointer
2 -- empty character processing
3 -- processing of positive sign and negative sign
4 -- overflow handling
5 -- how to deal with abnormal characters



The code is attached below:

#include<iostream> enum ret { kvalid=0,kinvalid }; // Whether there is illegal input mark int status = kvalid; long long Strtointcode(const char* digit, bool minus) { long long num = 0; while (*digit != '\0') { if (*digit >= '0'&&*digit <= '9') { int flag = minus ? -1 : 1; num = num * 10 + flag*(*digit - '0'); if ((!minus&&num > 0x7FFFFFFF) ||( minus&&num < (signed int)0x80000000)) { num = 0; break; } digit++; } else { num = 0; break; } } if (*digit == '\0') status = kvalid; return num; } int Strtoint(const char* str) { status = kinvalid; long long num = 0; if (str != NULL&&*str != '\0') { bool minus = false; if (*str == '+') str++; else if (*str == '-') { str++; minus = true; } if (*str != '\0') num = Strtointcode(str, minus); } return (int)num; } int main() { char arr[20]; int ret = 0; printf("Please enter the string you want to convert:\n"); scanf("%s", arr); ret = Strtoint(arr); if (kvalid == status) { printf("%d\n", ret); } else { printf("Illegal input\n"); printf("%d\n", ret); } system("pause"); return 0; }

II. Using character stream object defined in sstream header file to realize conversion

1 istringstream is("12"); //Construct the input character stream, the content of which is initialized to a string of "12" 2 int i; 3 is >> i; //from is Read in a int Integer deposit i in

reference material:

http://blog.csdn.net/loving_forever_/article/details/51285703

http://blog.csdn.net/lxj434368832/article/details/78874108

https://www.cnblogs.com/aminxu/p/4704281.html

4 May 2020, 22:26 | Views: 9918

Add new comment

For adding a comment, please log in
or create account

0 comments