-2

In a map if the values is itself a list then how can we display it.

key: 1 value: 1,2,3 (list)

key: 2 value: 4,5,6 (list)

void somefunction()

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

But if pos->second contains a List, how to display it?

I have a function where I am storing all the directories and its corresponding files.

I am facing problem in displaying the of VALUE the map. I have debugged and observed that the corresponding values are getting stored properly but I am not able to display it. Since value part of map is itself a list of pathiterator. Whenever I am getting same filename at different directory I am storing the reference or pathiterator in the map corresponding to the same key(file name).

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_;
};
tanz
  • 627
  • 1
  • 10
  • 21
  • You can loop over the list and print each element or overload operator `<<` for `decltype(pos->second)` to do this if you need it in more than one place. – 5gon12eder Feb 03 '15 at 11:35
  • 1
    possible duplicate of [Printing lists with commas C++](http://stackoverflow.com/questions/3496982/printing-lists-with-commas-c) – 5gon12eder Feb 03 '15 at 11:37
  • 3
    You can display it in exactly the same way as you would display a list that isn't contained in a map. – eerorika Feb 03 '15 at 11:39

3 Answers3

0

If your map is between an int and std::list<int> then,

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

will display the whole list at key.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

Don't let the map aspect distract you - you just use pos->second where you'd otherwise use any list object - as follows:

std::cout << "Value:";
for (auto& x : pos->second)
    std::cout << ' ' << x;
std::cout << '\n';
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • I get red squiggly at `x` – tanz Feb 03 '15 at 11:49
  • I am adding the code snippet. Please see where I am going wrong. Many thanks! – tanz Feb 03 '15 at 11:49
  • error says: No operator << matches these arguments. – tanz Feb 03 '15 at 11:56
  • @tanz are you sure that the list contains integers? – eerorika Feb 03 '15 at 12:08
  • @tanz then why would you expect it to display something like `1,2,3 `? You need to figure out the type that you're using. Then it should be trivial to display the value. – eerorika Feb 03 '15 at 12:20
  • @tanz Firstly, a "squiggly" at x suggests your compiler might not support C++11, or have it enabled. Consider upgrading or enabling support. Regarding displaying file paths, if they're not already stored as a streamable type (e.g. `std::string` or `const char*`) then you should write your own `operator<<` streaming operator for the class in question - plenty of questions illustrating how to do that. – Tony Delroy Feb 03 '15 at 17:12
0
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++;
}

Here i is the iterator used to iterate over each list in map

Thiyagu
  • 17,362
  • 5
  • 42
  • 79