使用map的find头文件_在 Visual c + + 中使用 map STL 函数 - Visual C++ | Microsoft Docs

在 Visual c + + 中使用 map:: end,map:: find,map:: insert,map:: iterator,and map:: value_type STL 函数

04/24/2020

本文内容

本文说明如何 map::end map::find map::insert map::iterator map::value_type 在 Visual c + + 中使用、、、和标准模板库(STL)符号。

原始产品版本:  Visual c + +

原始 KB 数:  157159

必需标头

原型

iterator map::end();

// Key is the data type of template argument #1 for map

iterator map::find(const Key& key);

pair map::insert(const value_type& x);

备注

原型中的类/参数名称可能与头文件中的版本不匹配。 已对某些部分进行了修改以提高可读性。

说明

end()函数返回一个在序列末尾之后的迭代器。

Find 返回一个迭代器,它选择其排序关键字等于的第一个元素 key 。 如果不存在此类元素,则迭代器等于 end() 。

如果该项尚不存在,则 insert 会将其添加到序列中并返回 pair 。 如果该项已存在, insert 则不会将其添加到序列中,也不会返回 pair 。

下面的示例创建一个从 int 到字符串的映射。 在这种情况下,映射是从数字到它们的字符串等效项(1-> 一个,2-> 两个,依此类推。)。

程序从用户处读取号码,查找每个数字的等效词(使用 map),并将该数字作为一系列单词打印回来。 例如,如果用户输入25463,程序将响应: 2 5 4 6 3。

示例代码

//

// Compile options needed: None

// : main.cpp

// Functions:

// end

// find

// insert

// of Microsoft Product Support Services,

// Copyright (c) 1996 Microsoft Corporation. All rights reserved.

//

// disable warning C4018: '

// okay to ignore

#pragma warning(disable: 4018)

#pragma warning(disable:4786)

#include

#include

#include

using namespace std;

typedef map, allocator > 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 参阅和、访问map:: insert、map:: find 和 map:: end。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值