我是靠谱客的博主 干净苗条,这篇文章主要介绍如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value...如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value_type 标准模板库 (STL) 函数,现在分享给大家,希望可以做个参考。
源自: 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
最后
以上就是干净苗条最近收集整理的关于如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value...如何在 Visual C++ 中使用的 map::end、 map::find、 map::insert、 map::iterator 和 map::value_type 标准模板库 (STL) 函数的全部内容,更多相关如何在内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复