C++에서 STL 맵 컨테이너 사용
- 
          
            std::map을 사용하여 C++에서 맵 컨테이너 객체 선언
- 
          
            find멤버 함수를 사용하여 C++에서 맵의 특정 요소 검색
- 
          
            insert멤버 함수를 사용하여 C++에서 맵에 요소 저장
- 
          
            merge멤버 함수를 사용하여 C++에서 두 맵 오브젝트의 요소 결합
 
이 가이드는 C++에서 STL 맵 컨테이너를 사용하는 방법에 대한 몇 가지 방법을 보여줍니다.
std::map을 사용하여 C++에서 맵 컨테이너 객체 선언
    
std::map컨테이너는 키가 고유 한 정렬 된 키-값 쌍 데이터 구조를 구현합니다. 키 값은 기본적으로 오름차순으로 쌍 요소를 정렬합니다. 그러나 사용자는 선택적으로 비교 함수를std::map템플릿에 전달할 수 있습니다. 각 요소가 별도의 중괄호로 지정된 값 목록으로map개체를 초기화 할 수 있습니다. 이 경우string쌍의 맵을 만든 다음 그 요소를cout스트림에 인쇄합니다. 각 요소는for루프에서std::pair멤버로 액세스됩니다.
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  printMap(m1);
  return EXIT_SUCCESS;
}
출력:
11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;
find멤버 함수를 사용하여 C++에서 맵의 특정 요소 검색
find멤버 함수는map컨테이너에서 특정 키-값 쌍을 찾는 데 유용합니다. 조회 작업에는 로그 복잡성이 있습니다. find함수는 키에 대한 참조를 가져와 동등한 키가있는 요소에 대한 반복자를 리턴합니다. 주어진 키를 가진 요소가 발견되지 않으면 과거의 반복자가 반환됩니다.
다음 코드 스 니펫은 범위 반복기를 사용하여 새map컨테이너를 초기화하는 방법을 보여줍니다. 첫 번째 반복자는find함수를 사용하여 검색됩니다. 또한 반환 된 이터레이터의 유효성을 확인하는 것이 유용 할 수 있습니다.
#include <cassert>
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  std::map<string, string> m2(m1.find("11700K"), m1.end());
  printMap(m2);
  auto ret = m1.find("11700K");
  assert(ret != m1.end());
  std::map<string, string> m3(ret, m1.end());
  printMap(m3);
  return EXIT_SUCCESS;
}
출력:
11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;
insert멤버 함수를 사용하여 C++에서 맵에 요소 저장
insert멤버 함수를 사용하여 기존map컨테이너에 새 요소를 추가 할 수 있습니다. insert멤버는 추가 할 오브젝트에 대한 참조를 가져와 삽입 된 요소에 대한 반복자와 성공적인 삽입 상태를 나타내는bool값으로 구성된std::pair컨테이너를 리턴합니다. 삽입이 실패하면 반복기가 작업을 방해 한 요소를 가리 킵니다.
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  auto ret1 = m1.insert({"1390P", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }
  ret1 = m1.insert({"1390", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }
  printMap(m1);
  return EXIT_SUCCESS;
}
출력:
Not inserted!
11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390 : Xeon W;
1390P : Xeon W;
merge멤버 함수를 사용하여 C++에서 두 맵 오브젝트의 요소 결합
merge멤버 함수를 사용하여 두 개의 맵 컨테이너에서 요소를 결합 할 수 있습니다. 결합 된 요소를 저장해야하는map객체에서 호출되고 또 다른map을 인수로 취합니다. 작업 후 전송 된 요소에 대한 모든 포인터와 참조가 유효합니다.
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  std::map<string, string> m4 = {
      {"11900KF", "Core i9"}, {"11600T", "Core i5"}, {"11700K", "Core i7"}};
  m1.merge(m4);
  printMap(m1);
  return EXIT_SUCCESS;
}
출력:
11600K : Core i5;
11600T : Core i5;
11700K : Core i7;
11900K : Core i9;
11900KF : Core i9;
1390P : Xeon W;
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook