Conversion of numbers to strings in C + + scanf string Summary (review required)

Source: Internet
Author: User
Tags first string sprintf string to number uppercase letter

1 scanf read-in operation of string

C + + inside the console input directly using the CIN operation can be, or getline (istringstream,string);.

Only built-in type Int,float,char,double,bool can be assigned directly, scanf read into string cannot be directly assigned using SCANF, because string is a class, has a special initialization function, cannot use scanf, It is also a char pointer that is received by the same token. The same is true of sizeof, a programming language, that cannot be manipulated directly on a string.

string s;s.resize (+); scanf ("%s", &s[0]);

Or:

string s; Char New Char (scanf); ("%s", c); // scanf ("%s", &c[0]); s = c;//to the first address

Strlen is a valid string to get input, instead of opening the space size to 100.

What Strlen does is work on a counter that starts scanning from somewhere in memory (which can be the beginning of a string, somewhere in the middle, or even an indeterminate area of memory) until it touches the first string Terminator , and then returns the counter value. --means the actual length of the actual string or array of characters (not the number of bytes in the occupied space).

 char a[6]={' a ', ' B ', ' n ', ' d ', ' e ', ' R '};  
int I=strlen (A);           //i for 2, because to 0 ' End, so the actual a[] only sizeof ()
 string   S;  char  *c = new  char  (100    " %s   " ,c);  int  len = strlen (c); S1.resize (len);// Dynamically compiled S1.assign (c,c  + len); // copy (C,c + len,s1.begin ());     
 class x  

int i; 
int j; 
char k; 
}; 
x x; 
< Span lang= "en-US" >cout<<sizeof (X) <<endl;  results  12 = = = Memory  
cout<<sizeof (x) <<endl;  results  12  ibid.  
class X, the largest space occupied by the members is int type of space 4 bytes, so memory is filled, the final result is ((int) (actual member occupied memory space and /4) +1) * *

* So you cannot get the actual number of char elements by sizeof (string)/sizeof (string[0]), regardless of the number of strings, the final result is 28, because string is a class.

Copy () Usage:

This algorithm receives three iterator iterator, the first two representing an input range, and the third represents the starting position of the destination sequence.

Conversions of Char and int:

int = char-' 0 ';

char = int + ' 0 ';

2 conversion between numbers and strings in C + + (important)

1. Conversion between string numbers

(1) string---char *
String str ("OK");
Const char * p = str.c_str ();

(2) char *-->string
Char *p = "OK";
String str (p);
(3) String->double
Double D=atof (S.c_str ());

Common functions Atoi (), itoa (), to_string ();


2. Numeric to String: Use sprintf () function

Char str[10];
int a=1234321;
sprintf (str, "%d", a);
--------------------
Char str[10];
Double a=123.321;
sprintf (str, "%.3LF", a);
--------------------
Char str[10];
int a=175;
sprintf (str, "%x", a),//10 conversion into 16, if the output uppercase letter is sprintf (str, "%x", a)
--------------------
char *itoa (int value, char* string, int radix);
It is also possible to convert a number to a string, but itoa () is a platform-dependent (not standard) function, so it is not recommended here.

3. String to number: using the sscanf () function

Char str[]= "1234321";
int A;
SSCANF (str, "%d", &a);
.............
Char str[]= "123.321";
Double A;
SSCANF (str, "%LF", &a);
.............
Char str[]= "AF";
int A;
SSCANF (str, "%x", &a); 16 binary converted to 10 binary

Alternatively, you can use Atoi (), Atol (), Atof ().

4. Using StringStream class

Write a string with the Ostringstream object, similar to sprintf ()
Ostringstream S1;
int i = 22;
S1 << "Hello" << i << Endl;
String s2 = S1.str ();
cout << S2;

Reads a string with the Istringstream object, similar to SSCANF ()
Istringstream stream1;
String string1 = "25";
STREAM1.STR (string1);
int i;
Stream1 >> i;
cout << i << Endl; Displays 25

3 Summary of all function operations of the string class

append ()   &nbs P;  
       
       
       
       
       
       
       
       
       

Conversion of numbers to strings in C + + scanf string Summary (review required)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.