如何在C ++中将字符串转换为int以及将int转换为字符串

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转换为字符串。 如果您的字符串不是有效的整数字符串,则应首先检查它或使用其他方法。

如何在C ++中将字符串转换为int以及将int转换为字符串_第1张图片

字符串转换为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 

long int strtol(const char *nptr, char **endptr, int base);

Example C++ code:

示例C ++代码:

#include 
#include 
#include 

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 
#include 
#include 

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 
#include 

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 

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 

#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 
#include 

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 
#include 

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/

你可能感兴趣的:(c++,字符串,python,leetcode,算法)