MAP이란?

MAP 사용 방법

map 선언하기

map search

if (m.find("Alice") != m.end()) {
	cout << "find" << endl;
}
else {
	cout << "not find" << endl;
}

데이터 삽입

반복문 데이터 접근

for (auto iter = m.begin() ; iter !=  m.end(); iter++)
{
	cout << iter->first << " " << iter->second << endl;
}

for (auto iter : m) {
	cout << iter.first << " " << iter.second << endl;
}

데이터 삭제

m.erase(m.begin()+2); // 특정 위치의 요소 삭제
m.erase("Alice"); // key 값을 기준으로 요소 삭제
m.erase(m.begin(),m.end()); // 모든 요소 삭제
m.clear();

key 값 찾기

#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> myMap;
    myMap[1] = "one";
    myMap[2] = "two";
    myMap[3] = "three";

    // 키 2가 맵에 존재하는지 확인
    if (myMap.count(2) > 0) {
        std::cout << "Key 2 exists in the map." << std::endl;
    } else {
        std::cout << "Key 2 does not exist in the map." << std::endl;
    }

    // 키 4가 맵에 존재하는지 확인
    if (myMap.count(4) > 0) {
        std::cout << "Key 4 exists in the map." << std::endl;
    } else {
        std::cout << "Key 4 does not exist in the map." << std::endl;
    }

    return 0;
}