728x90

STL의 문자열을 다루는 전용 컨테이너가 string입니다. 사실 string은 basic_string<char>의 typedef일 뿐입니다.

 

문자열을 다루는 컨테이너는 string과 wstring 두 가지입니다.

  • string은 basic_string<char>의 typedef입니다.
  • wstring은 basic_string<wchar_t>의 typedef입니다.

 

간단하게 말해서 char는 ASCII 문자셋(8비트 문자셋)을 위한 타입이며 wchar_t는 Wide Byte Character Set(WBCS:16비트 문자셋)을 위한 타입입니다. 이것은 윈도우즈 기준이며 문자셋에 대한 내용은 다음에 공부할 기회가 있을 것입니다.(SBCS, DBCS, MBCS, WBCS)

 

그래서 string과 wstring은 저장 바이트 수만 다를 뿐 모든 멤버와 기능은 같습니다.(basic_string<>의 typedef일 뿐이므로)

우리는 char형태의 문자를 저장하는 string을 공부합니다.

 

string은 '시퀀스 컨테이너'이며 '연속 메모리 기반 컨테이너'입니다. vector와 비슷한 특징을 갖습니다.

string의 주요 개념을 그림으로 표현하면

 

 

 

 

1, string의 특징과 주요 함수

string은 문자열 전용 컨테이너로 문자열의 추상화 클래스입니다. 그래서 문자열을 쉽게 처리할 수 있는 여러 가지 기능을 제공합니다.

 

간단한 문자열 출력 예제.

#include <iostream>
#include <string>
using namespace std;
void main( )
{  
    string str = "Hello!";

    cout << str << endl;
}
  1. Hello!

 << 연산자가 중복돼 있으므로 Hello!을 출력합니다.

 

연속 메모리 기반 컨테이너의 특징은 [] 연산자 함수도 가지고 있습니다.

#include <iostream>
#include <string>
using namespace std;

void main( )
{  
    string str;

    str.push_back('H');
    str.push_back('e');
    str.push_back('l');
    str.push_back('l');
    str.push_back('o');
    str.push_back('!');

    cout << str << endl;

    for(int i = 0 ; i < str.size() ; i++)
        cout << str[i] ;
    cout << endl;
}
  1. Hello!
    Hello! 

 str[i]가 가능합니다.

 

'연속 메모리 기반 컨테이너'이므로 string은 vector처럼 임의 접근 반복자를 제공합니다.

#include <iostream>
#include <string>
using namespace std;
void main( )
{  
    string str;

    str.push_back('H');
    str.push_back('e');
    str.push_back('l');
    str.push_back('l');
    str.push_back('o');
    str.push_back('!');

    string::iterator iter;
    for( iter = str.begin() ; iter != str.end() ; iter++)
        cout << *iter;
    cout << endl;

    iter = str.begin() + 2;
    cout << *iter << endl;
    iter += 2;
    cout << *iter << endl;
    iter -= 4;
    cout << *iter << endl;
}
  1. Hello!
    l
    o
    H

임의 접근 반복자는 산술연산이 가능합니다.

또 vector와 비슷한 특징을 갖습니다.


#include <iostream>
#include <string>
using namespace std;

void main( )
{  
    string str;

    cout << "size : capacity " << endl;
    for(int i = 0 ; i < 26  ; i++)
    {
        str.push_back('A'+i);
        cout << str.size() <<":" << str.capacity() << endl;
    }
}
  1. size : capacity
    1:15
    2:15
    3:15
    4:15
    5:15
    6:15
    7:15
    8:15
    9:15
    10:15
    11:15
    12:15
    13:15
    14:15
    15:15
    16:31
    17:31
    18:31
    19:31
    20:31
    21:31
    22:31
    23:31
    24:31
    25:31
    26:31

메모리를 확장하는 정책이 조금 다를 뿐입니다.

 

문자열은 문자들의 추가가 빈번하게 발생할 수 있으므로 reserve한 정확한 크기만큼 할당하지 않고 메모리 확장 정책에 따라 일정한 크기만큼 메모리 공간을 확보합니다.


#include <iostream>
#include <string>
using namespace std;

void main( )
{  
    string str;

    str.reserve(26);

    cout << "size : capacity " << endl;
    for(int i = 0 ; i < 26  ; i++)
    {
        str.push_back('A'+i);
        cout << str.size() <<":" << str.capacity() << endl;
    }
}
  1. size : capacity
    1:31
    2:31
    3:31
    4:31
    5:31
    6:31
    7:31
    8:31
    9:31
    10:31
    11:31
    12:31
    13:31
    14:31
    15:31
    16:31
    17:31
    18:31
    19:31
    20:31
    21:31
    22:31
    23:31
    24:31
    25:31
    26:31

 26만큼의 메모리를 예약하지만 실제 메모리 확장 정책에 따라 31크기만큼 확보합니다.

 

2, 문자열 관련 함수

문자열 관련해서 여러 가지 함수를 제공합니다.

 

입력 스트림도 지원합니다.

#include <iostream>
#include <string>
using namespace std;

void main( )
{  
    string str;

    cin >> str ; // Hello! 입력
    cout << str << endl;
}
  1. Hello!

 >> 연산자가 중복되어 있습니다.

 

 

기본적인 문자열 함수.

 #include <iostream>
#include <string>
using namespace std;
void main( )
{  
    string str1= "Hello!";
    string str2;

    str2 = str1;
    cout << str1 << " , " << str2 << endl;
    if( str1 == str2 )
        cout << "true" << endl;
    str2 += str1;
    cout << str2 << endl;
    str2 = str1.substr(0, 2);
    cout << str2 << endl;
    cout << str1.find('e') << endl;
}
  1. Hello! , Hello!
    true
    Hello!Hello!
    He
    1

 쉽게 알 수 있습니다.

 

또 string 컨테이너는 char * 관련 문자열 함수를 지원합니다.

#include <iostream>
#include <string>
using namespace std;

void main( )
{  
    string str = "Hello!";

    const char * s1 = str.c_str();
    cout << s1 << endl;

    char buf[100]={0};
    str.copy(buf, 6);
    cout << buf << endl;

}

  1. Hello!
    Hello!

 c_str()함수는 '\0'문자를 포함한 문자열을 리턴합니다. 버퍼에 문자열을 받아오는 copy()함수도 지원합니다.

 

이외에 string은 '임의 접근 반복자'를 지원하므로 임의 접근 반복자를 사용하는 여러 알고리즘을 이용할 수 있습니다.


출처 : http://blog.daum.net/coolprogramming/76

728x90

'Basic Programming > STL' 카테고리의 다른 글

STL - Set, MultiSet 컨테이너  (0) 2016.04.04
STL - Deque 컨테이너  (0) 2016.04.04
STL - List 컨테이너  (0) 2016.03.23
STL - Vector 컨테이너  (0) 2016.03.23
STL - Standard Template Library (STL)  (0) 2016.03.22

+ Recent posts