如何遍历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
}