(수근수근)
HashMap 자료구조 study 본문
출처 : http://www.hanbit.co.kr/channel/category/category_view.html?cms_code=CMS4230438179
C++ 지식
- 시퀀스 컨테이너 : vector, list , deque
- 연관 컨테이너 : key와 짝을 이루어 자료보관 / 자료 수정에 용이
- ex : map /set / hashmap / hashset
- 여기서 hash는 정렬이 필요없고 빠른 검색을 원할 때 사용한다.
- using namespace vs #include
#include <iostream>
#include <hash_map>
#include <string>
#include <stdio.h>
using namespace std;
다들 의례쓰지만... 나만 왜인지 몰라......ㅋㅋㅋ
C++에서 #include를 하면 헤더파일의 내용을 복사해서 붙여 넣는것과 같습니다.자바의 import와 비슷한 것 같다.
include(c++) vs import(java)
출처 : http://blog.naver.com/PostView.nhn?blogId=boy0&logNo=100038803187&parentCategoryNo=5&categoryNo=&viewDate=&isShowPopularPosts=true&from=search
namespace 를 통해서 변수의 선언이 가능하고 ::연산자를 통해서 선언없이 곧바로 변수의 이용이 가능하다
- 유니크 명명의 한계를 극복하기 위해서/
이것저것 찾아보다 보니 두서가 없지만.
include는 헤더파일의 내용을 복사하여 붙여넣기 하는 것이고 따라서 컴파일러는 전체 헤더파일이 실제로 C++에 붙여진 것을 알 수 있다.
반면에 namespace는 코드를 논리적으로 그룹화 하는 것이다.
따라서 nameaspace는 별로의 파일에 저장되지 않으며, 특정파일에 대한 확장자가 없다.
Hash_map 특징
1. 검색이 빠르다. but적은 요소를 사용할때는 메모리 낭비, 오버헤드
2. 빈번하게 자료 삭제 삽입에는 좋지 않다.
Hash_map 주요 method
- insert : 원소 추가
- size : 원소의 개수 반환
- erase(key) : 특정위치의 원소나 지정범위의 원소 삭제
- clear :모든 원소를 삭제
- find(key) : key와 연관된 원소의 반복자 반환
- count(key ) : key값에 해당하는 value의 갯수를 반환
- empty :지정한 요소가 없으면 true반환
- iterator
- begin() : beginning iterator반환
- end() ;: end iterator반환
Hash_map 실습
#include<iostream>
#include<hash_map>
#include<string>
#include<stdio.h>
using namespace std;
//1. Hashmap 선언
typedef hash_map<int, string> INT_STR_MAP;
INT_STR_MAP hashmap1;
//2. Hashmap 원소 추가
void add(int key, string value){
hashmap1[key] = value;
}
void print(){
INT_STR_MAP::iterator it;
for(it=hashmap1.begin(); it!=hashmap1.end(); it++) {
//cout << "KEY : " << it->first << "\t" << "Value : " << it->second << endl;
printf("KEY : %d\tValue : %s\n", it->first, it->second.c_str());
}
}
//3. Hashmap 조회
bool search(int input){
INT_STR_MAP::iterator it;
it = hashmap1.find(input);
if(it != hashmap1.end()) {
return true;
} else {
return false;
}
}
//4. Hashmap 삭제
void remove(int input){
INT_STR_MAP::iterator it;
it = hashmap1.find(input);
if(it != hashmap1.end()) {
//cout << "KEY : " << it->first << "\t" << "Value : " << it->second << endl;
hashmap1.erase(input);
} else {
cout << "해당 KEY 값으로 저장된 데이터가 없음." << endl;
}
}
void main()
{
add(101, "A");
add(102, "B");
add(103, "C");
add(104, "D");
add(105, "E");
print();
int input=0;
cout << "삭제할 key 입력 > ";
cin >> input;
if(search(input)){
remove(input);
print();
}
}
'C++' 카테고리의 다른 글
[C++] 기초 관련 용어 (0) | 2020.05.18 |
---|---|
Linking / Linker (0) | 2020.05.15 |
암호화 알고리즘 AES-256 (0) | 2020.05.07 |
[C++]선형자료구조 - vector (0) | 2020.04.06 |
[C++] 상속 공부 (0) | 2020.03.25 |
Comments