#include
#define MaxSize 10 //定义最大长度
typedef int ElemType;
typedef struct{
ElemType data[MaxSize]; //用静态的"数组"存放数据元素
int length; //顺序表当前长度
}SqList; //顺序表类型定义
//基本操作——初始化一个顺序表
void InitList(SqList &L)
{
for(int i = 0;i < MaxSize;i++)
{
L.data[i] = 0;
}
L.length =10; //顺序表初始长度为 10
}
//顺序表的插入
bool ListInsert(SqList &L,int i,int e) //i为位序 ;e为插入元素
{
if(i<=0 || i>L.length+1) //判断插入位置是否合法
{
return false;
}
if(L.length >= MaxSize) //超出空间
{
return false;
}
for(int j=L.length;j>=i;j--)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e; //在i位置处放e
L.length++;
return true;
}
int main()
{
SqList L;
InitList(L);
bool ret=ListInsert(L,2,5);
if(ret)
printf("增加成功");
else
printf("增加失败");
return 0;
}
修改如下,见注释处,供参考:
#include <stdio.h>
#define MaxSize 10 //定义最大长度
typedef int ElemType;
typedef struct {
ElemType data[MaxSize]; //用静态的"数组"存放数据元素
int length; //顺序表当前长度
}SqList; //顺序表类型定义
//基本操作——初始化一个顺序表
void InitList(SqList& L)
{
for (int i = 0; i < MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0; //顺序表初始长度为 0 修改
//L.length = 10;//顺序表初始长度为 10
}
//顺序表的插入
bool ListInsert(SqList& L, int i, int e) //i为位序 ;e为插入元素
{
if (i <= 0 || i > MaxSize) //判断插入位置是否合法 修改
//if (i <= 0 || i > L.length + 1)
{
return false;
}
if (L.length >= MaxSize) //超出空间
{
return false;
}
for (int j = L.length; j > i - 1; j--) //修改
//for (int j = L.length; j >= i; j--)
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e; //在i位置处放e
L.length++;
return true;
}
int main()
{
SqList L;
InitList(L);
bool ret = ListInsert(L, 2, 5);
if (ret)
printf("增加成功");
else
printf("增加失败");
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:命名空间的使用有三种方式:
加命名空间名称及作用域限定符
使用using将命名空间中成员引入
使用using namespace
//1.使用命名空间名称及作用域限定符
int main()
{
printf("%d\n", N::a);
return 0;
}
//2.使用using将命名空间中成员引入
using N::b;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
return 0;
}
//3.使用using namespace
using namespce N;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
Add(10, 20);
return 0;
}
在C++中我们开头总是写using namespace std; 很多人压根不知道这句话的意思,这里的std就是命名空间,当然这句话的意思就是使用标准命名空间
我很乐意为您解答这些问题。下面是对于c++中指针和引用的区别,以及它们的使用场景和注意事项,多继承和虚继承概念和使用方法的详细解答。
指针和引用都是c++中用来传递变量的地址,它们之间的主要区别如下:
指针和引用都有它们的使用场景和注意事项。一般来说,我们可以根据需要选择适合的方式。
下面是c++中多继承和虚继承概念和使用方法的详细解答。
多继承指的是一个派生类从多个基类继承成员。虚继承是指在多继承的情况下,使用虚基类来避免继承层次中的二义性。
在使用多继承的时候,需要注意以下几点:
在使用虚继承的时候,需要注意以下几点:
下面是使用虚继承的一些代码示例:
class Animal {
public:
Animal() {
cout<<"Animal Constructor"<<endl;
}
~Animal() {
cout<<"Animal Destructor"<<endl;
}
};
class Mammal: virtual public Animal {
public:
Mammal() {
cout<<"Mammal Constructor"<<endl;
}
~Mammal() {
cout<<"Mammal Destructor"<<endl;
}
};
class Bird: virtual public Animal {
public:
Bird() {
cout<<"Bird Constructor"<<endl;
}
~Bird() {
cout<<"Bird Destructor"<<endl;
}
};
class Bat: public Mammal, public Bird {
public:
Bat() {
cout<<"Bat Constructor"<<endl;
}
~Bat() {
cout<<"Bat Destructor"<<endl;
}
};
int main() {
Bat b;
return 0;
}
以上就是对于c++中指针和引用的区别,以及它们的使用场景和注意事项,多继承和虚继承概念和使用方法的详细解答。希望能对您有所帮助。