How to Iterate in Pair Class?

How to iterate over the pair that we made.?

For example I want to made a pair i.e

5 10

15 20

25 30

than generally we will define one pair like:

BUT the above code is showing error p[i] is not defined so basically I want to know how we will iterate in pair.

@rahul_bansal you are just creating a single pair that is why it is showing this error.
pair<int,int>p defines a single pair for eg. (1,2)
to create an array of pairs you have to define a vector of pair i.e - vector<pair<int,int>>v;
and then we iterate over this vector.
vector<pair<int,int>>v defines an array of pairs for eg: (1,2),(3,4),(5,6) .
then you can easily use for looop to iterate over this array of pairs.
complete code can be written like this

vector<pair<int,int>>v;
int a,b;
for(int i=0;i<5;i++)
{
cin>>a>>b;
v.push_back(make_pair(a,b));
}
for(int i=0;i<5;i++)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
}

Hope this helps

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

oh ok this means vector pairs are simply list or array of pairs, right??

yes @rahul_bansal :slight_smile: