利用数组进行删除一个整数
删除整数:假设整数数组a[10]中,存在与整数x相同的数据,其中数组a和x均为用户输入,则完成以下功能:(1)将数组a中与x相同的所有数据,除第一个之外,全部删除;如果只有一个(或没有)相同的数据,则保持数组a不变。
(2)输出删除后的数组a。
程序代码:#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main( void )
{
int a[10], x;
// 输入
using Type = remove_extent_t<decltype(a)>;
copy_n( istream_iterator<Type>(cin), size(a), begin(a) );
cin >> x;
// 删除
auto itor = find( begin(a), end(a), x );
if( itor != end(a) )
itor = remove( next(itor), end(a), x );
// 输出
copy( begin(a),itor, ostream_iterator<Type>(cout," ") );
}