STL #

Standard Template Library

STL은 Alexander Stepanov가 Meng Lee와 함께 template으로
구현한 라이브러리 입니다.

1993년 표준화가 이루어질쯤 Alexander Stepanov의 STL을 보고
C++ 표준 위원회의 만장 일치에 의해서 표준에 합류 하게 되었고
STL로 인해 표준화는 몇년 늦어진 1998년에야 이루어 지게 됬습니다.
(이 기간 언어적인 확장은 없었으며 explicit 같은 제한적인 사항이
몇개 추가 되었습니다.)


STL은 공식적인 용어라기 보다 관용적으로 쓰는 C++의
라이브러리 입니다.

STL의 범위는 C++의 표준 라이브러리중 알고리즘과 Iterator를
사용하는 컨테이너 객체를 일반적으로 말합니다.


STL의 의의중 하나는 C++가 OOP언어만이 아닌
multi-paradigm programming language로서의 generic한 기능을
충분히 살린 라이브러리 한 점입니다.


generic programming이란 특정 데이터를 어떻게 가공하느냐의
OOP의 개념 촛점을 맞춘것이 아닌 알고리즘(구현) 부분에
촛점을 맞춘 프로그램밍 방법입니다.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
using namespace std;

template <class IterT, class FuncT>
void ForEach(IterT B_, IterT E_, FuncT Fun_)
{
        while (B_ != E_) {
                Fun_(*B_++);
        }
}

void Print(char c)
{
        cout << c;
}


int main()
{
        char sz[] = "CHAR ARRAY";
        int nLen = strlen(sz);

        vector<char> v(sz, sz + nLen);

        ForEach(sz, sz + nLen, Print);
        cout << endl;

        ForEach(v.begin(), v.end(), Print);
        cout << endl;


        ifstream fin(__FILE__);
        ForEach(istreambuf_iterator<char>(fin),
                istreambuf_iterator<char>(),
                Print
        );

        cin.get();
}

ForEach는 std::for_each 함수를 흉내 내본 것입니다.

ForEach함수는 특정한 타입에 대해 구현을 해놓은것이 아니라
일반(generic)적인 타입에 대해서 순차적으로 노드를 이동해가며
Fun함수를 호출하는것입니다.

여기서 IterT는 char* 타입이든 특정 컨테이너의 Iterator 타입이든
후치증가연산자 "++" 와 operator*()를 제공되는 어떤 타입이라도
매치가 될수 있으며 FuncT 타입도 T(operator*()가 리턴하는 타입)
타입에 매칭되는 한개의 파라미터를 인수로 취하는 어떠한 함수도
매치가 될수 있습니다.


타입에 종속적이지 않는 일반적인 구현을 제공할수 있다는점이
generic programming의 핵심이며 그 generic programming 방식으로
만들어진 C++의 표준 라이브러리가 STL입니다.



STL의 구성 #


컨테이너 객체로는 아래와 같이 존재하며 algorithm 에는
70여개의 알고리즘 함수가 있고 그외 iterator(반복자)
그리고 유틸리티 함수로 구성 되어 있습니다.

컨테이너 
        deque 
        list 
        map 
        multimap 
        multiset 
        set 
        vector 

알고리즘 함수 
        adjacent_find               
        binary_search               
        copy 
        copy_backward               
        count                       
        count_if                     
        equal                       
        equal_range                 
        fill                         
        fill_n                       
        find                         
        find_end                     
        find_first_of               
        find_if                     
        for_each                     
        generate                     
        generate_n                   
        includes                     
        inplace_merge               
        iter_swap                   
        lexicographical_compare     
        lower_bound                 
        make_heap                   
        max                         
        max_element                 
        merge                       
        min                         
        min_element                 
        mismatch                     
        next_permutation             
        nth_element                 
        partial_sort                 
        partial_sort_copy           
        partition                   
        pop_heap                     
        prev_permutation             
        push_heap                   
        random_shuffle               
        remove                       
        remove_copy                 
        remove_copy_if               
        remove_if                   
        replace                     
        replace_copy                 
        replace_copy_if             
        replace_if                   
        reverse                     
        reverse_copy                 
        rotate                       
        rotate_copy                 
        search                       
        search_n                     
        set_difference               
        set_intersection             
        set_symmetric_difference     
        set_union                   
        sort                         
        sort_heap                   
        stable_partition             
        stable_sort                 
        swap                         
        swap_ranges                 
        transform                   
        unique                       
        unique_copy                 
        upper_bound                 


C++ Standard Library #

C++ Standard Library는 STL을 포함하며 그외에
iostream, complex, string 등등이 template 으로
작성 되어 있습니다.

표준 전의 C++ Standard Library는 모두 일반 클래스로
작성 되었으나 표준화 이후 모든 C++ Standard Library는
template으로 재작성 되었습니다.


그리고 새로운 헤더 형식이 채택 되었습니다.
// 예전 C++ 헤더 방식 입니다. 
// 일반 클래스로 작성된 라이브러리 이며 
// 더이상 표준은 아닙니다. 
// .h 가 붙은 헤더를 사용하시면 ISO C++의 호환성을 
// 보장할수 없습니다. 
// 일부 컴파일러는 옜날 코드를 위해 남겨 두었으나 
// 사용을 권장하지 않습니다. 
#include <iostream.h>



// 표준입니다. 
// template 으로 작성 되었으며 
// std 란 namespace 로 쌓여 있습니다. 
#include <iostream>


규칙은 이렇습니다.
C++ 표준 라이브러리는 .h 가 붙지 않습니다.
그리고 std 란 namespace 에 포함되어 있습니다.


C Standard Library #

C 언어는 C++의 부분 집합이며 라이브러리도 해당됩니다.
C 언어의 표준 라이브러리는 C++의 표준 라이브러리이기도 하며
C 언어의 헤더 형식을 사용 하면 됩니다.

#include <stdio.h>

그러나 C++ 의 header 규칙에 의하여 모든 C 라이브러리들은
앞에 c 가 붙고 .h 가 붙지않는 형식으로도 제공되며 권장되어 지고
그 형식으로 제공된 라이브러리는 std namespace에 포함 되어
있습니다.
#include <cstdio>   // #include <stdio.h> 
#include <cstring>  // #include <string.h> 
#include <cstdlib>  // #include <stdlib.h> 
#include <cmath>    // #include <math.h> 


C++ Headers #

<algorithm> <bitset> <cassert> <cctype> <cerrno> <cfloat> 
<ciso646> <climits> <clocale> <cmath> <complex> <csetjmp> 
<csignal> <cstdarg> <cstddef> <cstdio> <cstdlib> <cstring> 
<ctime> <cwchar> <cwctype> <deque> <exception> <fstream> 
<functional> <iomanip> <ios> <iosfwd> 
<iostream> <istream> <iterator> <limits> <list> <locale> 
<map> <memory> <new> <numeric> <ostream> <queue> 
<set> <sstream> <stack> <stdexcept> <streambuf> <string> 
<strstream> <utility> <valarray> <vector> <cstddef> <cstdlib> 
<exception> <limits> <new> <cstdarg> 

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

RAII (Resource Acquisition Is Initialization)  (1) 2008.05.01
상속되지 않는것  (0) 2008.05.01
new 2  (0) 2008.05.01
new 1  (0) 2008.05.01
initialization - array structure  (0) 2008.05.01

+ Recent posts