Giải pháp mạnh mẽ C89 ________ 15
With:
- Không có hành vi không xác định [như có thể có với gia đình
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;
6] - Một định nghĩa chặt chẽ hơn về số nguyên hơn
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;
5 [ví dụ: không có khoảng trắng hàng đầu cũng như thùng rác vảy] - Phân loại trường hợp lỗi [ví dụ: để cung cấp thông báo lỗi hữu ích cho người dùng]
- một "thử nghiệm"
#include
#include
#include
#include
#include
#include
typedef enum {
STR2INT_SUCCESS,
STR2INT_OVERFLOW,
STR2INT_UNDERFLOW,
STR2INT_INCONVERTIBLE
} str2int_errno;
/* Convert string s to int out.
*
* @param[out] out The converted int. Cannot be NULL.
*
* @param[in] s Input string to be converted.
*
* The format is the same as strtol,
* except that the following are inconvertible:
*
* - empty string
* - leading whitespace
* - any trailing characters that are not part of the number
*
* Cannot be NULL.
*
* @param[in] base Base to interpret string in. Same range as strtol [2 to 36].
*
* @return Indicates if the operation succeeded, or why it failed.
*/
str2int_errno str2int[int *out, char *s, int base] {
char *end;
if [s[0] == '\0' || isspace[s[0]]]
return STR2INT_INCONVERTIBLE;
errno = 0;
long l = strtol[s, &end, base];
/* Both checks are needed because INT_MAX == LONG_MAX is possible. */
if [l > INT_MAX || [errno == ERANGE && l == LONG_MAX]]
return STR2INT_OVERFLOW;
if [l < INT_MIN || [errno == ERANGE && l == LONG_MIN]]
return STR2INT_UNDERFLOW;
if [*end != '\0']
return STR2INT_INCONVERTIBLE;
*out = l;
return STR2INT_SUCCESS;
}
int main[void] {
int i;
/* Lazy to calculate this size properly. */
char s[256];
/* Simple case. */
assert[str2int[&i, "11", 10] == STR2INT_SUCCESS];
assert[i == 11];
/* Negative number . */
assert[str2int[&i, "-11", 10] == STR2INT_SUCCESS];
assert[i == -11];
/* Different base. */
assert[str2int[&i, "11", 16] == STR2INT_SUCCESS];
assert[i == 17];
/* 0 */
assert[str2int[&i, "0", 10] == STR2INT_SUCCESS];
assert[i == 0];
/* INT_MAX. */
sprintf[s, "%d", INT_MAX];
assert[str2int[&i, s, 10] == STR2INT_SUCCESS];
assert[i == INT_MAX];
/* INT_MIN. */
sprintf[s, "%d", INT_MIN];
assert[str2int[&i, s, 10] == STR2INT_SUCCESS];
assert[i == INT_MIN];
/* Leading and trailing space. */
assert[str2int[&i, " 1", 10] == STR2INT_INCONVERTIBLE];
assert[str2int[&i, "1 ", 10] == STR2INT_INCONVERTIBLE];
/* Trash characters. */
assert[str2int[&i, "a10", 10] == STR2INT_INCONVERTIBLE];
assert[str2int[&i, "10a", 10] == STR2INT_INCONVERTIBLE];
/* int overflow.
*
* `if` needed to avoid undefined behaviour
* on `INT_MAX + 1` if INT_MAX == LONG_MAX.
*/
if [INT_MAX < LONG_MAX] {
sprintf[s, "%ld", [long int]INT_MAX + 1L];
assert[str2int[&i, s, 10] == STR2INT_OVERFLOW];
}
/* int underflow */
if [LONG_MIN < INT_MIN] {
sprintf[s, "%ld", [long int]INT_MIN - 1L];
assert[str2int[&i, s, 10] == STR2INT_UNDERFLOW];
}
/* long overflow */
sprintf[s, "%ld0", LONG_MAX];
assert[str2int[&i, s, 10] == STR2INT_OVERFLOW];
/* long underflow */
sprintf[s, "%ld0", LONG_MIN];
assert[str2int[&i, s, 10] == STR2INT_UNDERFLOW];
return EXIT_SUCCESS;
}
Github ngược dòng.
Dựa trên: //stackoverflow.com/a/6154614/895245
Chuyển đổi số thành chuỗi hoặc ngược lại là một chút khó hiểu. Chúng ta có thể phải thực hiện các chuyển đổi như vậy trong khi làm việc với các số và chuỗi với nhau. Mặc dù là các hoạt động đơn giản, nhiều lập trình viên hoặc thất bại hoặc bị nhầm lẫn trong khi làm điều này. & nbsp;
Trước khi biết làm thế nào chúng ta nên chuyển đổi chuỗi thành số, nhu cầu chuyển đổi là gì: & nbsp;
Bạn sẽ gặp lỗi nếu bạn cố gắng nhập một giá trị không được chấp nhận với kiểu dữ liệu. Cả C/C ++ là một ngôn ngữ được gõ mạnh. Bạn sẽ gặp lỗi nếu bạn cố gắng nhập một giá trị không được chấp nhận với kiểu dữ liệu. Không chỉ trong các đầu vào mà bạn sẽ gặp lỗi trong khi thực hiện các hoạt động. Có một cơ hội công bằng để nhận được cả cú pháp và lỗi logic.
Ví dụ: giả sử chúng tôi có một chuỗi số là 673, mà chúng tôi muốn chuyển đổi thành một loại số. Chúng tôi sẽ cần sử dụng & nbsp; một hàm chuyển đổi một chuỗi thành số nguyên và trả về 673 làm giá trị số.
Hơn nữa, việc chuyển đổi một văn bản thành một số nguyên khó khăn hơn so với việc chuyển đổi đôi sang số nguyên bằng cách sử dụng loại đúc. Chúng tôi không được phép thực hiện đúc loại vì cả hai và chuỗi không nằm trong cùng một hệ thống phân cấp đối tượng.We are not allowed to perform type casting because int and string both are not in the same Object hierarchy.
Ví dụ: bạn không thể làm điều này vì nó sẽ gây ra lỗi
C++
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;8
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;9
int sscanf [ const char * s, const char * format, ...];0
int sscanf [ const char * s, const char * format, ...];1
int sscanf [ const char * s, const char * format, ...];2
int sscanf [ const char * s, const char * format, ...];3
int sscanf [ const char * s, const char * format, ...];4
int sscanf [ const char * s, const char * format, ...];5
int sscanf [ const char * s, const char * format, ...];6
int sscanf [ const char * s, const char * format, ...];7
int sscanf [ const char * s, const char * format, ...];4
int sscanf [ const char * s, const char * format, ...];2
The value of x : 123450
int sscanf [ const char * s, const char * format, ...];4
The value of x : 123452
int sscanf [ const char * s, const char * format, ...];222
The value of x : 123455
Lỗi sau khi biên dịch sẽ là:
Đối với chuyển đổi số thành chuỗi, hãy tham khảo bài viết - chuyển đổi số thành chuỗi trong C ++– Converting Numbers to String in C++
Chuyển đổi chuỗi thành số
Có 3 phương pháp chính để chuyển đổi một số thành một chuỗi, như sau:
- Sử dụng luồng chuỗi & nbsp;
- Sử dụng stoi []
- Sử dụng atoi [] & nbsp;
1. Sử dụng lớp Chuỗi hoặc sscanf []
StringStream []: Đây là một cách dễ dàng để chuyển đổi chuỗi các chữ số thành ints, phao hoặc đôi. Một luồng số khai báo một đối tượng luồng đầu tiên chèn một chuỗi, dưới dạng một số vào một đối tượng và sau đó sử dụng ‘StringStream [], để tuân theo chuyển đổi nội bộ. This is an easy way to convert strings of digits into ints, floats, or doubles. A number stream declares a stream object which first inserts a string, as a number into an object, and then uses ‘stringstream[]’ to follow the internal conversion.
Để sử dụng nó, trước tiên, & nbsp; thêm dòng #include vào đầu chương trình của bạn & nbsp; để bao gồm thư viện SStream.
Sau đó, chuỗi chuỗi được thêm vào và một đối tượng chuỗi được tạo, sẽ giữ lại giá trị của chuỗi bạn muốn chuyển đổi thành int và sẽ được sử dụng & nbsp; trong quá trình chuyển đổi.
Để trích xuất chuỗi từ biến chuỗi, & nbsp; sử dụng toán tử >.
Example:
CPP
The value of x : 123456
The value of x : 123457
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;9
int sscanf [ const char * s, const char * format, ...];0
int sscanf [ const char * s, const char * format, ...];1
int sscanf [ const char * s, const char * format, ...];2
int sscanf [ const char * s, const char * format, ...];3
The value of x : 12345.5400393
The value of x : 12345.5400394
The value of x : 12345.5400395
The value of x : 12345.5400396
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
The value of x : 12345.5400399
int sscanf [ const char * s, const char * format, ...];4
int sscanf [ const char * s, const char * format, ...];2
The value of x : 123450
The value of x : 12345.5400394
The value of x : 12345.540039 // output of floating number4
int sscanf [ const char * s, const char * format, ...];4
The value of x : 123452
int sscanf [ const char * s, const char * format, ...];222
Lỗi sau khi biên dịch sẽ là:
The value of x : 123455
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;
HELLP.CPP: 9: 10: Lỗi: Không chuyển đổi phù hợp cho Cast kiểu C từ 'std :: __ 1 :: String' [AKA & nbsp; & nbsp; 'basic_string'] sang 'int' & nbsp; num = [int] str; & nbsp; & nbsp; & nbsp; & nbsp; toán tử __Self_view [] const _noExcept {return __elf_view [data [], size []]; } & nbsp; ^1 lỗi được tạo ra.sscanf[]‘ is a C style function similar to scanf[]. It reads input from a string rather than standard input.
Đối với chuyển đổi số thành chuỗi, hãy tham khảo bài viết - chuyển đổi số thành chuỗi trong C ++
int sscanf [ const char * s, const char * format, ...];
Chuyển đổi chuỗi thành số: Integer
Parameters::
- Có 3 phương pháp chính để chuyển đổi một số thành một chuỗi, như sau:– string used to retrieve data
- Sử dụng luồng chuỗi & nbsp;– a string that contains the type specifier[s]…
- Sử dụng stoi []– arguments contain pointers to allocate storage with the appropriate type.
Sử dụng atoi [] & nbsp;
1. Sử dụng lớp Chuỗi hoặc sscanf []
C++
The value of x : 123456
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;9
int sscanf [ const char * s, const char * format, ...];0
int sscanf [ const char * s, const char * format, ...];1
int sscanf [ const char * s, const char * format, ...];2
int sscanf [ const char * s, const char * format, ...];3
The value of x : 12345.5400393
int sscanf [ const char * s, const char * format, ...];4
int sscanf [ const char * s, const char * format, ...];2
The value of x : 123450
int sscanf [ const char * s, const char * format, ...];4
The value of x : 123452
int sscanf [ const char * s, const char * format, ...];222
The value of x : 12345.5400394
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313370
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313371
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313372
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313373
Lỗi sau khi biên dịch sẽ là:
Lỗi sau khi biên dịch sẽ là:
The value of x : 123455
HELLP.CPP: 9: 10: Lỗi: Không chuyển đổi phù hợp cho Cast kiểu C từ 'std :: __ 1 :: String' [AKA & nbsp; & nbsp; 'basic_string'] sang 'int' & nbsp; num = [int] str; & nbsp; & nbsp; & nbsp; & nbsp; toán tử __Self_view [] const _noExcept {return __elf_view [data [], size []]; } & nbsp; ^1 lỗi được tạo ra.
Đối với chuyển đổi số thành chuỗi, hãy tham khảo bài viết - chuyển đổi số thành chuỗi trong C ++
int sscanf [ const char * s, const char * format, ...];2
int sscanf [ const char * s, const char * format, ...];3
The value of x : 12345.5400393
int sscanf [ const char * s, const char * format, ...];4
int sscanf [ const char * s, const char * format, ...];2
The value of x : 123450
int sscanf [ const char * s, const char * format, ...];4
The value of x : 123452
int sscanf [ const char * s, const char * format, ...];222
The value of x : 12345.5400394
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313370
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313371
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313372
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313373
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;11
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;12
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;13
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;14
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
The value of x : 123455
Đầu ra
The value of x : 12345
Tương tự, chúng ta có thể đọc float và gấp đôi sử dụng %F và %LF tương ứng.%f and %lf respectively.
C
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313372
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.5400393
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String1
| | |___> integer | |_______> to |__________> String2
| | |___> integer | |_______> to |__________> String3
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;27
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;30
| | |___> integer | |_______> to |__________> String8
The value of x : 12345.5400394
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313370
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313371
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;35
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313373
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;11
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;12
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;40
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;14
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
The value of x : 123455
Đầu ra
The value of x : 12345.540039
Tương tự, chúng ta có thể đọc float và gấp đôi sử dụng %F và %LF tương ứng.
The value of x : 12345.540039 // output of floating number
C
The value of x : 12345.540000 // output of double number
stoi["45"] is 45
stoi["3.14159"] is 3
stoi["31337 geek"] is 31337
2
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.540039
4| | |___> integer
| |_______> to
|__________> String
1 | | |___> integer
| |_______> to
|__________> String
2| | |___> integer
| |_______> to
|__________> String
3// A stringstream is similar to input/output
// file stream. We need to declare a stringstream
// just like an fstream, for example:
stringstream ss;
// and, like an fstream or cout,
// we can write to it:
ss > myCstring; or
ss >> myInt;
27int sscanf [ const char * s, const char * format, ...];
7
The value of x : 12345.540039
4// A stringstream is similar to input/output
// file stream. We need to declare a stringstream
// just like an fstream, for example:
stringstream ss;
// and, like an fstream or cout,
// we can write to it:
ss > myCstring; or
ss >> myInt;
30 | | |___> integer
| |_______> to
|__________> String
8 The stoi[] function takes a string as an argument and returns its value in integer form. This approach is popular in current versions of C++, and it was first introduced in C++11.And if you observe stoi[] a little closer you will find out that itstands for:
Đầu ra 1:
| | |___> integer | |_______> to |__________> String
Example:
Đầu ra 2:
The value of x : 123456
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;8
Nếu các giá trị giống nhau thì tại sao các đầu ra lại khác nhau?
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.5400393
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;55
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;56
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;59
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;60
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;63
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;64
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String1
| | |___> integer | |_______> to |__________> String2
| | |___> integer | |_______> to |__________> String3
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;27
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;30
| | |___> integer | |_______> to |__________> String8
Đầu ra 1:
Đầu ra 2:
Nếu các giá trị giống nhau thì tại sao các đầu ra lại khác nhau?
Lý do đằng sau điều này là các số nổi là tất cả về tốc độ, độ chính xác và sự thuận tiện ngoài việc chúng sử dụng biểu diễn nhị phân để hiển thị đầu ra của chúng phần nào đưa đầu ra đến gần đúng nhất; Đó là lý do tại sao có những chữ số phụ ở cuối đầu ra. Ngoài ra, với số lượng gấp đôi, giá trị sẽ được hiển thị bởi vì gấp đôi là tất cả về độ chính xác và độ tin cậy, mặc dù nó tiêu thụ nhiều không gian hơn phao và cũng chậm hơn một chút so với số nổi. & NBSP;
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;81
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;82
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;83
int sscanf [ const char * s, const char * format, ...];7
2. Chuyển đổi chuỗi bằng stoi []
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;81
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;82
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;83
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
The value of x : 123455
Đầu ra
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 31337
Output::
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 31337
Tương tự, chúng ta có thể đọc float và gấp đôi sử dụng %F và %LF tương ứng.
Catoi[] a little closer you will find out that it stands for:
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;0
Example:
C++14
int sscanf [ const char * s, const char * format, ...];09
The value of x : 123456
Nếu các giá trị giống nhau thì tại sao các đầu ra lại khác nhau?
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.5400393
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String1
| | |___> integer | |_______> to |__________> String2
| | |___> integer | |_______> to |__________> String3
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;27
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;30
| | |___> integer | |_______> to |__________> String8
Đầu ra 1:
Đầu ra 2:
Nếu các giá trị giống nhau thì tại sao các đầu ra lại khác nhau?
Lý do đằng sau điều này là các số nổi là tất cả về tốc độ, độ chính xác và sự thuận tiện ngoài việc chúng sử dụng biểu diễn nhị phân để hiển thị đầu ra của chúng phần nào đưa đầu ra đến gần đúng nhất; Đó là lý do tại sao có những chữ số phụ ở cuối đầu ra. Ngoài ra, với số lượng gấp đôi, giá trị sẽ được hiển thị bởi vì gấp đôi là tất cả về độ chính xác và độ tin cậy, mặc dù nó tiêu thụ nhiều không gian hơn phao và cũng chậm hơn một chút so với số nổi. & NBSP;
2. Chuyển đổi chuỗi bằng stoi []
stoi []: hàm stoi [] lấy một chuỗi làm đối số và trả về giá trị của nó ở dạng số nguyên. Cách tiếp cận này là phổ biến trong các phiên bản hiện tại của C ++ và lần đầu tiên nó được giới thiệu trong C ++ 11. Và nếu bạn quan sát stoi [] gần hơn một chút, bạn sẽ phát hiện ra rằng điều đó cho:
& nbsp; & nbsp; s & nbsp; & nbsp; đến & nbsp; & nbsp; tôi[]
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
The value of x : 123455
C
stoi["45"] is 45 stoi["3.14159"] is 3 stoi["31337 geek"] is 313372
int sscanf [ const char * s, const char * format, ...];79
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.5400393
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String2
int sscanf [ const char * s, const char * format, ...];20
int sscanf [ const char * s, const char * format, ...];21
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String2
int sscanf [ const char * s, const char * format, ...];26
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;60
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String2
int sscanf [ const char * s, const char * format, ...];32
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;64
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String1
| | |___> integer | |_______> to |__________> String2
| | |___> integer | |_______> to |__________> String3
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;27
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;30
| | |___> integer | |_______> to |__________> String8
Đầu ra 1:
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;11
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;12
The value of x : 1234516
The value of x : 1234517
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;11
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;12
The value of x : 1234516
The value of x : 1234522
The value of x : 12345.5400394
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;11
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;12
The value of x : 1234516
The value of x : 1234527
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
Đầu ra 2:
Đầu ra
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;1
Tương tự, chúng ta có thể đọc float và gấp đôi sử dụng %F và %LF tương ứng.
Catoi[] is a legacy C-style function. stoi[] is added in C++ 11.
stoi["45"] is 45
stoi["3.14159"] is 3
stoi["31337 geek"] is 31337
2 atoi[] works only for C-style strings [character array and string literal], stoi[] works for both C++ strings and C style strings
int sscanf [ const char * s, const char * format, ...];
2 The value of x : 12345.540039
2 atoi[] takes only one parameter and returns integer value.
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;2
The value of x : 12345.540039
4| | |___> integer
| |_______> to
|__________> String
1 | | |___> integer
| |_______> to
|__________> String
2| | |___> integer
| |_______> to
|__________> String
3// A stringstream is similar to input/output
// file stream. We need to declare a stringstream
// just like an fstream, for example:
stringstream ss;
// and, like an fstream or cout,
// we can write to it:
ss > myCstring; or
ss >> myInt;
27int sscanf [ const char * s, const char * format, ...];
7 stoi[] can take up to
three parameters, the second parameter is for starting index and the third parameter is for the base of the input number.
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;3
The value of x : 12345.540039
4// A stringstream is similar to input/output
// file stream. We need to declare a stringstream
// just like an fstream, for example:
stringstream ss;
// and, like an fstream or cout,
// we can write to it:
ss > myCstring; or
ss >> myInt;
30 | | |___> integer
| |_______> to
|__________> String
8: Write your own atof[] that takes a string [which represents a floating-point value] as an argument and returns its value as double.
C++
int sscanf [ const char * s, const char * format, ...];09
The value of x : 123456
Nếu các giá trị giống nhau thì tại sao các đầu ra lại khác nhau?
int sscanf [ const char * s, const char * format, ...];2
The value of x : 12345.5400392
The value of x : 12345.5400393
The value of x : 12345.5400394
| | |___> integer | |_______> to |__________> String1
| | |___> integer | |_______> to |__________> String2
| | |___> integer | |_______> to |__________> String3
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;27
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
The value of x : 1234547
int sscanf [ const char * s, const char * format, ...];37
The value of x : 1234549
int sscanf [ const char * s, const char * format, ...];39
The value of x : 12345.5400394
The value of x : 12345.540039 // output of floating number6
The value of x : 1234553
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;78
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;79
int sscanf [ const char * s, const char * format, ...];55
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;83
int sscanf [ const char * s, const char * format, ...];7
The value of x : 12345.5400394
The value of x : 12345.540000 // output of double number0
The value of x : 12345.540000 // output of double number1
The value of x : 123455
Đầu ra
// A stringstream is similar to input/output // file stream. We need to declare a stringstream // just like an fstream, for example: stringstream ss; // and, like an fstream or cout, // we can write to it: ss > myCstring; or ss >> myInt;4
4. Sử dụng cho vòng lặp để chuyển đổi chuỗi thành số
C++
The value of x : 1234563
The value of x : 1234564
int sscanf [ const char * s, const char * format, ...];2
The value of x : 1234566
The value of x : 12345.5400394
int sscanf [ const char * s, const char * format, ...];2
The value of x : 1234569
The value of x : 12345.5400394
The value of x : 1234571
The value of x : 1234572
The value of x : 1234573
The value of x : 1234574
The value of x : 1234575
The value of x : 123455
Bài viết này được đóng góp bởi Siffi Singh. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết trên write.geeksfeeks.org. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc nếu bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.Siffi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article on write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.