본문 바로가기

알고리즘 문제풀이/알고리즘

[c++] 문자열, 숫자 변환 / stoi 함수 / to_string 함수 /

string -> integer로 변환

http://www.cplusplus.com/reference/string/stoi/

 

stoi - C++ Reference

function template std::stoi int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); Convert string to integer Parses str interpreting its content as an integral number of the spe

www.cplusplus.com

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

using namespace std;

int main ()
{
  string str = "12";
  int num = stoi(str);
  
  printf("%d\n", num);

  return 0;
}

 

string으로 변환

http://www.cplusplus.com/reference/string/to_string/

 

to_string - C++ Reference

function std::to_string string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (

www.cplusplus.com

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

using namespace std;

int main ()
{
  int number = 123;
  string str = "Hi, I'm" + to_string(number);
  
  cout << str << '\n';

  return 0;
}