Similarities:
The C++ character processing function, the numeric string into an int output
Header files are #include< cstring>
Difference:
Atoi () is const char*, so we must call c_str() to const char* for a string STR. Stoi () is const string*, const char* is not required.
As shown in figure:
Stoi () will do the scope check, the default scope is within the scope of int, if beyond the scope will be runtime error!
As shown in figure:
Atoi () will not check the range. If it exceeds the range, it will output the upper limit, and if it exceeds the lower limit, it will output the lower limit.
Code:
The C++ character processing function, the numeric string into an int output
Header files are #include< cstring>
Difference:
Atoi () is const char*, so we must call c_str() to const char* for a string STR. Stoi () is const string*, const char* is not required.
As shown in figure:
Stoi () will do the scope check, the default scope is within the scope of int, if beyond the scope will be runtime error!
As shown in figure:
Atoi () will not check the range. If it exceeds the range, it will output the upper limit, and if it exceeds the lower limit, it will output the lower limit.
Code:
//myfirst.cpp--displays a message
#include "stdafx.h"
#include < iostream>
#include < set>
#include < string>
using namespace std;
int main()
{
string s1 = "2147482", s2 = "-214748";
string s3 = "214748666666663", s4 = "-21474836488";
cout < < stoi(s1) < < endl;
cout < < stoi(s2) < < endl;
cout < < atoi(s3.c_str()) < < endl;
cout < < atoi(s4.c_str()) < < endl;
return 0;
}
Console:
Read More:
- In C + + cin.getline The difference between () and getline () functions
- The usage of several integer functions in MATLAB (fix, floor, ceil, round)
- Usage and examples of three important functions of tidyr package in R language: gather, spread and separate
- Differences between length() size() and C strlen() of C + + string member functions
- The function and usage of argc and argv in C language
- The difference and usage of insmod and modprobe
- error: ‘atoi’ was not declared in this scope
- go :Multiple-value strconv.Atoi() (int, error) in single-value context
- error: ‘stoi’ was not declared in this scope linux (Fixed)
- C#: Analysis of the difference between write() and writeline()
- C + + pauses the black window system (“pause”); (get ch(), getchar(), system (pause)’s connection and difference
- The difference between sleep() and wait() in Java
- Yield usage in Python
- Usage of NVL in SQL
- C++:error C2228: left of ‘.str’ must have class/struct/union
- Raise in Oracle_APPLICATION_Error Usage
- The usage of typing.union in Python
- In Python sys.argv Usage of
- ERF and erfc functions
- Difference between isempty method and isblank method in stringutils