You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何访问C++中嵌套pair结构的vector内的指定元素?

访问元素3的方法

你可以通过逐层访问pair的first和second成员来获取元素3,具体代码示例如下:

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

int main()
{
    vector<pair<pair<pair<int,int>,int>,int>>v;
    
    v.push_back({{{1,3},4},5});
    v.push_back({{{2,4},6},7});
    
    // 访问并输出元素3
    int target = v[0].first.first.second;
    cout << target << endl; // 输出结果为3
}

结构拆解:

  • v[0]:获取vector中的第一个元素(元素3包含在这个元素内)
  • v[0].first:取出外层pair的第一个成员,其类型为pair<pair<int,int>,int>
  • v[0].first.first:取出这一层pair的第一个成员,其类型为pair<int,int>
  • v[0].first.first.second:取出最内层pair的第二个成员,也就是目标元素3

内容的提问来源于stack exchange,提问作者Ashutosh Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:57:19