带方括号的for循环

时间:2021-06-10 02:22:07

标签: c++ for-loop c++11 c++17

        map<int, string> m = { {1,string("abc")},{2,string("ttt")}, };
        for (auto&[a, b] : m)
        {
            cout << a << b << endl;
        }

我知道另一种样式 for (auto &iter : m),iter 是每个元素的对。但在这个 auto&[a, b] 中,我是第一次看到它。谁能告诉我用法?

编辑:我的问题是结构化绑定——正如 Eljay 提示的那样,不一定是 std::map 循环。

0 个答案:

没有答案