源自: 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++标准库中,`Map`被实现为`std::map`,但在这个场景中,我们讨论的是一个用户自定义的简易`Map`实现,名为"MyMap",这是在Visual Studio 2013环境下编写的。 自定义`Map`的主要目的是为了学习和理解数据结构的...
在C++编程中,`std::map` 是一个...这些基本操作展示了如何在C++中使用`std::map`进行数据的插入、查询和遍历。在实际应用中,`map`通常用于高效地存储和检索数据,其内部使用红黑树实现,保证了O(log n)的时间复杂度。
我们的实验环境是使用C++语言和Visual Studio开发环境。 程序源代码及运行结果 在第一个实验中,我们将定义一个整型数组和一个整型vector容器对象,分别包含若干个整型元素,然后输入一个整型值,调用STL中的find...
- 使用`<string>`和`<sstream>`库将每一行分割成单词,并检查这些单词是否匹配用户输入的目标单词。 - 如果找到目标单词,则记录该单词所在的文件路径及行号,并输出相关信息。 #### 代码解析 ```cpp // 溯実ļ·...
庖丁解牛(侯捷自序) i 目录 v 前言 xvii 本书定位 xvii 合适的读者 xviii 最佳阅读方式 xviii 我所选择的剖析对象 xix 各章主题 xx 编译工具 xx ...microsoft visual c++ 6.0 477 索引 481