dimanche 2 mai 2021

List of Pairs iterator in c++

#include <bits/stdc++.h>
using namespace std;

int main() {
    unordered_map< int,list<pair<int,int>>> adjList;
    adjList[1].push_back(make_pair(2,2));
    adjList[1].push_back(make_pair(4,-1));
    adjList[0].push_back(make_pair(1,3));
    for(pair<int,int> neighbour : adjList[1]){
        pair<int,int>* it = &neighbour;
       std::advance (it,1);
       cout<<it->first<<" ";    //1)showing random value twice instead of 4(-1113715584 -1113715584) 
    }
    for (list<pair<int,int>>::iterator i=adjList[1].begin(); i!=adjList[1].end(); i++)
        cout<<*i.first;         //2)list<std::pair<int, int> >::iterator'has no member named 'first'
    }
}
  1. it is showing correct value(2,4) without {std::advance (it,1);} which is supposed to tajke iterator to next head but it is showing some random value twice everytime.
  2. error : 'std::__cxx11::list<std::pair<int, int> >::iterator' {aka 'struct std::_List_iterator<std::pair<int, int> >'} has no member named 'first' cout<<*i.first;

Aucun commentaire:

Enregistrer un commentaire