如何遍历std :: map< string,int>和std :: vector< int>使用单for循环? [英] How to iterate through a std::map<string,int> and std::vector<int> using single for loop?

查看:131
本文介绍了如何遍历std :: map< string,int>和std :: vector< int>使用单for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用单个for循环遍历std::map<string,int>std::vector<int>?

How to iterate through a std::map<string,int> and std::vector<int> using single for loop ?

我已经看到了这个问题,但无法解决我的问题.

I have seen this questions but could not solve my problem.

我正在尝试这样

map<string,int> data;
data["Shravan"] = 1;
data["Mama"] = 2;
data["Sa1"] = 3;
data["Jhandu"] = 4;

vector<int> values = {1,2,3,4};

for(const auto& it1: data,it2 : values) {
    // Do something 
}

编辑:我无法一一列举.因为我在同一函数中使用std::map的键和std::vector的值.将在for循环内调用.

Edit : I can not go through one by one. Because i am using the key of std::map and value of std::vector in the same function. Which will be called inside for loop.

两个相同大小的容器.

Both the container of same size.

推荐答案

如果您知道两者的长度相同,请使用类似以下内容的

If you know there's both the same length, use something like:

auto vit = begin(value);
auto mit = begin(data);
for (; vit != end(value); ++mit, ++vit) {
    // Use mit and vit
}

这篇关于如何遍历std :: map&lt; string,int&gt;和std :: vector&lt; int&gt;使用单for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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