[cpp] STL map 사용법

roopre·2022년 5월 24일
0

코딩테스트준비

목록 보기
2/6

선언

  • < key, value >
map< string, int > m;

insert

m.insert(make_pair("a",1));
m.insert(make_pair("b",2));
m.insert(make_pair("c",3));
m.insert(make_pair("d",4));
m.insert(make_pair("e",5));
m.insert(make_pair("f",6));

erase

m.erase("d");
m.erase("a");

size, empty

if(!m.empty()) {
	cout << m.size() << endl;
}

find

cout <<"a:" << m.find("a")-> second << endl;
cout <<"b:" << m.find("b")-> second << endl;

count

cout << "a count:" << m.count("a") << endl

traverse

for(auto it = m.begin(); it != m.end();it++) {
	cout <<"key: " << it->first << "value: " << it->second << endl;
}
    
profile
Roopretelcham

0개의 댓글