Category : erase

The problem is:- https://www.hackerrank.com/challenges/vector-erase/problem?h_r=next-challenge&h_v=zen I wrote the below code: #include<bits/stdc++.h> using namespace std; int main() { vector<int> vec; int n,a; cin>>n; for(int i=0;i<n;i++){ cin>>a; vec.push_back(a); } int c,d,e; cin>>c; vec.erase(vec.begin()+c-1); cin>>d>>e; vec.erase(vec.begin()+d-1, vec.end()+e); cout<<vec.size()<<endl; for(int i=0; i<vec.size(); i++){ cout<<vec[i]<<" "; } return 0; } I don’t think that I have done anything unusual, but it ..

Read more

Below is my code. I erase the element of which value is 3 and obtain next iterator by erase() function. But when I tried to print its value.It crashed to my surprise. Anyone know the problem?? int main() { std::vector<int> a = {1, 2, 3, 4, 5}; for(vector<int> ::iterator it=a.begin();it!=a.end();it++) { vector<int> ::iterator g; if(*it==3 ..

Read more