fill vector pair from cin

I can't seem to wrap my head around this.

My task is to get input from cin (using push_back) as a vector and then sort the vector based on 'pairs' that are entered linearly into said vector. Essentially I want to convert A[]={1,2,3,4,5,6} into vector< pair<int, int> > v = {{1,2},{3,4},{5,6}}, then sort (already done in this example.)

I've tried using std::fill, but it doesn't conform to my needs. Other resources on this site and stackoverflow have gone a little over my head, is there a way to input directly into a vector pair?

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>

using namespace std;

bool compare(const pair<int,int>&A, const pair<int,int>&B);

int main()
{
    vector<int> v;
    int tok;
    while(cin >> tok){v.push_back(tok);}
    
    sort(v.begin(),v.end(),&compare);
    for(int i = 0; i < v.size(); i++)
    {
        cout << v[i].first << " " v[i].second;
    }
    return 0;
}

bool compare(const pair<int,int>&A, const pair<int,int>&B)
{
    if(A.second <= B.second)
    {
        if(A.first >= B.first)
            return 1;
        else return 0;
    }
    return 0;
}
1
2
3
4
std::vector< std::pair<int, int> > v;
std::pair<int, int> tok;
while( std::cin>>tok.first>>tok.second )
   v.push_back(tok);
I can't believe it was that easy. Thank you.
Topic archived. No new replies allowed.