C/C++ 刪除陣列中指定的元素,寫的不是很完整。
範例碼(一):
// test.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a[6]={10,20,30,40,50,60};
int b=sizeof(a)/4;
char str[10];
memset(str,0x00,sizeof(str));
cout<<"輸入要刪除的陣列元素索引=";
gets(str);
int c=atoi(str);
a[c]=0x00;
for(int i=0 ; i < b ; i++)
{
if(a[i] != 0x00)
{
if(i == 0)
{
cout<<a[i];
}
else
{
cout<<","<<a[i];
}
}
}
cout<<endl;
return 0;
}
執行結果:
文章標籤
全站熱搜

留言功能已依作者設定調整顯示方式