1

I am want to iterate over set here map<int,map<int,set<int>>> mep;

Getting error (base operator of '->' has no pointer type) in this way

for(auto p : mep){
  vector<int> temp;
  auto s = p->second->second;//getting here here
  for(auto it : s){
    temp.push_back(it);
  }
  result.push_back(temp);
}
BiagioF
  • 9,368
  • 2
  • 26
  • 50
user121
  • 29
  • 1
  • p->second gives you the map, not the set in the second map, and there is no second to map this is not iterator – alon Jul 02 '19 at 10:38
  • but second is a map in that second ,second is a set – user121 Jul 02 '19 at 10:41
  • but second is a map in that second(Yes), second is a set No you get map object not an iterator object so don't have ->seconed when you have map object you need to have new iterator over this map object – alon Jul 02 '19 at 10:44
  • You can tell from the type that `p->second` is a `map>`; the value type of `mep`. It is not a pointer, nor does it have an overloaded `->`. (Your code treats `mep` like it were a `map>>`.) – molbdnilo Jul 02 '19 at 11:07

3 Answers3

1

You have three nested containers. You should expect to have three nested for-loops:

std::vector<int> temp;

for (const auto& p1 : mep) {
  for (const auto& p2 : p1.second) {
    for (const int n : p2.second) {
      temp.push_back(n);
    }
  }
}

Notes:

  • Use const auto& (reference) in the loop. Without reference, you are going to create copies.
  • Try to use a better naming convention for the loop variables (i.e., p1, p2, n); in accordance with your context.
  • using namespace std; is considered bad practise. The reasoning is explained here.
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • can you please explain your 1st point how am i using copy here – user121 Jul 02 '19 at 10:49
  • There is much material on the internet about this topic. Anyway, the declaration `auto x = y` creates a *copy* of `y` into a new variable `x`. The same happens in *loop*. I.e., `for (auto p : mep)` will make `p` to be a copy of the object iterated over `mep`. Sometimes this copy is negligible, but I would keep `&` as a general good practice. – BiagioF Jul 02 '19 at 10:54
  • when to use `auto`, `auto&` or `const auto&` is neatly explained [here](https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const/15176127#15176127). – Stack Danny Jul 02 '19 at 10:58
  • But I am getting error after making changed in my code as mentioned – user121 Jul 02 '19 at 11:01
  • can you tell me what is the time complexity of above program – user121 Jul 02 '19 at 11:06
0
for(auto p : mep){
vector<int> temp;
auto s = p->second;
for(auto k : s){
   auto ss = k->second;
    for(auto it : ss)
   temp.push_back(it);
}
result.push_back(temp);
}

p->second gives you the map object not an iterator object so don't have ->seconed, you have a map object you need to have new iterator over this map object

alon
  • 220
  • 1
  • 16
0

Below is the same code which sets and prints the value of set for your map datastructure,

set<int> sv = { 1,2,3,4,5};

map<int, set<int>> ms = {{2,sv}, {1,sv}};

map<int, map<int,set<int>>> mep = {{1,ms}};

for (auto i:mep) {     // Outer map iteration
    for (auto j:i.second) {  // Inner map iteration
        for (auto s:j.second) {  // Set iteration
            cout<<"Set:"<< s;
        }
    }
}

Based on your need you can modify this.

Saravanan
  • 1,270
  • 1
  • 8
  • 15
  • can you tell me the time complexity – user121 Jul 02 '19 at 11:04
  • As per the example data provided 1*2*5. In general mep.size()*ms.size()*sv.size(). Example for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { sequence of statements } } . Time complexity is N*M. – Saravanan Jul 02 '19 at 11:37