`
zybing
  • 浏览: 445064 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value

    博客分类:
  • c++
阅读更多

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

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

文章编号: 157159 - 查看本文应用于的产品
展开全部 | 关闭全部

本页

概要

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

更多信息

所需的头文件

   <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()。

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

下面的示例创建一个映射的为字符串的整数。在这种情况下,该映射是从数字为对应的字符串 (1->"One",2 >"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
				

参考

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics