map::insert, map::find, 및 map::end

사용 하는 방법을 보여 줍니다 있는 map::insert, map::find, 및 map::end Visual C++에서 표준 템플릿 라이브러리 (STL) 함수입니다.

iterator map::end( ); 
iterator map::find(
   const Key& Key
);
pair<iterator, bool> 
map::insert(
   const value_type& x
);

설명

[!참고]

프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

해당 최종 함수 시퀀스 끝 지 나 하나를 가리키는 반복기를 반환 합니다.찾기 정렬 키가 있는 첫 번째 요소를 지정 하는 반복기를 반환 합니다. .이러한 요소가 있으면 반복기 같음 최종.키가 아직 없는 경우 삽입 시퀀스에 추가 하 고 반환 pair<반복기, true>.키가 이미 있는 경우 삽입 에 시퀀스를 추가 하 고 반환 pair <반복기, false>.다음 샘플 맵을 만듭니다 ints 문자열s.이 경우 매핑을 자리에서에 상응 하는 문자열을 나타냅니다 (1-> "하나", 2-> "2", 등).프로그램이 여러 사용자 로부터, (지도 사용) 각 자리에 해당 하는 단어를 찾습니다 읽고 숫자 뒤 일련의 단어와 출력 합니다.25463 사용자가 입력 하는 경우 예를 들어 프로그램이 응답: 두 5 개 네 6 세.

예제

// map_insert_find_end.cpp
// compile with: /EHsc
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>

using namespace std;

typedef map<int, string, less<int> > INT2STRING;

int main()
{
   // 1. Create a map of ints to strings
   INT2STRING theMap;
   INT2STRING::iterator theIterator;
   string theString = "";
   unsigned int index;

   // Fill it with the digits 0 - 9, each mapped to its string counterpart
   // Note: value_type is a pair for maps...
   theMap.insert(INT2STRING::value_type(0,"Zero"));
   theMap.insert(INT2STRING::value_type(1,"One"));
   theMap.insert(INT2STRING::value_type(2,"Two"));
   theMap.insert(INT2STRING::value_type(3,"Three"));
   theMap.insert(INT2STRING::value_type(4,"Four"));
   theMap.insert(INT2STRING::value_type(5,"Five"));
   theMap.insert(INT2STRING::value_type(6,"Six"));
   theMap.insert(INT2STRING::value_type(7,"Seven"));
   theMap.insert(INT2STRING::value_type(8,"Eight"));
   theMap.insert(INT2STRING::value_type(9,"Nine"));

   // Read a Number from the user and print it back as words
   for( ; ; )
   {
      cout << "Enter \"q\" to quit, or enter a Number: ";
      cin >> theString;
      if (theString == "q")
         break;

      // extract each digit from the string, find its corresponding
      // entry in the map (the word equivalent) and print it
      for (index = 0; index < theString.length(); index++)
      {
         theIterator = theMap.find(theString[index] - '0');
         if (theIterator != theMap.end() )   // is 0 - 9
            cout << (*theIterator).second << " ";
         else    // some character other than 0 - 9
            cout << "[err] ";
      }
      cout << endl;
   }
}

입력

12
q

샘플 출력

Enter "q" to quit, or enter a Number: 12
One Two
Enter "q" to quit, or enter a Number: q

요구 사항

헤더: <map>

참고 항목

개념

표준 템플릿 라이브러리 샘플