Hi everyone, I was a solving a problem of changing one vector in a way of removing all the elements of the vector that the other vector has. And unfortunately it doesn't work. Here is the code :
Code:#include<iostream> #include<vector> #include<string> using namespace std; template <typename type> void function(vector<type>&a,vector<type>&b){ for(int i=0;i<a.size();i++){ int k(0); for(int j=0;j<b.size();j++){ if(a[i]==b[j]) k=1; } if(k==1){ for(int p(i);p<a.size()-1;p++){ a[p]=a[p+1]; } a.resize(a.size()-1); } } } int main(){ int n; cout<<"How many words ?"; cin>>n; cin.ignore(1000,'\n'); vector<string>s(n); for(int i=0;i<n;i++){ getline(cin,s[i]); } int b; cout<<"How many words now?"; cin>>b; cin.ignore(1000,'\n'); vector<string>j(b); for(int u(0);u<b;u++){ getline(cin,j[u]); } function(s,j); cout<<endl; for(int i=0;i<s.size();i++){ cout<<s[i]<<endl; } return 0; }



LinkBack URL
About LinkBacks


