0
int n,a,b,c;
cin >> n;
map<int,vector<pair<int,int>>> mv;
for(int i=0;i<n;++i)
{
    cin>>a>>b>>c;
    mv[a].insert(make_pair(b,c));
}

I m trying to take input from std::vector of std::pair which is in a std::map.Is it possible?

And, how can I iterate through the map?

2
  • 2
    insert takes an iterator to indicate where to insert the new element. Perhaps you meant push_back? Commented Oct 15, 2019 at 19:32
  • 2
    Could you clarify the problem a bit more? It doesn't make any sense. By saying 'trying to take input from vector pair' what do you want to achieve? Commented Oct 15, 2019 at 19:37

1 Answer 1

4

mv[a].insert(...) is not the right function call. You need to use mv[a].push_back(...). Remember that mv[a] return a reference to the value in the map that corresponds to the key a.

If you divide that line in two, it will make more sense.

int n,a,b,c;
cin >> n;
map<int,vector<pair<int,int>>> mv;
for(int i=0;i<n;++i)
{
    cin>>a>>b>>c;
    vector<pair<int,int>>& v = mv[a];
    v.push_back(make_pair(b,c));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or even v.emplace_back(b,c);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.