Vector-Erase

Sort by

recency

|

412 Discussions

|

  • + 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;
    

    }

  • + 0 comments
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    
    int main() {
        vector<int> v;
        int x, a, b;  //x=elem to remove, a and b=range to remove
    
        int size; //size of vector
        cin >> size;
    		
    		//fill vector
        copy_n(istream_iterator<int>(cin), size, back_inserter(v));  
    
        cin >> x;  //insert element to remove
        v.erase(v.begin()+(--x));
    
        cin >> a >> b;  //insert range to remove
        v.erase(v.begin()+(--a), v.begin()+(--b));
    
        cout << v.size() << endl;  //output size of vector
    		
    		//output vect
        copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); 
    		
        return 0;
    }
    
  • + 2 comments

    all test case clear

    #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,ele;
        vector<int>v;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>ele;
            v.push_back(ele);
        } 
        int p,s,e;
        cin>>p;
        cin>>s>>e;
        v.erase(v.begin()+p-1);
        v.erase(v.begin()+s-1,v.begin()+e-1);
        cout<<v.size()<<endl;
        for(int i:v){
            cout<<i<<" ";
        }
        
        return 0;
    }
    
  • + 0 comments

    Earasing the vectors using the C++ language

    Code solution :

    include

    include

    include

    include

    include

    using namespace std;

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ vectorv; int n,x; cin>>n; for(int i = 0; i>x; v.push_back(x); } int y; cin>>y; v.erase(v.begin()+y-1); int z,a; cin>>z>>a; v.erase(v.begin()+(z-1), v.begin()+(a-1));

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

    }