반응형

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;
}

 

<이상>

 

반응형

+ Recent posts