源自: 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
相关推荐
C++使用iterator的例子
大家都知道map容器是C++ STL中的重要一员,平时会遇到删除map容器中value为指定元素的问题,例如删除所有字符串为”123″或者能整除3的元素。 一、map容器下的方法说明 由于map容器下的方法较多,这里只列举代码中用...
Map a = new HashMap(); //方法一 Iterator it = a.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); System.out.println(pairs.getValue()); } //以下方法需要jdk5以上...
Map接口定义了四种类型的方法,每个Map都包含这些方法。...remove(Object key) 从Map中删除键和关联的值。 put(object key,Object value) 将指定值与指定键相关联。 clear() 从Map虽删除所有映射。
Collection、Map、List、Set、Iterator
// java中对象容器主要有Set,List和Map三个接口类。 // 迭代器(Iterator)模式,又叫做游标(Cursor)模式。 // GOF给出的定义为:提供一种方法访问一个容器...另包含一篇网文:在java中使用TreeMap进行中文排序
STL hash_map: 链式散列 版权所有 (c) 2014,龙 (Ryan) 南宫...iterator insert (const Key& key, const Value& value); iterator find (const Key& key); void erase (iterator pos); Value operator[] (const Key&
1、map简介 map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。 2、map的功能 自动建立Key - ...
NULL 博文链接:https://topbox163.iteye.com/blog/669084
js中使用map形式存取遍历
迭代器模式(Iterator)C++实现
<s:iterator value="list" id="book" status="u"> <s:property value="id" /> <s:property value="name" /> <s:...
计算部分的数量 前提, 目标, 结果 前提: 学生需要掌握以下机能 • 树- 了解树的表示方法 • Map容器- 了解如何日用标准库中的map容器以及迭代器 • 递归- 了解如何构造一个递归的解决方案去解决一个问题
NULL 博文链接:https://takeme.iteye.com/blog/1663239
23种设计模式之二十二(行为模式)Iterator模式
NULL 博文链接:https://shupili141005.iteye.com/blog/667446
CUJ:标准库:定义iterator和const iterator
废话不多说,先看下面一段代码 #include #include <map> ...map.insert(std::make_pair(i, s)); } void deleteMap() { for (myMap::iterator it = map.begin(); it != map.end(); ++it) { map.erase(it->first)
一个iterator_jsonDemo. 1.通过iterable接口和iterator实现迭代输出。2.通过JSONArray实现输出(自带json相关jar包)。供参考。
Iterator接口在集合中的使用方法!