【问题标题】:how to print std::set of std::maps如何打印 std::set 的 std::maps
【发布时间】:2023-03-12 04:50:01
【问题描述】:

这是带有字符串键和结构值的映射 1.首先,我正在创建一个整数映射和一个结构作为值 std::map<int,struct value>; 然后我将所有这些地图对象添加到一个集合中 std::set<std::map<int,struct value>> 我想了解如何循环遍历这个集合 我无法访问该集合中的地图,请建议

struct values
{
    std::string a;
    std::string b;
    values():a("milepost"),b("dummyval"){};
    values( std::string ab, std::string bc)
    {
        a=ab;
        b=bc;
    };

    bool operator<(const values& other) const {
        return (a< other.a && b < other.b) ;
    }
    friend std::ostream& operator<<(std::ostream& os, const values& val);

};

std::ostream& operator<< (std::ostream& os , const values& val)
{
  os << val.a <<"\t"<< val.b;
  return os;

}

typedef std::map<std::string,values>  myWsData;

main() 
{
    values a;

    myWsData et_Data1,pt_Data2;

    et_Data2.insert(std::make_pair("780256", a));

    pt_Data2.insert(std::make_pair("780256", a));

    std::set<myWsData> myet_pt_data;

    myet_pt_data.insert(et_Data1);

    myet_pt_data.insert(pt_Data2);

    for (auto &i:myet_pt_data)
    {
        std::cout<<i<<"\n";
    }
}

【问题讨论】:

  • Iam not able to access the maps that are the part of this set,请详细说明....您的问题是什么:编译?运行?你有什么经历让你觉得它不起作用?
  • 你会遇到两个地图的小于比较的问题,不是吗?你真正想解决什么问题?
  • @RichardHodges 我认为(因为他还是个学习者)我们应该补充一点,小于比较是必要的,因为std:set 被实现为 sorted 设置。因此,我们需要定义此代码缺少的元素的顺序。
  • @Christoph :这是否意味着 std::set 中的模板值需要有一个比较运算符,除非它是一个 pod
  • @LearningCpp std::set的完整模板是template&lt; class Key, class Compare = std::less&lt;Key&gt;, class Allocator = std::allocator&lt;Key&gt; &gt; class set;。因此,std::less&lt;Key&gt; 对您的类型有效(例如,默认情况下或因为您已定义它),或者您必须使用定义元素顺序的比较器覆盖 Compare

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


【解决方案1】:

你必须使用两个循环,像这样:

for (auto const& it1 : myet_pt_data)
{
    for (auto const& it2 : it1)
    {
       std::cout << it2.first << '\t' << it2.second << std::endl;
    }
}

当您在range-based for loops 中使用auto const&amp; 时,您可以避免复制集合及其所有内容。 类型如下:

  • decltype(it1)std::map&lt;std::string,values&gt; const&amp;,并且
  • decltype(it2)std::pair&lt;std::string const,values&gt; const&amp;

为了完整起见,请注意it2std::pair)中的std::string 是常量。

【讨论】:

    【解决方案2】:

    这个怎么样:

    #include <iostream>
    #include <map>
    #include <set>
    #include <algorithm>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        set<map<int,string> > list;
        //fill list
        std::for_each(list.begin(), list.end(), [](auto set_el){
            std::for_each(set_el.begin(),set_el.end(),[](auto map_el) {
                std::cout<<map_el.first<<"\t"<<map_el.second<<std::endl;
            });
        });
        cout << "Hello World!" << endl;
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      您可能缺少一个内部循环:

      // iterates through all elements of the set:
      for (const auto& the_map : myet_pt_data)
      {
          // the_map takes all values from the set
          // the_map actual type is const std::map<std::string,values>&
          for (const auto& the_value : the_map)
          {
             // the_value takes all value of the current map (the_map)
             // the_value actual type is const std::pair<std::string,values>&
             std::cout << the_value.first << " value is " << the_value.second << std::endl;
          }
      }
      

      【讨论】:

      • 您可能应该研究如何在不创建临时对象的情况下迭代集合。
      • @RustyX:对,太快了。更正了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2018-09-26
      • 2015-08-01
      • 2012-10-03
      相关资源
      最近更新 更多