#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});
}
how to access 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});
}
how to access 3 ???
For these cases, it helps if you use some temporary references to access the inner elements of the vector. Then, realize that you want to print the second element of the inner pair for a given element.
#include <cassert> // assert
#include <iostream> // cout
#include <vector>
using vector_of_nested_pairs =
std::vector<std::pair<std::pair<std::pair<int, int>, int>, int>>;
void print_second_element_of_inner_pair(const vector_of_nested_pairs& v, size_t i) {
assert(i < v.size());
const auto& outer_pair = v[i];
const auto& middle_pair = outer_pair.first;
const auto& inner_pair = middle_pair.first;
std::cout << inner_pair.second;
}
int main() {
vector_of_nested_pairs v{};
v.push_back({{{1,3},4},5});
v.push_back({{{2,4},6},7});
print_second_element_of_inner_pair(v, 0);
}
// Outputs: 3
In other order of things, prefer not to:
#include<bits/stdc++.h>
, since it's not portable (mainly, but there are other reasons), norusing namespace std;
; basically it can confuse the compiler (among other things).