I am confused , please help

How to take input of K arrays from the user and then pass it in a vector ?

hello @dhruvtrehan45
declare a vector of vector (say A).
run loop for k times and each time read one vector and and push it to A ( vector of vector)

vector< vector< int> > A;
for(int i=0;i<k;i++){
read one vector and do A.push_back( new vector);

}

#include #include #include using namespace std; typedef pair<int ,pair<int ,int> > node; vectormerge(vector<vector > arr) { vector result; priority_queue<node , vector , greater >pq; //min heap for(int i=0;i<arr.size();i++) { pq.push({arr[i][0],{i,0}}); } while(!pq.empty()) { node current=pq.top(); pq.pop(); int element =current.first; int x=current.second.first; int y=current.second.second; result.push_back(element); if(y+1< arr[x].size()) { pq.push({arr[x][y+1],{x,y+1}}); } } return result; } int main() { int k,n; cin>>k>>n; vector <vector > arr; for(int i = 0 ; i < k ; i++) { vector v1; int a; for(int i = 0 ; i < n ; i++) { cin>>a; v1.push_back(a); } arr.push_back(v1); } vector output=merge(arr); for(auto x:output) { cout<<x<<" "; } }

https://ide.codingblocks.com/s/355969 Please see segmentation fault is comming

hello @dhruvtrehan45
a) pls share it using cb ide
b) also tell me what issue u r facing

@dhruvtrehan45

image

it is working fine from my side.
pls refresh the tab and try again

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.