English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ map emplace_hint() 함수 사용법 및 예제

C++ STL map(컨테이너)

C ++ map emplace_hint()함수는 새로운 요소를 컨테이너에 추가하여 map 컨테이너를 확장합니다. 요소는 직접 생성됩니다. 복사나 이동은 없습니다.

함수에 전달된 매개변수 args를 통해 요소의 생성자를 호출하여 요소를 추가합니다. 키가 존재하지 않을 때만 추가됩니다.

문법

template<class... Args>
    iterator emplace_hint(const_iterator position, Args&&...args);  //C에서++ 11 시작

파라미터

args: 맵에 삽입할 요소를 생성하는 파라미터를 전달합니다.

위치: 새로 삽입할 요소 위치를 제시합니다.

반환 값

이는 새로 삽입된 요소로 이터너토를 반환합니다. 요소가 이미 존재하면 삽입 실패하고, 기존 요소로 이터너토를 반환합니다.

예시1

맵에 요소를 삽입하는 간단한 예제를 보겠습니다.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void) {
   map<char, int> m = {
            {'b', 20},
            {'c', 30},
            {'d', 40},
            });
   m.emplace_hint(m.end(), 'e', 50);
   m.emplace_hint(m.begin(), 'a', 10);
  cout << "맵은 다음 요소를 포함하고 있습니다" << endl;
  for (auto it = m.begin(); it != m.end(); ++it){
      cout << it->first << " = " << it->second << endl;
  }
  
   return 0;
}

출력:

맵은 다음 요소를 포함하고 있습니다
a = 10
b = 20
c = 30
d = 40
e = 50

위의 예제에서는 주어진 키-값 쌍과 위치를 맵 m에 삽입합니다.

예시2

이제 간단한 예제를 보겠습니다.

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;
template<typename M> void print(const M& m) {
    cout << m.size() << " 개의 요소: " << endl;
    for (const auto& p : m) {
        cout << "( " << p.first << " , " << p.second << " ) ";
    }
    cout << endl;
}
int main()
{
    map<string, string> m1;
    m1.emplace("람", "회계");
    m1.emplace("라케이시", "회계");
    m1.emplace("선일", "공학");
    cout << "맵 시작 데이터: ";
    print(m1);
    cout << endl;
    m1.emplace_hint(m1.end(), "Deep", "공학");
    cout << "맵이 수정되었습니다. 지금은 포함하고 있습니다: ";
    print(m1);
    cout << endl;
}

출력:

맵 시작 데이터: 3 요소:
(라케이시, 회계) (람, 회계) (선일, 공학)
map이 변경되었습니다. 지금은 이렇게 포함됩니다]} 4 요소:
(Deep,Engineering) (Rakesh,Accounting) (Ram,Accounting) (Sunil,Engineering)

예시3

위치를 지정한 map에 요소를 삽입하는 간단한 예제를 보겠습니다.

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char, int> mymap;
  auto it = mymap.end();
  it = mymap.emplace_hint(it, 'b',10);
  mymap.emplace_hint(it, 'a',12);
  mymap.emplace_hint(mymap.end(), 'c',14);
  cout << "mymap이 포함합니다:";
  for (auto& x: mymap){
    cout << " [" << x.first << ':' << x.second << "]";
    cout << '\n';  
  }
  return 0;
}

출력:

mymap contains: [a:12] [b:10] [c:14]

예시4

요소를 삽입하는 간단한 예제를 보겠습니다.

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
 typedef map<string, int> city;  
   string name;
   int age;
   city fmly;
   int n;
  cout << "가족 구성원 수를 입력하세요: ";
  cin >> n;
  cout << "각 구성원의 이름과 나이를 입력하세요: \n";
   for(int i = 0; i < n; i++)
   {
       cin >> name;
       cin >> age; 
       fmly.emplace_hint(fmly.begin(), name, age);
       
   }
   
      cout << "\n가족 총 구성원은:" << fmly.size();
      cout << "\n가족 구성원의 상세 정보: \n";
      cout << "\nName | Age \n __________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p != fmly.end(); p++)
      {
          cout << (*p).first << " " << "|" << (*p).second <<" \n ";
      }
    
   return 0;
}

출력:

가족 구성원 수를 입력하세요 : 3
각 구성원의 이름과 나이를 입력하세요: 
Ram 42
Sita 37
Laxman 40
가족 총 구성원은:3
가족 구성원의 상세 정보: 
Name    |  Age 
__________________________
Laxman | 40 
Ram      | 42 
Sita       | 37

위의 예제에서는 사용자 선택에 따라 요소를 map의 시작 부분에 삽입합니다.

C++ STL map(컨테이너)