---
title: "如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value"
author: "zybing"
date: "2013-03-25T15:55:07Z"
tags: []
categories: ["非技术"]
slug: "visual-c-map-end-map-find-map-insert-map-iterator-map-value"
draft: false
lastmod: "2013-03-25T15:55:07Z"
id: 1836224
url: "https://zybing.iteye.com/blog/1836224"
---

源自: http://support.microsoft.com/kb/157159/zh-cn

# 如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value\_type 标准模板库 (STL) 函数

文章编号: 157159 - [查看本文应用于的产品](http://support.microsoft.com/kb/157159/zh-cn#appliesto)

![机器翻译](http://support.microsoft.com/library/images/support/cn/mt_16.png)[查看机器翻译免责声明](http://support.microsoft.com/kb/157159/zh-cn#mtDisclaimer)

![逐句中英文参照视图](http://support.microsoft.com/library/images/support/en-us/MTKB-SbS_16.png)[点击这里查看逐句中英文对照机器翻译](http://www.microsofttranslator.com/BV.aspx?ref=CSSKB&lo=SS&from=en&to=zh-chs&a=http%3A%2F%2Fsupport.microsoft.com%2Fkb%2F157159%2Fen-us%3Ffr%3D1)

[]()展开全部 | []()关闭全部

## ![](http://support.microsoft.com/library/images/support/en-us/20x20_grey_plus.png)[]()本页

## ![](http://support.microsoft.com/library/images/support/en-us/20x20_grey_minus.png)[概要]()

下面的代码示例演示如何用 Visual C++ map::end、 map::find、 map::insert、 map::iterator 和 map::value\_type STL 的符号。

[![](http://support.microsoft.com/library/images/support/en-us/uparrow.gif)回到顶端](http://support.microsoft.com/kb/157159/zh-cn#top) | [提供反馈](http://support.microsoft.com/kb/157159/zh-cn#survey)

## ![](http://support.microsoft.com/library/images/support/en-us/20x20_grey_minus.png)[更多信息]()

### 所需的头文件

```
   <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 返回一个迭代器，指定的排序关键字的第一个元素等于键。如果没有这样的元素存在，则迭代器等于 end()。

如果该项不存在，插入将添加到序列并返回对 &lt; 迭代器，真 &gt;。如果此键已存在，则插入不会添加到序列中的并返回对 &lt; 迭代器，假 &gt;。

下面的示例创建一个映射的为字符串的整数。在这种情况下，该映射是从数字为对应的字符串 （1-&gt;"One"，2 &gt;"2"，等等。）。

程序读取用户的数量、 查找单词等效的 （使用地图），每一位并打印为词的一系列数字的背后。例如，如果用户输入 25463，该程序使用响应： 两个五四个六三。

### 示例代码

```
////////////////////////////////////////////////////////////////////// 
// 
// Compile options needed: None
// 
// <filename> :  main.cpp
// 
// Functions:
// 
//      end
//      find
//      insert
// 
// Written by Rick Troemel
// 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
				
```

[![](http://support.microsoft.com/library/images/support/en-us/uparrow.gif)回到顶端](http://support.microsoft.com/kb/157159/zh-cn#top) | [提供反馈](http://support.microsoft.com/kb/157159/zh-cn#survey)

## ![](http://support.microsoft.com/library/images/support/en-us/20x20_grey_minus.png)[参考]()

有关 map::end，map::find 和 map::insert 相同的信息，请访问下面的 MSDN Web 站点：  
[http://msdn.microsoft.com/en-us/library/wwcahb6y.aspx](http://msdn.microsoft.com/en-us/library/wwcahb6y.aspx)