본문 바로가기
OLD_알고리즘

C++ ] Hash - map

by 달승 2021. 6. 2.

Hash란

Key-value쌍으로 데이터를 저장하는 자료구조입니다.

 


Map

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;


Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).

Maps are typically implemented as binary search trees.

 

 

 

💊 멤버 함수

  Member functions definition example
 - - #include <map>
map< string, int > example;
// map < KEY, VALUE >
capacity empty - example.empty(  )? yes : no;
size 요소 수를 계산합니다. example.size(  );
Iterators begin 처음에 대한 반복자를 return합니다. example.begin(  );
end last + 1의 원소를 return합니다. example.end(  );
Element access at 지정된 키 값이 있는 요소에 접근합니다. example = {  {"Mars" 3000} , 
                 {"Jupiter", 7000}  };

example.at( "Mars" ) = 25;> example 내의 Mars의 value는 25로 바뀝니다.
Element lookup find Get iterator to element example.find( "Mars" );

find 함수의 경우 만일 해당하는 원소가 존재한다면 이를 가리키는 반복자를, 없다면 end 를 리턴합니다.
count Count elements with a specific key
(find와 동일)
example.count( "Mars" )
Modifiers insert Insert elements example.insert({"three", 3});
example.insert(make_pair("four", 4));
erase Erase elements example.erase( example.find( "Hi" ) );

간단히 find 함수로 원소를 가리키는 반복자를 찾은 뒤에, 이를 전달하면 됩니다.

 

 


📖 예시

1. 기본 예제

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

map<char,int> mymap;

int main ()
{
  // first insert function version (single parameter):
  mymap.insert ({'a',100 });
  mymap.insert ( pair<char,int>('a',100) );
  mymap.insert ( pair<char,int>('z',200) );

  mymap.insert (pair<char,int>('b',300));
  
  // showing contents:
  cout << "mymap contains: insert 순서 - a, z, b \n";
  for (auto it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  
  mymap.insert (pair<char,int>('c',400));  // no max efficiency inserting

  // third insert function version (range insertion):
  map<char,int> anothermap;
  anothermap.insert(mymap.begin(), mymap.find('c'));

  // showing contents:
  cout << "mymap contains: insert 순서 - a, z, b, c \n";
  for (auto it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';

  cout << "anothermap contains:\n";
  for (auto it=anothermap.begin(); it!=anothermap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}
출력 결과

 

2. 활용 예제

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

 


reference

 

map - C++ Reference

difference_typea signed integral type, identical to: iterator_traits ::difference_type usually the same as ptrdiff_t

www.cplusplus.com

 

댓글