简体   繁体   English

从向量中擦除元素(hackerrank)

[英]erasing elements from a vector (hackerrank)

I'm a beginner in C++.我是 C++ 的初学者。 I have tried to solve this problem but couldn't, thus I checked the solution afterwards but there are some lines I don't understand:我试图解决这个问题但无法解决,因此我后来检查了解决方案,但有些行我不明白:

v.erase(v.begin() + x - 1); // i want to know why the "-1" is put here
v.erase(v.begin() + b - 1, v.begin() + c - 1);

Here is the code:这是代码:

int main()
{
  int N, a, x, b, c;
  vector <int> v;
  cin >> N;
  for (int i = 0; i<N; i++)
  {
    cin >> a;
    v.push_back(a);
  }
  cin >> x >> b >> c;
  v.erase(v.begin() + x - 1);
  v.erase(v.begin() + b - 1, v.begin() + c - 1);
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++)
  {
    cout << v[i] << " ";
  }
}

The question aims to make you familiar with 2 common syntax of vector erase method.该问题旨在让您熟悉向量擦除方法的 2 种常见语法。 For deleting single element use,对于删除单个元素使用,

v.erase( pass iterator pointing to the element that you want to erase ) v.erase(传递指向要擦除的元素的迭代器

For example, v.erase(v.begin()) will erase first element of the vector or in other words will erase element at position 0 of the vector.例如, v.erase(v.begin())将擦除向量的第一个元素,或者换句话说,将擦除向量的 position 0 处的元素。

Since here v.begin() is iterator to the first element of the vector, provided vector isn't empty.由于这里v.begin()是向量的第一个元素的迭代器,因此提供的向量不为空。

Similarly,相似地,

v.erase(v.begin() + x -1);

erases element at position x of the vector.擦除向量的 position x 处的元素。

Now to erase a range in the vector, overloaded method of erase is used.现在要擦除向量中的范围,使用擦除的重载方法。 It is used as follows,它的使用如下,

v.erase(iter1,iter2) v.erase(iter1,iter2)

It will erase all the elements in the range of iter1 to iter2 but not including iter2, that is elements in the range [iter2, iter2) will be erased.它将擦除 iter1 到 iter2 范围内的所有元素,但不包括 iter2,即 [iter2, iter2) 范围内的元素将被擦除。 Remember iter2 won't be erased.请记住 iter2 不会被删除。 Thus this code,因此这段代码,

v.erase(v.begin() + b - 1, v.begin() + c - 1);

will erase all the elements from index b to index c, but not including index c.将擦除从索引 b 到索引 c 的所有元素,但不包括索引 c。

#include <cmath>           //Header section
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {  
    vector<int> v;            //initializing vector v
    int n,i,b,c,d,size,val;
    cin>>n;                   //no. of integers in vector v
    for(i=0;i<n;i++)          
    {
        cin>>val;
        v.push_back(val);     /*syntax for input elements in vector, "val" is the 
                                    element*/
    }
    cin>>b;   // input position of element to be erased

    cin>>c>>d;  //input range of elements to be erased

    v.erase(v.begin()+b-1); // func to be used to erase an element

    v.erase(v.begin()+c-1,v.begin()+d-1) // func used to erase a range

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM