137 字
1 分钟
字符串与数字的转化
2026-07-09

将字符串转化为指定类型的数字#

  • string \rightarrow int

    • std::string s = "123";
      int i;
      /* std to int */
      i = stoi(string);
  • string \rightarrow long

    • std::string s = "123";
      long i;
      /* std to long */
      i = stol(string);
  • string \rightarrow long long

    • std::string s = "123";
      long long i;
      /* std to long long */
      i = stoll(string);
  • string \rightarrow float

    • std::string s = "123.4";
      float i;
      /* std to float */
      i = stof(string);
  • string \rightarrow double

    • std::string s = "123.4";
      double i;
      /* std to double */
      i = stod(string);
  • string \rightarrow long double

    • std::string s = "123.4";
      long double i;
      /* std to long double */
      i = stold(string);

数字转换为字符串#

  • 数字 \rightarrow string
    • int i = 100;
      std::string s = std::to_string(i);
字符串与数字的转化
https://kuchikirei.github.io/posts/stringandnums/
作者
LINKONG
发布于
2026-07-09
许可协议
CC BY-NC-SA 4.0