【问题标题】:How to display list which is contained in map stl如何显示地图 stl 中包含的列表
【发布时间】:2015-02-03 11:34:24
【问题描述】:

在地图中,如果值本身是一个列表,那么我们如何显示它。

键:1 值:1,2,3(列表)

键:2 值:4,5,6(列表)

void somefunction()

{
    cout << "Key: " << pos->first << endl;
    cout << "Value:" << pos->second << endl;
}

但是如果pos->second包含一个List,如何显示呢?

我有一个存储所有目录及其对应文件的功能。

我在显示 VALUE 地图时遇到问题。我已经调试并观察到相应的值正在正确存储,但我无法显示它。由于 map 的 value 部分本身就是 pathiterator 的列表。每当我在不同的目录中获得相同的文件名时,我都会将引用或路径迭代器存储在对应于相同键(文件名)的映射中。

class FileMgr
{
    public:
    using path_1 = std::string;
    using paths_1 = std::set<path_1>;
    using pathiter = paths_1::iterator;
    using listofiter = std::list<pathiter>;
    using file = std::string;
    using store = std::map<file, listofiter>;
    using store_iter = store::iterator;
    paths_1 pp;
    pathiter oo;
    listofiter l1;
    store s6;
    store_iter d;
  void addPattern(const std::string& patt)
  {
    if (patterns_.size() == 1 && patterns_[0] == "*.*")
      patterns_.pop_back();
    patterns_.push_back(patt);
  }
  void search()
  {

      for (recursive_directory_iterator i("."), end; i != end; ++i)
      {
          pp.insert(i->path().parent_path());
          oo = pp.find(i->path().parent_path());

          if (!is_directory(i->path()))
          {
              s6[i->path().filename()].push_back(oo);
          }
      }
      for ( d = s6.begin(); d != s6.end(); d++)
      {
          cout << "Key: " << d->first << "  Value: ";
          std::cout << "Value:";
          for (auto& x : d->second)
              std::cout << ' ' << x;
                                //^^Red Squiggly here ERROR
          std::cout << '\n';

      }
  }
private:
  std::string path_;
  DataStore& store_;
  patterns patterns_;
};

【问题讨论】:

  • 您可以遍历列表并打印每个元素或重载运算符&lt;&lt; for decltype(pos-&gt;second),如果您在多个地方需要它。
  • Printing lists with commas C++ 的可能重复项
  • 您可以以与显示地图中未包含的列表完全相同的方式显示它。

标签: c++ file stl


【解决方案1】:

如果您的地图介于intstd::list&lt;int&gt; 之间,则

std::map<int, std::list<int>> map_list ;
int key ;
std::list<int> val = map_list[key] ;
for(int x : val)
    std::cout << x << " " ; // printing each element in list

将在key显示整个列表。

【讨论】:

    【解决方案2】:

    不要让地图方面分散您的注意力 - 您只需使用 pos-&gt;second,否则您会使用任何 list 对象 - 如下:

    std::cout << "Value:";
    for (auto& x : pos->second)
        std::cout << ' ' << x;
    std::cout << '\n';
    

    【讨论】:

    • 我在x得到红色波浪形
    • 我正在添加代码sn-p。请看看我哪里出错了。非常感谢!
    • 错误说:没有运算符
    • @tanz 你确定列表中包含整数吗?
    • @tanz 那么你为什么希望它显示类似1,2,3 的内容?您需要弄清楚您正在使用的类型。那么显示值应该是微不足道的。
    【解决方案3】:
    list<int> l;
    map<int,list<int> > m;
    .
    .
    .
    .
    list<int>::iterator i;
    map<int,list<int> > ::iterator mitr=m.begin();
    
    while(mitr!=m.end())
    {
       l=mitr->second;
       i=l.begin();
       while(i!=l.end())
       {
            cout<<*i<<" ";
            i++;
       } 
       mitr++;
    }
    

    这里的i 是用于迭代map 中每个list 的迭代器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2021-05-08
      • 2020-12-19
      • 1970-01-01
      • 2011-11-02
      • 2014-10-16
      • 1970-01-01
      相关资源
      最近更新 更多