Vector-Erase

Sort by

recency

|

415 Discussions

|

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin>>n;
    vector v(n);

    for(int i=0; i<n; i++){
        cin>> v[i];
    } 
    int s;
    cin>> s ;
    
    v.erase(v.begin() + (s-1));
    
    int a,b;
    cin>> a >> b;
    
    v.erase(v.begin() + (a-1), v.begin()+(b-1));
    
    cout<<v.size()<<endl;
    for(int i=0; i<v.size(); i++){
        cout<<v[i]<<" ";
    }
    return 0;
    

    }

  • + 0 comments

    Here is Vector-Erase problem solution in C++ - https://programmingoneonone.com/hackerrank-vector-erase-solution-in-cpp.html

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int N; cin>>N;//taking the size of vector vectorv; int i; //giving elements to the vector for (i=0; i>e; v.push_back(e); } int x; cin>>x; //erasing the vector v.erase(v.begin()+(x-1)); int a,b; cin>>a; cin>>b; //position deleting v.erase(v.begin()+(a-1),v.begin()+(b-1)); vector::iterator it; int count=0; for (it=v.begin(); it

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        int n;
        cin >> n; // Read the size of the vector
    
        vector<int> v(n); // Declare a vector of size n
    
        for (int i = 0; i < n; i++) {
            cin >> v[i]; // Read the elements of the vector
        }
    
        int q1;
        cin >> q1; // Read the index to erase
        v.erase(v.begin() + q1 - 1); // Erase the element at index q1
    
        int q2, q3;
        cin >> q2 >> q3; // Read the range [q2, q3) to erase
        v.erase(v.begin() + q2 - 1, v.begin() + q3 - 1); // Erase the elements in the given range
    
        cout << v.size() << endl; // Print the size of the vector
    
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " "; // Print the elements of the vector
        }   
        return 0;
    }
    
  • + 0 comments

    // Can anyone help to find out the mistake

    include

    include

    include

    include

    include

    using namespace std;

    int main() { vector v; int n,value; cin>>n; for(int i=0;i>value; v.push_back(value); } v.erase(v.begin()+1); v.erase(v.begin()+1,v.begin()+3);

    for(int i=0;i<3;i++)
    {
        cout<<v[i]<<" ";
    }
    return 0;
    

    }