【问题标题】:How to access map of set of pairs elements?如何访问一组对元素的映射?
【发布时间】:2019-08-10 17:57:54
【问题描述】:
#include <iostream>
#include <map>
#include <set>
#include <utility>
int main()
{
    std::map<int,std::set<std::pair<int,int>>>map1;

    for(int i = 0; i != 3; ++i) 
        map1[i].insert({i+1,i+2});

    for(auto i : map1){

        std::cout<<i.first<<" ";

        pair<int,int> j = i.second;

        j.first<<" "<<j.second<<"\n";

    }
    return 0;
}          

错误:从 std::set > 转换为非标量类型 std::pair 请求对 j = i。第二;

【问题讨论】:

  • @rtpax 你能举个例子吗?

标签: c++ c++11 stl stdmap stdset


【解决方案1】:

i.secondstd::set,而不是内部std::pair

你应该这样做:

for(auto i : map1)
{
    std::cout<< i.first << " ";
    std::set<std::pair<int,int>> j = i.second;
    for (const auto& k : j)
    {
        std::cout << k.first<<" "<<k.second<<"\n";
    }
}

Demo

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 2011-07-05
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2017-05-13
    相关资源
    最近更新 更多