연관컨테이너-맵(map)
- 프로그래밍/STL
- 2011. 5. 2. 15:04
[프로그래밍/STL] - 일반화 프로그래밍, STL기초
[프로그래밍/STL] - 컨테이너 (시퀀스,연관,어댑터)
[프로그래밍/STL] - 연관 컨테이너-셋(set)
*맵(map)
1. 키(key)와 값(value)을 가지는 컨테이너
2. 연관 컨테이너의 한종류이므로 삽입시 자동 정렬된다.( 디폴트는 키를 중심으로 정렬 )
3. untiliy.h 안의 std::pair 구조체를 쓴다. (first, second 로 접근)
4. #include <map>을 추가하여야함. std안에 존재함
5. 맵은 키의 중복을 허용하지 않는다.
6. 멀티 맵은 키의 중복을 허용한다.
7. 양방향 반복자 사용.
8. []operator 가 오버로딩되있다.
=> 일반적인 할당 연산자가 아닌 키의 값을 참조하여 값을 넣을 수 있게 조작되있음.
ex) MArray[3] = "헐";
9. []연산자는 보통의 포인터 연산이 아닌 양방향으로 순회하여 찾는식이기때문에 찾는 속도가 느릴 수 있다.
10. []는 멀티맵에는 적용 안됨.
*맵을 만드는 기본 조건
1. key/value 쌍은 반드시 할당 가능해야 하며, 복사 가능해야 한다.
2. key는 반드시 정렬 기준에 따라서 비교될 수 있어야 한다.
*구현 관련
1)이진 트리로 구현되있다.
2)key에 따라서 정렬하고 기본적으로 함수객체 less<>로 전달됨
그러므로 컨테이너는 < 연산자로 비교가능하여야함.
3)key를 직접적으로 수정할 수 없다. value값만 바꿀 수 있으며, key를 바꾸려면 지웠다 다시 넣어주는 식으로 해야함.
Member functions
(constructor) | Construct map (public member function) |
(destructor) | Map destructor (public member function) |
operator= | Copy container content (public member function) |
Iterators:
begin | Return iterator to beginning (public member function) |
end | Return iterator to end (public member function) |
rbegin | Return reverse iterator to reverse beginning (public member function) |
rend | Return reverse iterator to reverse end (public member function) |
Capacity:
empty | Test whether container is empty (public member function) |
size | Return container size (public member function) |
max_size | Return maximum size (public member function) |
Element access:
operator[] | Access element (public member function) |
Modifiers:
insert | Insert element (public member function) |
erase | Erase elements (public member function) |
swap | Swap content (public member function) |
clear | Clear content (public member function) |
Observers:
key_comp | Return key comparison object (public member function) |
value_comp | Return value comparison object (public member function) |
Operations:
find | Get iterator to element (public member function) |
count | Count elements with a specific key (public member function) |
lower_bound | Return iterator to lower bound (public member function) |
upper_bound | Return iterator to upper bound (public member function) |
equal_range | Get range of equal elements (public member function) |
Allocator:
get_allocator | Get allocator (public member function) |
Member types
of template <class Key, class T, class Compare=less<Key>, class Allocator=allocator<pair <const Key, T> > > class map;member type | definition |
---|---|
key_type | Key |
mapped_type | T |
value_type | pair<const Key,T> |
key_compare | Compare |
value_compare | Nested class to compare elements (see member function value_comp) |
allocator_type | Allocator |
reference | Allocator::reference |
const_reference | Allocator::const_reference |
iterator | Bidirectional iterator |
const_iterator | Constant bidirectional iterator |
size_type | Unsigned integral type (usually same as size_t) |
difference_type | Signed integral type (usually same as ptrdiff_t) |
pointer | Allocator::pointer |
const_pointer | Allocator::const_pointer |
reverse_iterator | reverse_iterator<iterator> |
const_reverse_iterator | reverse_iterator<const_iterator> |
*반복자로 key와 value 접근하기.
pair구조체를 이용했으므로 이런식으로 pair구조체의 first와 second를 이용하면된다.
map<Key,T>::iterator it;
*생성자와 소멸자
*멤버함수 원형
*pair 구조체 사용법
1)value_type 사용법
std::map< std::string, float > coll;
coll.insert( std::map<std::string, float>::value_type( "상기", 187.0 ) );
2)pair<> 사용
std::map< std::string, float > coll;
coll.insert( std::map<std::pair<std::string, float> ( "상기", 187.0 ) );
3)make_pair() 사용
std::map< std::string, float > coll;
coll.insert( std::make_pair( "상기", 187.0 ) );
이 글을 공유하기