如何访问嵌套在 stl 向量中的 pair 内的 pair 元素

tri*_*der -4 c++ stl

我有一个这样的向量:

vector < pair < int, pair < int,int > > > v
Run Code Online (Sandbox Code Playgroud)

我想访问所有三个元素。我怎样才能通过迭代器做到这一点?我在下面将迭代器声明为 it1 和 it2:

#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;
}
Run Code Online (Sandbox Code Playgroud)

mol*_*ilo 5

遵循类型。

如果it是一个迭代器

vector<pair<int, pair<int, int>>> 
Run Code Online (Sandbox Code Playgroud)

然后*it是一个

pair<int, pair<int, int>>  
Run Code Online (Sandbox Code Playgroud)

所以it->first(又名(*it).first)是一个int,并且it->second是一个pair<int,int>

这意味着您的元素是

it->first
it->second.first
it->second.second
Run Code Online (Sandbox Code Playgroud)