c++ vector 선언의 공간할당에 관한 질문

gyxor의 이미지

이런한 선언에서..


    string s1[4] = {"fine", "fish", "fashion", "fate"};
    string s2[2] = {"busy", "bats"};
    string s3[2] = {"silly", "singers"};
    vector<string> words(4);
    copy(s1, s1 + 4, words.begin());
    ostream_iterator<string, char> out(cout, " ");
    copy (words.begin(), words.end(), out);
    cout << endl;
 

세가지 질문입니다.

1.번..

string 자체의 선언에서는

동적 할당인가요

정적할당인가요?

동적할당인거 같은데요.. 그렇다면 소멸자를 자동으로 제공하는것인가요?

void remode(string &str)

{

string *ps = new string (str );

}

이런 구문이 있는것으로 봐서 정적 할당인거 같기도 하구요

잘 이해가 안됩니다.

string 도 클래스이므로 string 클래스에 대한 new 연산자가 따로

정의 되어있다고 생각하면 되는것인가요?

일반변수에 대한 new 의 쓰임과는 다른것 같아서요

void remode(double &str)

{

double *ps = new double (str );

}

이런 식으로는 안쓰지 안나요?

double *ps = new double (사이즈); 가 되어야 하는데..

void remode(string &str)

{

string *ps = new string (str );

}여기에서는 str 이 string 변수이기 때문에 크기변수가 아니고..

쓰임이 다른거 같습니다.

설명 부탁드립니다.

2.번..질문

vector<int> words(4); 이라면..

int 의 크기를 알기 때문에 2바이트 4개의 사이즈로 동적할당

8바이트를 하게 되는데요

vector<string> words(4);
이부분에서는 string 자체가 클래스이고 그 크기는 대입되는

문자열에 따라 달라지는데요

어떻게 크기를 잡고서 4개를 선언알수있는지 알고싶습니다.

3.번질문

ostream_iterator 이 후방삽입 반복자 인가요?
갑자기 이런 내용이 나와서 생소하게 느껴집니다.
ostream_iterator<string, char> out(cout, " ");

stl 을 쓰게 되면 대부분이 이런 함수들인가요?

이구문에 대한 설명부탁드립니다.

고수님들의 답변을 부탁드립니다.

urmajest의 이미지

아마 compile-time에 binding되는 heap-dynamic type일겁니다

즉, 동적이란 말이죠

그리고 new operator를 사용할때

new (type)[n] 과

new (type)(arg1,arg2, ...) 는 차이가 있죠..많죠 -_-

위의 형태는 아시는 것 같구.. 아래의 형태는 constructor에서 지정된

argument를 pass해주는 것이죠..

아마 string class의 constructor중에 string* type의 argument를 받는

게 있어서 그게 가능한거겠죠

그럼 ^^

clhitter의 이미지

1.
string class 자체는 동적 할당 할수도 있고 정적 할당 할 수도 있습니다.
string class가 다뤄야 하는 문자열들이 variable length여야 하므로 내부적으로는 메모리를 동적할당 해놓고 멤버 변수들을 통해서 이곳을 포인터로 가리키고 있겠지만 이것과 string class 자체가 동적할당 해야 하느냐 정적할당 되어야 하느냐는 관계가 없습니다.
string class destructor에서는 string class가 내부적으로 관리하는 동적할당된 메모리를 해제 시켜주겠죠.
만약 string instance가 정적할당 되어 stack에 정의된 경우 string instance 자체는 stack에 올라가 있지만 string이 관리하는 문자열은 heap에 동적할당되어 있고 string class의 member 변수가 이곳을 어떤 방법으로 가리키고 있겠죠.

2.
string instance 자체의 크기는 고정입니다. string instance가 가리키는 문자열의 크기는 가변이지만 string instance는 단지 그것에 대한 pointer를 가지고 있을 뿐이죠.
그리고 vector<int> words(4);
한다고 해서 반드시 2*4=8 byte가 할당된다고 볼 수 없습니다. 그건 vector class의 구현이 어떻게 되었느냐에 따라 다르죠. vector는 사이즈가 크기될때마다 새로 메모리 할당되지 않고 performance를 위해서 지수적으로 크기가 증가하는게 일반적입니다. 물론 construct 할때는 정확히 필요한 메모리 크기 만큼만 할당하는 implementation도 있겠지만요.

3.
ostream_iterator는 후방삽입 iterator라기 보단 그냥 output iterator의 일종이라고 보시면됩니다.
STL을 제대로 활용하시려면 container와 algorithm 이라는 두개의 축이 있고 이것을 연결해 주는 것이 iterator라는 것을 이해하셔야 합니다. STL 책을 좀더 자세히 읽어보시는게 어떨까 싶네요 ^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.