简体   繁体   English

在地图中查找键值

[英]Finding key value in map

I am using an iterator in a for loop to traverse a map that has keys of type int. 我在for循环中使用迭代器遍历具有int类型键的映射。 How would I be able to access the value of the key at the specific iteration, so that I can use it later? 如何在特定的迭代中访问键的值,以便以后使用?

Dereferencing the iterator gives you a key-value pair, std::pair<key_type, mapped_type> . 取消引用迭代器会为您提供一个键值对, std::pair<key_type, mapped_type>

So to get the key from the iterator, get the first element of the pair: 因此,要从迭代器获取密钥,请获取该对的第一个元素:

key_type key = it->first;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM