31
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Stringの変換(cast) チートリスト

Last updated at Posted at 2018-08-31

char[]からStringに変換

文字列リテラルなどのアドレスをそのまま渡す。

test.cpp
char c_str[] = {"hello world!"};
std::string s_str = c_str;

Stringからchar*に変換

c_str()を使う

test.cpp
std::string str = "hello world!"
const char* cstr = str.c_str();

intからStringに変換

test.cpp
int num = 4321;
str = std::to_string(num);

Stringからintに変換

test.cpp
std::string numStr = "1234";
int num = std::stoi(numStr);
31
20
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?