C++复合类型的应用

数组

char类型数组,以‘\0’结尾才是字符串,否则是数组。
cout输出char数组时,到’\0’时才停止输出。
数组不可以直接通过数组名进行赋值。

字符串

”S“是字符串,由‘S’和‘\0’组成,而‘S’是单个字符。

strlen()返回可见字符长度,而sizeof()返回整个数组的长度,包括空的位数。

int main()
{
	char string[15]="C++";
	cout<<sizeof(string)<<endl;			//15
	cout<<strlen(string)<<endl;			//3	
}

cin.getline(数组,输入的最多个数);输入一整行并删除‘\n’
cin.get(数组,输入的最多个数);输入一整行不删除’\n’
cin.get() 输入一个字符到cin中;

int main()
{
	char string2[100];
	int Int;							//char型或int型均有错
	cout<<"input the number"<<endl;
	cin>>Int;
	cin.get();							//可以消除一个字符 
	cout<<"input the string"<<endl;
	cin.getline(string2,10);			//混合输入 数字和面向行的输入会出现错误 

	cout<<Int;
	cout<<string2;
} 

&:地址运算符

string类

使用时需引入‘#include’文件。
不需要设置长度,可以在程序中动态分配内存空间。可使用cin与cout函数,进行IO操作。
getline(cin,str)

int main()
{
	string s1,s2,s3;
	cout<<"input s1"<<endl;
	getline(cin,s1);
	cout<<"input s2"<<endl;
	getline(cin,s2);
	s3=s1+s2;															//可直接赋值,添加  
	cout<<s1<<"\t\t\t\t"<<sizeof(s1)<<"\t\t"<<s1.size()<<endl;			//.size()返回非空元素个数 
	cout<<s2<<"\t\t\t\t"<<sizeof(s2)<<"\t\t"<<s2.size()<<endl;	
	cout<<s3<<"\t\t\t\t"<<sizeof(s3)<<"\t\t"<<s3.size()<<endl;	
}

结构体

struct type_name
{
	int a;
	string str;
	float b;
};

结构体可以互相直接赋值。

指针以及new /delete 的用法

指针变量数值一般变量数值
p指针变量值(地址值),可变&a变量的地址值,不可变
*p存储在对应地址的变量,可变a变量的值,可变(const除外)

指针可能出现的错误:
在定义指针后未对指针变量做初始化,导致其指向不明。
必须在使用‘*’前,对指针变量进行赋值。

动态分配的内存均在堆(heap)中,而静态分配的内存在栈(stack)内。

//不限长度输入 ,但不能输入多个单词 
char *in_func(void)
{
	cout<<"please input"<<endl;
	char temp[100];
	cin>>temp;
	int length=strlen(temp);
	char *p=new char [length+1];
	strcpy(p,temp);
	return p;
}
int main()
{
	char *p[3];					//指针数组 
	cout<<"what is your first name?";
	p[0]=in_func();
	cout<<"what is your last name?";
	p[1]=in_func();
	cout<<"what is your age?";
	p[2]=in_func();
	cout<<(p[0])<<endl;
	cout<<(p[1])<<endl;
	cout<<(p[2])<<endl;
	delete p[0];				//new与delete要配对使用 
	delete p[1];
	delete p[2];
}

在这里插入图片描述

指针与结构体的应用,->的优先级问题。

struct worker
{
	string name;
	int age;
	float weight;
};

int main()
{
	cout<<"please input the number";
	int t=0;
	cin>>t;
	
	worker *p=new worker [t+1]; 
	int i;
	for (i=0;i<t;i++)
	{
		cin.get();
		cout<<"input the "<<i+1<<" th"<<" name"<<endl;
		getline(cin,(p+i)->name);
		cout<<"input the "<<i+1<<" th"<<" age"<<endl;
		cin>>(p+i)->age;
		cout<<"input the "<<i+1<<" th"<<" weight"<<endl;
		cin>>(p+i)->weight;
	}
	
	for (i=0;i<t;i++)
	{
		cout<<"input the "<<i+1<<" th"<<" name"<<(p+i)->name<<endl;
		cout<<"input the "<<i+1<<" th"<<" age"<<(p+i)->age<<endl;
		cout<<"input the "<<i+1<<" th"<<" weight"<<(p+i)->weight<<endl;
	}
	delete [] p;
}

指针与数组的关系

指针数组
p地址 (可变)a地址(不可变)
*p变量值(可变)a[0]变量值(可变)

int *p[3]; //指针数组(由三个指针组成)
p[0] //指针
*(p[0]) //指针指向的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值