본문 바로가기

자료구조 /cpp

1 map 사용하기

map은 key, value로 이루어진 자료구조이다.

 

맵 기본 함수

 

기본형태

 

 

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

 

 

iterator(반복자)

 

 

    • begin() : beginning iterator를 반환

 

    • end() : end iterator를 반환

 

 

추가 및 삭제

 

 

    • insert( make_pair(key,value) ) : 맵에 원소를 pair 형태로 추가

 

    • erase(key) : 맵에서 key(키값)에 해당하는 원소 삭제

 

    • clear() : 맵의 원소들 모두 삭제

 

 

조회

 

 

    • find(key) : key(키값)에 해당하는 iterator를 반환

 

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

 

 

기타

 

 

    • empty() : 맵이 비어있으면 true 아니면 false를 반환

 

    • size() : 맵 원소들의 수를 반환

 

 

 

 


#include 

#include 

#include 

#include 

#include 

using namespace std;



int main(void) {

	map<string, int> m;

	

	m.insert(make_pair("1", 1));

	



	map<string, int>::iterator iter;



	for (iter = m.begin(); iter != m.end(); ++iter) {

		cout << iter->first << " + "<second<<  endl;

	}



	/*cout << m.find("1")->second << endl;;*/



	cout << m.count("1") << endl;

	return 0;

}

'자료구조 > cpp' 카테고리의 다른 글

3 vector와 priority_queue sorting하기  (0) 2019.05.18
vector & dequeue & list  (0) 2019.04.23