본문 바로가기

C++

[C++] String 맛보기

String(int,char);

char 문자를 int만큼 복사해서 저장한다.

 

 

String concatenate

 

string1+string2

string1이나 string2 둘중 하나만 string literal이거나 char이어야 한다.(둘다는 안됨)

※string literal: "asdf"와 같이 변하지 않는 데이터

 

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

정상작동한다.

 

const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;

작동하지 않는다. 

 

첫번째 코드가 작동하는 것은 hello와 string literal이 더해지고 나서 한번더 "!" string literal을 더하니 작동하는 듯?

두번째 코드처럼 string literal 2개를 처음부터 더하면 작동하지 않는다.

'C++' 카테고리의 다른 글

[C++] Call by Value ,Call by Address, Call by Reference  (0) 2021.12.18
[C++] typedef  (0) 2021.12.17
[C++] cin >> x는 cin을 반환한다  (0) 2021.12.17
[C++] type 지정하기  (0) 2021.12.17
[C++] 특수 기호 출력하기  (0) 2021.12.16