如何插入std :: map? [英] How to insert into std::map?

查看:66
本文介绍了如何插入std :: map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个std迭代器,我可以使用std算法(例如std :: copy)将元素插入到std :: map中?

Is there a std iterator I could use to insert elements into std::map using a std algorithm (for example std::copy) ?

我需要一个容器来将一个对象链接到字符串,并且我考虑使用std :: map.有没有更好的容器?忘了说-项目需要排序.

I need a container to link one object to a string, and I thought about using std::map. Is there a better container? Forgot to say - items needs to be sorted.

推荐答案

我认为OP正在寻找的是 std :: inserter(mymap,mymap.end())

I think what the OP is looking for is std::inserter(mymap, mymap.end())

因此您可以这样做:

std::copy( inp.begin(), inp.end(), std::inserter(mymap, mymap.end()) );

输入类型必须是您的地图采用的对类型,否则您的算法将必须是带有功能/函数的 std :: transform ,以将输入类型转换为这样的std :: pair .

The input types must be a pair type that your map takes, otherwise your algorithm would need to be std::transform with a function/functor to convert the input type into such a std::pair.

inserter实际上不是迭代器,而是生成迭代器的模板化函数( std :: insert_iterator ,它是模板化类型,但该类型在函数调用中自动解析).

inserter is not actually an iterator but a templated function that produces an iterator (std::insert_iterator, which is a templated type but the type is automatically resolved in the function call).

这篇关于如何插入std :: map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆