I was trying to sort a vector having user defined data type having two values, on the basis of one. But i get the error of bad_alloc. this is the code :
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct s{
int value;
int weight;
};
bool comp(s a , s b){
return a.value>b.value;
}
int main(){
vector <s> p;
s temp;
int n;
cin>>n;
while(n>=1){
cin>>temp.value;
cin>>temp.weight;
p.push_back(temp);
}
sort(p.begin(), p.end(), comp);
vector <s> :: iterator it;
for(it = p.begin(); it != p.end();it++){
*it = temp;
cout<<temp.value<<endl;
}
}
on running :
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
can anyone help?