Mixture

身未动,心已远

导航

STL: remove

leetcode:Remove Element

code from: https://github.com/soulmachine/leetcode

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        return distance(A,remove(A,A+n,elem));
    }
};

又是STL一句话搞定。distance之前已经学过了,正好复习一下:

distance(InputIterator first, InputIterator last), 返回的是两个迭代器的距离。

remove的说明是这样的:

template <class ForwardIterator, class T>
  ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);

 Transforms the range [first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range.

 

posted on 2014-04-01 05:45  parapax  阅读(189)  评论(0编辑  收藏  举报