연관컨테이너-맵(map)

앞의 게시판을 참고하고 보면 도움이 됩니다^^.
[프로그래밍/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


Iterators:

Capacity:

Element access:

Modifiers:

Observers:

Operations:

Allocator:

Member types

of template <class Key, class T, class Compare=less<Key>, class Allocator=allocator<pair <const Key, T> > > class map; 
member typedefinition
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>

출저:http://www.cplusplus.com/reference/stl/map/



*반복자로 key와 value 접근하기.
pair구조체를 이용했으므로 이런식으로 pair구조체의 first와 second를 이용하면된다. 


map<Key,T>::iterator it;
(*it).first;             // the key value (of type Key)
(*it).second;       // the mapped value (of type T)
(*it);                   // the "element value" (of type pair<const Key,T>) 

it->first;               // same as (*it).first   (the key value)
it->second;         // same as (*it).second  (the mapped value) 




*생성자와 소멸자 

explicit map(const BinPred& comp = BinPred());
           map(const map& x);
           map(InIt first, InIt last, const BinPred& comp = BinPred());


 *멤버함수 원형
pair<iterator, bool> insert(const value_type& val);
iterator                  insert(iterator it, const value_type& val);
void                      insert((iterator first, iterator last);

T& operator[](const Key& k);

iterator     erase(iterator it);
iterator     erase(iterator first, iterator last);
size_type erase(const Key& key);

iterator          find(const Key& val);
const_iterator find(const Key& val) const; 




*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 ) );  







이 글을 공유하기

댓글

Designed by JB FACTORY