编程论坛
注册
登录
编程论坛
→
C++教室
利用数组进行删除一个整数
k3552
发布于 2022-04-21 16:23, 1333 次点击
删除整数:假设整数数组a[10]中,存在与整数x相同的数据,其中数组a和x均为用户输入,则完成以下功能:
(1)将数组a中与x相同的所有数据,除第一个之外,全部删除;如果只有一个(或没有)相同的数据,则保持数组a不变。
(2)输出删除后的数组a。
1 回复
#2
rjsp
2022-04-21 21:48
程序代码:
#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
,
"
"
) );
}
输入
0 1 2 3 4 5 6 7 8 9
10
输出
0 1 2 3 4 5 6 7 8 9
输入
0 1 2 3 3 4 3 3 5 3
3
输出
0 1 2 3 4 5
1