본문 바로가기
PROGRAMING📚/자료구조📑

STL_템플릿 _map을 이용하여 (Map 트리)

별찌루 2021. 4. 3.
728x90
반응형

#include<iostream>

#include<map>

#include<string>

using namespace std; 

 

 

void main() {

   map<int, string> myMap;

  

 //배열처럼 사용하면 덮어쓰기가 가능하다 

   myMap[0] = "abc";

   myMap[2] = "ㅅㅂㄴ";

   myMap[10] = "qwer";

   

//덮어쓰기 금지

   myMap.insert(pair<int, string>(5, "가나다"));

   map<int, string> ::iterator iterFind = myMap.find(2);

   if (iterFind != myMap.end()) {

      cout << iterFind->second.c_str();

   }

   cout << myMap[10].c_str() << endl;

 

   map <int, string> ::iterator iter = myMap.begin();

   while (iter != myMap.end()) {

      cout << iter->first << endl;

      cout << iter->second.c_str() << endl;

      iter++;

   }

 

   //string, string일때는 정렬이 되지 않음 int일때는 정렬 가능;

}

728x90
반응형

댓글