class MyArray
{
public:
MyArray();
void insert(int position, int content);
void deleteByPosition(int position);
void print();
private:
int contents[100]; // 数组,用来存放元素
int size; // 数组内元素个数
};
只需要提交类的声明以及函数实现
C++数组插入删除问题
答案:3 悬赏:10 手机版
解决时间 2021-02-23 06:56
- 提问者网友:椧運幽默
- 2021-02-22 22:21
最佳答案
- 五星知识达人网友:走死在岁月里
- 2021-02-22 23:05
#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;
class MyArray
{
public:
MyArray();
void insert(int position, int content);
void deleteByPosition(int position);
void print();
private:
int contents[100]; // 数组,用来存放元素
int size; // 数组内元素个数
};
MyArray::MyArray()
{
size = 0;
}
void MyArray::insert( int position, int content )
{
if(position<100 && position>=0)
{
for (int i=size; i>position; i--)
{
contents[i] = contents[i-1];
}
position = position>size ? size : position;
contents[position] = content;
size++;
}
}
void MyArray::deleteByPosition( int position )
{
if(position<size && position>=0)
{
for (int i=position; i<size;i++)
{
contents[i] = contents[i+1];
}
size--;
}
}
void MyArray::print()
{
for (int i=0; i<size; i++)
{
cout << contents[i] << "\t";
}
cout << endl;
}
int main()
{
MyArray a;
for (int i=0; i<20; i++)
{
a.insert(i, i);
}
a.print();
for (int i=0; i<10; i++)
{
a.insert(rand()%100, rand()%10+20);
}
a.print();
for (int i=0; i<5; i++)
{
a.deleteByPosition(rand()%30);
}
a.print();
return 0;
}声明+实现+测试...么么哒
#include <time.h>
#include <Windows.h>
using namespace std;
class MyArray
{
public:
MyArray();
void insert(int position, int content);
void deleteByPosition(int position);
void print();
private:
int contents[100]; // 数组,用来存放元素
int size; // 数组内元素个数
};
MyArray::MyArray()
{
size = 0;
}
void MyArray::insert( int position, int content )
{
if(position<100 && position>=0)
{
for (int i=size; i>position; i--)
{
contents[i] = contents[i-1];
}
position = position>size ? size : position;
contents[position] = content;
size++;
}
}
void MyArray::deleteByPosition( int position )
{
if(position<size && position>=0)
{
for (int i=position; i<size;i++)
{
contents[i] = contents[i+1];
}
size--;
}
}
void MyArray::print()
{
for (int i=0; i<size; i++)
{
cout << contents[i] << "\t";
}
cout << endl;
}
int main()
{
MyArray a;
for (int i=0; i<20; i++)
{
a.insert(i, i);
}
a.print();
for (int i=0; i<10; i++)
{
a.insert(rand()%100, rand()%10+20);
}
a.print();
for (int i=0; i<5; i++)
{
a.deleteByPosition(rand()%30);
}
a.print();
return 0;
}声明+实现+测试...么么哒
全部回答
- 1楼网友:酒醒三更
- 2021-02-23 00:28
void insert(int position,int content)
{
int i;
if(position>=size)
{
position = size;
contents[position] = content;
return;
}
size += 1;
for(i=size-1;i>position;i--)
contents[i] = contents[i-1];
contents[position] = content;
}
void deleteByPosition(int position)
{
int i;
if(position>=size)
return;
for(i=position;i<size-1;i++)
contents[i] = contents[i+1];
size -= 1;
}
void print()
{
int i;
for(int i=0;i<size;i++)
cout<<contents[i]<<"\t";
cout<<endl;
}直接写的,没有测试。如果有问题,追问解决。没问题的话,就采纳了吧!
- 2楼网友:青灯有味
- 2021-02-22 23:14
不知楼主的意思是什么? 是数组的这些操作在内存中的实现还是简单地对数组的操作.
对于简单的数组操作:
例:const unsigned int size = 5; // 定义数组大小
int array[size]; // 定义一个静态数组, 对于静态数组,一旦声明了
// 大小, 数组的大小就不会变了
array[0] = 0; // 将整数 0 插入到 数组array 的第一个单元
array[1] = 0; // 将整数 0 插入到 数组array 的第二个单元
array[2] = 9; // 将整数 9 插入到 数组array 的第三个单元
std::cout << array[0] << std::endl; // 输出 数组array 的第一个单元, 也就是0
std::cout << array[1] << std::endl; // 输出 数组array 的第一个单元, 也就是0
std::cout << array[2] << std::endl; // 输出 数组array 的第一个单元, 也就是9
// 静态数组一经声明, 大小就固定了, 无法删除其中的某个, 要想做到删除, 只要
// 你不去 索引 它就行了
对于数组方法的内存实现, 在下不知道, sorry
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯