이 문서에서는 Visual C++에서 map::end
map::iterator
map::find
map::insert
map::value_type
STL(표준 템플릿 라이브러리) 기호를 사용하는 방법을 보여 줍니다.
원래 제품 버전: Visual C++
원래 KB 번호: 157159
필수 헤더
<map>
프로토타입
iterator map::end();
// Key is the data type of template argument #1 for map
iterator map::find(const Key& key);
pair<iterator, bool> map::insert(const value_type& x);
참고 항목
프로토타입의 클래스/매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.
설명
이 함수는 end()
시퀀스의 끝을 지나 하나를 가리키는 반복기를 반환합니다.
Find는 정렬 키가 같은 첫 번째 요소를 선택하는 반복기를 반환합니다 key
. 이러한 요소가 없으면 반복기가 같습니다 end()
.
키가 아직 없는 insert
경우 해당 키를 시퀀스에 추가하고 반환 pair<iterator, true>
합니다. 키가 이미 있는 insert
경우 시퀀스에 추가하지 않고 반환합니다 pair <iterator, false>
.
다음 샘플에서는 문자열에 대한 ints의 맵을 만듭니다. 이 경우 매핑은 숫자에서 해당 문자열 등가물(1 -> 1, 2 -> 2 등)으로 구성됩니다.
프로그램은 사용자로부터 숫자를 읽고, 각 숫자에 해당하는 단어(지도 사용)를 찾고, 숫자를 다시 일련의 단어로 출력합니다. 예를 들어 사용자가 25463을 입력하면 프로그램에서 다음으로 응답합니다.
샘플 코드
//////////////////////////////////////////////////////////////////////
// Compile options needed: None
// <filename> : main.cpp
// Functions:
// end
// find
// insert
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
// disable warning C4018: '<' : signed/unsigned mismatch
// okay to ignore
#pragma warning(disable: 4018)
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string, less<int>, allocator<string> > INT2STRING;
void main()
{
// 1. Create a map of ints to strings
INT2STRING theMap;
INT2STRING::iterator theIterator;
string theString = "";
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;
}
}
프로그램 출력:
Enter "q" to quit, or enter a Number: 22
Two Two
Enter "q" to quit, or enter a Number: 33
Three Three
Enter "q" to quit, or enter a Number: 456
Four Five Six
Enter "q" to quit, or enter a Number: q
참조
에 map::insert
대한 map::find
map::end
동일한 정보를 보려면 map::insert, map::find 및 map::end를 방문하세요.