I am doing a leetcode problem, and I am required to return a 2d array of results. But I am using a priority queue for that and am unable to move elements to a 2d array. I am not able to come up with a syntax for this. The push_back(), is not working when I try to push the pair in a 2d array. here is the link to the problem
Code -
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& p, int k) {
vector<vector<int>>closest;
//pairp;
priority_queue<pair<int,pair<int,int>>>heap;
for(int i = 0; i < p.size(); i++){
heap.push({p[i][0] * p[i][0] + p[i][1]*p[i][1],
{p[i][0],p[i][1]}});
}
if(heap.size() > k){
heap.pop();
}
while(heap.size() > 0){
pair<int,int>ptr = heap.top().second;
//want to add the statement to copy elements to closest[][] here
heap.pop();
}
return closest;
}
};
Error message on adding, closest.push_back(ptr);
Line 22: Char 21: error: no matching member function for call to 'push_back'
closest.push_back(ptr);
~~~~~~~~^~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1184:7: note: candidate function not viable: no known conversion from 'pair<int, int>' to 'const std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>::value_type' (aka 'const std::vector<int, std::allocator>') for 1st argument push_back(const value_type& __x) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1200:7: note: candidate function not viable: no known conversion from 'pair<int, int>' to 'std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>::value_type' (aka 'std::vector<int, std::allocator>') for 1st argument push_back(value_type&& __x) ^ 1 error was generated.
closestis a vector of a vector of int. You can't push apair<int,int>into it as it expects avector<int>. Maybe you want to convert your pair into a vector, maybe you want something else; it's not really clear from your question. Ifheapconsists of the element{2, {1,0}}, what do you wantclosestto look like after you inserted that element?