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.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步