'컨테이너'에 해당되는 글 1건

  1. 2010.03.22 STL 컨테이너 맵 <std::map> 간략 정리.
C++2010. 3. 22. 10:51

#. 설명.
 - 다른 STL의 컨테이너 처럼 map은 많은 장점을 가진 유익한 컨테이너입니다. 빠른 Indexing을 위해서라면 STL의 컨테이너중 map을 가장 먼저 생각할 수 있겠습니다.
 - map은 Associative Container (연관컨테이너)에 속하는데 Associative Container란 키를 가지고 sort된 후에 적재되는 컨테이너로 검색속도를 빠르게 하기 위한 목적을 가진 컨테이너 입니다. map은 정렬 연관 컨테이너이며, key와 value의 쌍으로 이루어진 자료구조입니다.
 - 대부분 균형 이진 탐색 트리(balanced binary search tree)를 사용해 구현하며 추가/삭제에 O(log n)이 걸리며 검색에 있어서 탁월한 성능을 발휘합니다.

#. map의 특징에 대해서 간단히 살펴 보면 다음과 같습니다.

1. key와 value로 element를 관리할 수 있다.
2. key는 숫자 이외에도 문자열 순서를 가질 수 있는 (즉, 비교 연산자를 가지고 있는) 데이터 타입이면 모두 key가 될 수 있다.
3. key는 순서 (order)를 가지고 관리되기 때문에 검색이 빠르다.
4. key는 unique해야 한다.
5. aMapkey = value 와 같이 [] 연산자를 제공해준다.
6. Element가 추가/삭제 될 때 크기도 같이 확장 축소 된다.


#. 아래는 MSDN에 나와있는 샘플코드입니다. 참고하세요^^

Sample Code:

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: None
//
// <filename> :  main.cpp
//
// Functions:
//
//      end
//      find
//      insert
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <map>

using namespace std;


typedef map<int, string, less<int> > INT2STRING;

void main()
{
// 1. Create a map of ints to strings
    INT2STRING theMap;
    INT2STRING::iterator theIterator;
    string theString = "";
    int index;

// Fill it with the digits 0 - 9, each mapped to its string counterpart
// Note: value_type is a pair for maps...
    theMap.insert(INT2STRING::value_type(0,"Zero"));
    theMap.insert(INT2STRING::value_type(1,"One"));
    theMap.insert(INT2STRING::value_type(2,"Two"));
    theMap.insert(INT2STRING::value_type(3,"Three"));
    theMap.insert(INT2STRING::value_type(4,"Four"));
    theMap.insert(INT2STRING::value_type(5,"Five"));
    theMap.insert(INT2STRING::value_type(6,"Six"));
    theMap.insert(INT2STRING::value_type(7,"Seven"));
    theMap.insert(INT2STRING::value_type(8,"Eight"));
    theMap.insert(INT2STRING::value_type(9,"Nine"));

// Read a Number from the user and print it back as words
    for( ; ; )
    {
        cout << "Enter \"q\" to quit, or enter a Number: ";
        cin >> theString;
        if(theString == "q")
            break;
        // extract each digit from the string, find its corresponding
        // entry in the map (the word equivalent) and print it
        for(index = 0; index < theString.length(); index++){
            theIterator = theMap.find(theString[index] - '0');
            if(theIterator != theMap.end() )    // is 0 - 9
                cout << (*theIterator).second << " ";
            else    // some character other than 0 - 9
                cout << "[err] ";
        }
        cout << endl;
    }
}


#. 위 소스의 출력화면 입니다.

Program Output is:

Enter "q" to quit, or enter a Number: 22
Two Two
Enter "q" to quit, or enter a Number: 33
Three Three
Enter "q" to quit, or enter a Number: 456
Four Five Six
Enter "q" to quit, or enter a Number: q

Posted by 쿵캉켕