It is common to convert string to int and int to string in C++ programs. This post introduces how to convert a valid string to int and int to string in C++ using C and C++ ways and libraries. If your string is not a valid integer string, you should check it first or use other methods.
在C ++程序中,通常将字符串转换为int并将int转换为字符串。 这篇文章介绍了如何使用C和C ++方式和库在C ++中将有效字符串转换为int以及将int转换为字符串。 如果您的字符串不是有效的整数字符串,则应首先检查它或使用其他方法。
将字符串转换为int (Convert string to int)
1. C风格的方式 (1. The C-style way)
Use C standard library strtol (avoid atoi() which does not report errors).
使用C标准库strtol (避免使用atoi() 不会报告错误 )。
#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);
Example C++ code:
示例C ++代码:
#include <iostream>
#include <cstdlib>
#include <string>
int main()
{
std::string text{"123"};
errno = 0; // pre set to 0
int number = (int)std::strtol(text.c_str(), nullptr, 10);
if (errno == ERANGE) {
// the number is too big/small
// number = (int)LONG_MAX or (int)LONG_MIN
std::cerr << "Too big or small: " << errno << "\n";
return 1;
} else if (errno) {
// maybe EINVAL, E2BIG or EDOM
// unable to convert to a number
std::cerr << "ERROR: " << errno << "\n";
return 1;
}
// TODO: you need to check whether the long to int overflow too if neccessary
std::cout << number << "\n";
return 0;
}
2. C ++风格的方式 (2. The C++-style way)
Way 1. You can use the C standard library as above in C++ too.
方法1.您也可以在C ++中使用上述C标准库。
Way 2. Use C++ standard library std::stringstream.
方式2。使用C ++标准库std::stringstream 。
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string text = "123";
std::istringstream iss (text);
int number;
iss >> number;
if (iss.fail()) {
// something wrong happened
std::cerr << "ERROR!\n";
return 1;
}
std::cout << number << "\n";
return 0;
}
Way 3. Use std::stoi() from C++ standard library since C++11.
方式3.从C ++ 11开始,使用C ++标准库中的std::stoi() 。
#include <iostream>
#include <string>
int main ()
{
std::string str("123");
try {
int n = std::stoi(str);
std::cout << n << "\n";
}
catch (...) {
std::cerr << "ERROR!\n";
}
return 0;
}
将int转换为字符串 (Convert int to string)
1. C风格的方式 (1. The C-style way)
Use C standard library function snprintf().
使用C标准库函数snprintf() 。
#include <stdio.h>
int snprintf(char *str, size_t size, const char *format, ...);
The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.
#include <cstdio>
#define MAX_BUFFER_SIZE 128
int main()
{
int number = 123;
char out_string [MAX_BUFFER_SIZE];
snprintf(out_string, MAX_BUFFER_SIZE, "%d", number);
printf("out_string = \"%s\"\n", out_string);
return 0;
}
2. C ++风格的方式 (2. The C++-style way)
Way 1. You can use the C standard library as above in C++ too.
方法1.您也可以在C ++中使用上述C标准库。
Way 2. Use C++ standard library std::stringstream.
方式2。使用C ++标准库std::stringstream 。
#include <sstream>
#include <iostream>
int main()
{
int i = 123;
std::stringstream ss;
ss << i;
std::string out_string = ss.str();
std::cout << out_string << "\n";
return 0;
}
Way 3. Use C++ standard library std::to_string() available in standard library since C++11.
方式3。使用自C ++ 11起在标准库中可用的C ++标准库std::to_string() 。
#include <iostream>
#include <string>
int main ()
{
int n = 123;
std::string str = std::to_string(n);
std::cout << n << " ==> " << str << std::endl;
return 0;
}
翻译自: https://www.systutorials.com/convert-string-to-int-and-reverse/

1万+

被折叠的 条评论
为什么被折叠?



