Vector-Erase

Sort by

recency

|

413 Discussions

|

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

    }

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

      how does this code works? for(int i:v){ cout<

    • + 1 comment

      Hey!!, So i like tried your solution and it works with every default test case and i compared it to mine but it has a bug, when i submit my code it successfully passes all the test cases except for 1, 2 & 3 which are test cases with very large input and i cant figure out. It'ld be very nice if you could help me with it, here's the code:- #include

      include

      include

      include

      include

      using namespace std;

      int main() {

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

      }

      • + 1 comment

        Hi, You need to perform two erase() calls. The first for the index and the second for the range. Try to run your code in paper with different use cases, then you will realize where the issue is.

        • + 0 comments

          I actually did figure that out, Here's the final code that i submitted. But Thanks for respondnig! :-

          include

          include

          include

          include

          include

          include

          using namespace std;

          int main() {

          vector<int> arr_no; 
          int n, element;
          cin >> n;
          
          for (int i = 0; i < n; i++) {
              cin >> element;
              arr_no.push_back(element);
          }
          int index;
          int a, b;
          cin >> index; 
          cin >> a >> b;
          
          arr_no.erase(arr_no.begin() + index  - 1);
          arr_no.erase(arr_no.begin() + a - 1, arr_no.begin() + b - 1);
          
          cout << arr_no.size() << endl;
          
          for (int i: arr_no) {
              cout << i << " ";
          }
          
          return 0;
          

          }