티스토리 뷰

Algorithm/C++ STL

[C++] [STL] 6. hash_map

쟌쥰 2019. 11. 29. 19:19

hash_map을 사용하는 경우

  • 많은 자료를 저장하고, 검색 속도가 빨라야 한다.
  • 너무 빈번하게 자료를 삽입, 삭제 하지 않는다.

 

hash_map 주요 함수

<기본형태>

  • map<key,value> : key와 value를 pair형태로 선언

<iterator = 반복자> 

  • begin() : beginning iterator 반환 
  • rbegin() : 역방향으로 첫 번째 원소의 iterator 반환 
  • end() : end iterator 반환 
  • rend() : 역방향으로 마지막 원소 다음의 iterator 반환 
  • lower_bound() : 지정한 key의 요소가 있다면 해당 위치의 iterator 반환 
  • upper_bound() : 지정한 key의 요소가 있다면 해당 위치 다음 위치의 iterator 반환 

<추가 및 삭제>

  • clear() : 저장한 모든 원소 삭제 
  • erase() : 특정위치 or 지정범위의 원소 삭제 
  • insert() : 원소 추가

<조회>

  • find(key) : key(키값)에 해당하는 iterator를 반환 
  • count(key) : key(키값)에 해당하는 원소들(value들)의 개수를 반환

<기타>

  • empty() : 리스트가 비어있음 true 아님 false반환 
  • size() : 리스트 원소들의 갯수를 반환

 

 

예제 코드

#include<hash_map>
using namespace std;

int main(void) {

	//변수선언!
	hash_map<int, float> hash1;	//hash_map 선언
	hash_map<int, float> *hash2 = new hash_map<int, float>;	//hash_map 동적할당

	//자료추가 = insert() 함수
	hash1.insert(hash_map<int, float>::value_type(10, 45.6f));	//단순 추가
	hash1.insert(hash1.begin(), hash_map<int, float>::value_type(11, 50.2f));	//특정위치에 추가
	hash2->insert(hash1.begin(), hash1.end());	//지정한 구간에 있는것들 추가

	//삭제
	hash1.erase(hash1.begin());		//첫번째 위치의 요소 삭제
	hash1.erase(hash1.begin(), hash1.end());	//처음~마지막 삭제
	hash1.erase(11);		//key가 11인 요소 삭제

	//검색 - key를 사용해 같은 key를 갖고 있는 요소를 찾음(찾으면 iterator 반환, 아니면 end() 반환)
	hash_map<int, float>::iterator Finditer = hash1.find(10);
	if (Finditer != hash1.end())
		Finditer->second = 290.44f;

}

 

 

hash_map 을 사용한 유저 관리

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

//struct GameCharacter {
//	GameCharacter(int _CharCd, int _Level, int _Money) {
//		CharCd = _CharCd;
//		Level = _Level;
//		Money = _Money;
//	}
//	
//	int CharCd;
//	int Level;
//	int Money;
//};

int main(void) {
	//hash_map<int, GameCharacter> Characters;

	////초기화
	//GameCharacter	Character1(12, 7, 1000);
	//Characters.insert(hash_map<int, GameCharacter>::value_type(Character1.CharCd, Character1));
	//GameCharacter	Character2(15, 20, 111000);
	//Characters.insert(hash_map<int, GameCharacter>::value_type(Character2.CharCd, Character2));
	//GameCharacter	Character3(200, 34, 3345000);
	//Characters.insert(hash_map<int, GameCharacter>::value_type(Character3.CharCd, Character3));

	hash_map<int, int> Characters;

	//초기화
	//Characters.insert(hash_map<int, int>::value_type(1, 10));
	Characters.insert(1,10);
	//Characters.insert(hash_map<int, int>::value_type(2, 20));
	//Characters.insert(hash_map<int, int>::value_type(3, 30));

	//iterator (begin, end) 사용, 정방향 순회
	hash_map<int, GameCharacter>::iterator Iter1;
	for (Iter1 = Characters.begin(); Iter1 != Characters.end(); ++Iter1) {
		cout << "캐릭터 코드 : " << Iter1->second.CharCd << "| 레벨 : " << Iter1->second.Level << "| 가진 돈 : " << Iter1->second.Money << endl;
	}
	cout << endl;

	//iterator (rbegin, rend) 사용, 역방향 순회

}

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

[C++] [STL] 8. priority_queue  (0) 2019.11.29
[C++] [STL] 7. set  (0) 2019.11.29
[C++] [STL] 5. map  (0) 2019.11.29
[C++] [STL] 4. list  (0) 2019.11.29
[C++] [STL] 3. vector  (0) 2019.11.29
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함