删除索引 [英] remove index

查看:57
本文介绍了删除索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我回来了

我有这个问题

一切编译都很好,但我只需要检查索引比数组大小多了
。虽然它说越界,但它仍然是删除最后一个元素。有人可以帮忙吗

谢谢大家

奥尔加


我只想测试我的功能,所以不要打扰带主要和打印

功能

/ *************************** *********************** *************************** **

写一个函数,removeAt,它有三个参数:一个数组

*

整数,数组的长度, ,和整数说(索引)。

函数*

应该删除索引指示的数组元素。如果索引是* b / b $ b / b
范围或数组为空,则输出相应的消息。 (注意

在*

删除元素后,数组大小减少1.)假设

数组为*

未分类

*

/ ************************** ************************ ************************** ** /

#include< iostream>


使用命名空间std;


void print(int [ ],int);

int removeAt(int [],int&,int);


int main()

{

int list [100] = {1,2,3,6,10,14,20,25,30,40};

int listSize = 10, index;

print(list,listSize);

cout<< 要删除的项目由索引指示: ;

cin>> index;

removeAt(list,listSize,index);

print(list,listSize);

system(" PAUSE");

}

void print(int list [],int listSize)

{

for(int i = 0; i< listSize -1; i ++)

{

cout<< list [i]<< ",";

}

cout<< list [listSize-1]<< endl;

}

int removeAt(int list [],int& listLength,int index)

{

int j;

if(index< 0 || index> listLength - 1)

cout<< 列表超出范围 << (b =指数; j< listLength -1; j ++)

list [j] = list [j + 1];

listLength--;


}

解决方案



oLgAa25写道:

大家好,
我回来了
我有这个问题
一切编译好,但我只需要检查一个比数组大小更多的索引。虽然它说出界,但它仍然是删除最后一个元素。有人可以帮忙吗
谢谢大家
奥尔加

我只想测试我的功能,所以不要打扰主打,并打印
功能
/ ********************************************* *** *****************************
写一个函数,removeAt,它有三个参数:一个数组
*
整数,数组的长度,和整数说(索引)。
函数*
应删除索引指示的数组元素。如果索引超出*
范围或数组为空,则输出相应的消息。 (注意
删除元素后,数组大小减少1.)假设
数组是*
未分类
*
/ ************************************************** **************************** /
#include< iostream>

使用命名空间std ;

void print(int [],int);
int removeAt(int [],int&,int);


为什么返回类型为int?除非你想要返回删除操作的

状态(比如0表示成功,1表示失败),否则使其无效。我希望
宁愿让它无效。

int main()
{
int list [100] = {1,2,3,6 ,10,14,20,25,30,40};
int listSize = 10,index;
print(list,listSize);
cout<< 要删除的项目由索引指示: ;
cin>> index;
removeAt(list,listSize,index);
print(list,listSize);
system(" PAUSE");
}

void print(int list [],int listSize)
{
for(int i = 0; i< listSize -1; i ++)
{
cout<< ; list [i]<< "," ;;
}
cout<< list [listSize-1]<< endl;
}

int removeAt(int list [],int& listLength,int index)


我更喜欢:

void removeAt(int list [],int& listLength,int index)

{
int j;
if(index< 0 || index> listLength - 1)
cout<< 列表超出范围 << ENDL;


如果要从这里返回,请再添加1条声明。

if(condition ...){

cout statement;

返回;

}

for(j = index; j< listLength -1; j ++)

list [j] = list [j + 1];
listLength--;

}




< blockquote>文章< 11 ********************* @ i39g2000cwa.googlegroups。 com>,

" oLgAa25" <醇******** @ yahoo.com>写道:

大家好,
我回来了
我有这个问题
一切编译好,但我只需要检查索引比数组大小更多。虽然它说出界,但它仍然是删除最后一个元素。有人可以帮忙吗
谢谢大家
奥尔加

我只想测试我的功能,所以不要打扰主打,并打印
功能
/ ********************************************* *** *****************************
**
写一个函数,removeAt,需要三个参数:*
整数的数组,数组的长度,以及整数说(索引)。
函数*
应删除索引指示的数组元素。如果索引超出*
范围或数组为空,则输出相应的消息。 (注意
删除元素后,数组大小减少1.)假设
数组是*
未分类
*
/ ************************************************** *******************************
* /



你函数说它返回一个int,那个int是什么意思?如果返回的int

是数组的新大小,那么''listLength''可能

不应该是一个参考。

int removeAt(int list [],int& listLength,int index)
{
int j;
if(index< 0 || index> listLength - 1)
cout<< 列表超出范围 << (j =索引; j< listLength -1; j ++)

list [j] = list [j + 1];
listLength - ;


上面的缩进非常容易让人误解。请注意,无论函数发生什么,上面的行都会执行。

}



-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验来纠正。


你好,

我把它弄虚了。谢谢

我的程序正在运行。谢谢大家


Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don''t bother with main, and print
functions
/************************************************** *****************************
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/************************************************** ****************************/
#include <iostream>

using namespace std;

void print (int [], int);
int removeAt(int [], int&, int);

int main()
{
int list[100] = {1, 2, 3, 6, 10, 14, 20, 25, 30, 40};
int listSize = 10, index;
print (list, listSize);
cout << "The Item to be removed indicated by the index: " ;
cin >> index;
removeAt(list, listSize, index);
print (list, listSize);
system ("PAUSE");
}
void print(int list[], int listSize)
{
for (int i = 0; i < listSize -1; i++)
{
cout << list[i] << ", ";
}
cout << list[listSize-1] << endl;
}
int removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;

}

解决方案


oLgAa25 wrote:

Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don''t bother with main, and print
functions
/************************************************** *****************************
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/************************************************** ****************************/
#include <iostream>

using namespace std;

void print (int [], int);
int removeAt(int [], int&, int);
Why is the return type int ? Make it void unless you want to return the
status of the remove operation (say 0 for success and 1 for failure). I
would rather make it void.

int main()
{
int list[100] = {1, 2, 3, 6, 10, 14, 20, 25, 30, 40};
int listSize = 10, index;
print (list, listSize);
cout << "The Item to be removed indicated by the index: " ;
cin >> index;
removeAt(list, listSize, index);
print (list, listSize);
system ("PAUSE");
}
void print(int list[], int listSize)
{
for (int i = 0; i < listSize -1; i++)
{
cout << list[i] << ", ";
}
cout << list[listSize-1] << endl;
}
int removeAt(int list[], int& listLength, int index)
I would prefer:
void removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;
Add 1 more statement inside if to return from here.
if (condition...) {
cout statement;
return;
}

for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;

}




In article <11*********************@i39g2000cwa.googlegroups. com>,
"oLgAa25" <ol********@yahoo.com> wrote:

Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don''t bother with main, and print
functions
/************************************************** ***************************
**
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/************************************************** ***************************
*/

You function says it returns an int, what does that int mean? If the int
returned is the new size of the array, then ''listLength'' probably
shouldn''t be a reference.
int removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;
Your indentation above is very misleading. Note that the line above
executes no matter what happens in the function.
}


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


Hello,
I made it void. Thank you
and I had my program running. Thank you all


这篇关于删除索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆