tanz
tanz

Reputation: 647

How to display values contained in LIST in a MAP stl: C++

Image

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). Kindly look into it.

void search()
      {
          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;
          store s6;
          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);
              }
          }
          store_iter d;
          for ( d = s6.begin(); d != s6.end(); d++)
          {
              cout << "Key: " << d->first << "  Value: "<<d->second;
                                                         // ^^not able 
//to print it!! 
    
          }
   }
        

I have added screenshot that I took while debugging. If the same file is present at different location the d->second {size} will be more than 1.

I thought of applying loop: Here is what I did. I think I messed it up.

  for ( d = s6.begin(); d != s6.end(); d++)
          {
              cout << "Key: " << d->first << "  Value: ";
              for (int i = 0; i < d->second.size; i++)
              {
                  cout << d->second[i];
                                 //  ^^Error at this place
              } 
          }

ERROR: while using loop in a loop

enter image description here

Upvotes: 1

Views: 144

Answers (1)

MSalters
MSalters

Reputation: 179991

<< d->second does not work because that is a collection.

You already knew one way to display a collection, using a for( ; ; ) loop. Since you have a container inside a container, you would need a for-loop inside a for-loop to print all elements.

As a bonus, here's how you can write more compact loops:

for (auto&& element : container) { ...

No need for iterators, begin and end calls. That's all handled for you. The body of the loop is executed once for each element in the container. Same restrictions apply: don't try to add or remove elements while inside the loop.

[edit] You added a new, third kind of for-loop. For some reason you now tried to use for(int i = 0; i != container.size; ++i) to iterate over a container. Why? You already know one way that works, and I just showed you another way that works.

BTW, the reason that it doesn't work is that [i] requires operator[], which is not present in most containers. begin/end is present in all containers.

Upvotes: 1

Related Questions