vector - I am making a c++ code and error occurs I don't know why -


don't know problem is. tried compile code once , worked. however, copied in new project in order modify it, colour or time wait, , works in first programme. wrong? feel frustrated. code want graphic sine function. here code:

#include <bits/stdc++.h>  using namespace std; int main() {     int t,n1,n2,res=0;     cin>>t;     vector<int> v1;     vector<int> v2;     while (t--) {         cin>>n1>>n2;         v1.push_back(n1);         v2.push_back(n2);     }     (int i=v1.size();i>0;i++) {         bool state=binary_search(v2.begin(),v2.end(),v1[i-1]);         if (state){             v1.pop_back();             res++;         }     }     cout<<res<<endl;     return 0; } 

error of console

change

for (int i=v1.size();i>0;i++) 

to

for (int i=v1.size();i>0;i--) 

otherwise access behind bounds -> ub.

or segment in memory -> sigsegv.


also std::binary_search says:

"... fully-sorted range meets these criteria."

so before calling binary-search have do

std::sort (v2.begin(), v2.end()); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -