c++ - How to access pair elements nested inside pair in a vector in stl -
i have vector :
vector < pair < int, pair < int,int > > > v
i want access 3 elements . how can through iterator? have declared iterator it1 , it2 below :
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { vector<pair<int,pair<int,int> > > v; int n,a,b,i; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); v.push_back(make_pair(b,make_pair(a,i+1))); } sort(v.begin(),v.end()); vector<pair<int,pair<int,int> > > :: iterator it1=v.begin(); vector<pair<int,pair<int,int> > > :: iterator it2=v.begin()+1; printf("%d ",(it1->first)->second); while(it2!=v.end()) { if(it2->first.first>it1.first) { printf("%d ",it2.first.second); it1=it2; } it2++; } } return 0; }
follow types.
if it
iterator on
vector<pair<int, pair<int, int>>>
then *it
pair<int, pair<int, int>>
so it->first
(a.k.a. (*it).first
) int
, , it->second
pair<int,int>
.
this means elements are
it->first it->second.first it->second.second
Comments
Post a Comment