C++, string 형 크기와 문자열 길이 비교 출력
글. 오상문 sualchi@daum.net
string 형 크기와 문자열 데이터 길이는 다릅니다.
string s = "abcd";
위 예제에서,
sizeof(string) 또는 sizeof(s)는 string 형 또는 string 형 변수의 메모리 크기이며,
s 문자열 데이터의 실제 길이를 구하려면 length() 메소드를 이용해야 합니다.
아래 예제를 참고하시기 바랍니다.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s = "abcd";
cout << sizeof(string) << " Bytes" << endl; // 8 Bytes
cout << sizeof(s) << " Bytes" << endl; // 8 Bytes
cout << s.length() << " Bytes" << endl; // 4 Bytes
system("pause");
return 0;
}
<이상>
반응형
'C++' 카테고리의 다른 글
C++ 프로그래밍 가이드 (TCP School) (0) | 2022.06.06 |
---|---|
C++ 비트값 문자열 다루기, 비트 1의 개수, 비트 문자열 너비 계산 예제 (0) | 2021.10.12 |
C++ 싱글 링크드 리스트 구현 (0) | 2016.12.28 |
바이너리(이진) 파일 입출력 C++ 예제 (0) | 2016.12.08 |
C++ 텍스트 파일 입출력 예제 (0) | 2016.12.07 |