-3

Why is the following code not inserting elements in the map<int, set<int> > ?

(The size of all the sets after executing the following code is 0.)

I am creating an adjacency list in this map.

map<int, set<int> > m;
cin>>n;
while(n--)
{
    cin>>t;
    int i=0;
    while(t--)
    {
        scanf("%d",&x);
        set<int> s;
        m[x] = s;
        if(i != 0)
            m[prev].insert(x);
        prev = x;
    }
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
piXel_pRo
  • 159
  • 2
  • 14

2 Answers2

2

You aren't ever changing i so i == 0 when it does the check if(i != 0).

Jimi
  • 901
  • 7
  • 11
1

Your variable 'i' is always 0.
And you are only inserting elements into the set if 'i' is != 0