Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 472 Bytes

string和int互相转换.md

File metadata and controls

32 lines (24 loc) · 472 Bytes

int转string

// 使用std::to_string()
#include <iostream>
#include <string>

int main ()
{
  int n = 123;
  std::string str = std::to_string(n); // 使用std::to_string()
  std::cout << n << " ==> " << str << std::endl;

  return 0;
}

string转int

#include <iostream>
#include <stdlib.h> // atoi需要的头文件

int main()
{
    std::string str = "668";
    std::cout << atoi(str.c_str()); // 借助char*, 使用atoi()
    return 0;
}