1:最基本的:int a[3] ={1,2,3}
2:数组可不可不初始化?#include<iostream>
using namespace std;
int main(){
int a [4];
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
return 0;
}//对于没有初始化的数组,程序自动赋一个初值;目的就是对程序的安全性做一个保护。
3:没有任何常量表达式的数组的初始化:
#include<iostream>
using namespace std;
int main(){
int a [] = {1,2,3,4};//这种是ok的,程序 知道是四个元素
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
return 0;
}
4:数组初始化指明了大小,但是只指明了部分元素;
#include<iostream>
using namespace std;
int main(){
int a [4] = {1,2};//前面两个元素为1,2,后 面的两个元素为0,0
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
return 0;
}
5:利用前面4的特性,就可以对数组所有元素进行0的初始化。
#include<iostream>
using namespace std;
int main(){
int a[4] = {0};//这样数组的每一个元素都初始化 为0;
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
return 0;
}
6:当数组的元素大于数组的大小,编译错误:int a[4] ={1,2,3,4,5,6}是错误的。数组的 元素小于数组的大小是允许的。
Dienstag, 20. Oktober 2015
数组的定义
1数组的定义:
类型 数组名 [常量表达式]
float sheep [10];
int a2001 [1000];
注意:数组的大小定义后,不能随意更改:int n = 10;
int a [n] = {0};//是不被允许的,[]中必须是常量表达式;
如果要更改数组大小,方法可以用:1:
#include<iostream>
using namespace std;
int main(){
const int i = 4;// const int i 符号常量
int a [i] = {1,2,3,4}
cout<<"a[0]="<<a[0]<<endl;
cout<<"a[1]="<<a[1]<<endl;
cout<<"a[2]="<<a[2]<<endl;
cout<<"a[3]="<<a[3]<<endl;
return 0;
}
如果发现数组大小不够用了,只需要改 i 的大小。但是并不是说定义了可变长的数 组。这样可以使程序的修改变的简单一些。
2:
#include<iostream>
using namespace std;
# define N 4//预定义 ,表示在程序的任何地方只要碰到N就把它当做4对待
int main(){
int a [N] = {1,2,3,4};
cout<<"a[0]="<<a[0]<<endl;
cout<<"a[1]="<<a[1]<<endl;
cout<<"a[2]="<<a[2]<<endl;
cout<<"a[3]="<<a[3]<<endl;
return 0;
}
类型 数组名 [常量表达式]
float sheep [10];
int a2001 [1000];
注意:数组的大小定义后,不能随意更改:int n = 10;
int a [n] = {0};//是不被允许的,[]中必须是常量表达式;
如果要更改数组大小,方法可以用:1:
#include<iostream>
using namespace std;
int main(){
const int i = 4;// const int i 符号常量
int a [i] = {1,2,3,4}
cout<<"a[0]="<<a[0]<<endl;
cout<<"a[1]="<<a[1]<<endl;
cout<<"a[2]="<<a[2]<<endl;
cout<<"a[3]="<<a[3]<<endl;
return 0;
}
如果发现数组大小不够用了,只需要改 i 的大小。但是并不是说定义了可变长的数 组。这样可以使程序的修改变的简单一些。
2:
#include<iostream>
using namespace std;
# define N 4//预定义 ,表示在程序的任何地方只要碰到N就把它当做4对待
int main(){
int a [N] = {1,2,3,4};
cout<<"a[0]="<<a[0]<<endl;
cout<<"a[1]="<<a[1]<<endl;
cout<<"a[2]="<<a[2]<<endl;
cout<<"a[3]="<<a[3]<<endl;
return 0;
}
Freitag, 2. Oktober 2015
什么是链表
用指针把结构体链起来。
从数组的缺陷说起:一个是数组中所有元素的类型必须一致,第二个是数组的大小必须事先制定并且一旦指定之后不能更改。
如何解决:数组的第一个缺陷靠结构体去解决。数组的一个缺陷靠结构体去解决。结构体允许其中的元素的类型不相同,因此解决了数组的第一个缺陷。
第二个缺陷,我们希望数组的大小能够实时扩展。普通的数组显示不行。我们可以对数组进行封装以达到这种目的。我们还可以使用一个新的数据结构来解决。这个新的数据结构就是链表。几乎可以这样理解:链表就是一个元素个数可以实时变化的数组
箭头表示指针。
这样就能很容易的插入数据。
链表的形式
从数组的缺陷说起:一个是数组中所有元素的类型必须一致,第二个是数组的大小必须事先制定并且一旦指定之后不能更改。
如何解决:数组的第一个缺陷靠结构体去解决。数组的一个缺陷靠结构体去解决。结构体允许其中的元素的类型不相同,因此解决了数组的第一个缺陷。
第二个缺陷,我们希望数组的大小能够实时扩展。普通的数组显示不行。我们可以对数组进行封装以达到这种目的。我们还可以使用一个新的数据结构来解决。这个新的数据结构就是链表。几乎可以这样理解:链表就是一个元素个数可以实时变化的数组
箭头表示指针。
这样就能很容易的插入数据。
链表的形式
Donnerstag, 1. Oktober 2015
指向结构体变量的指针
# include <iostream>
using namespace std;
struct student
{
int id_num;
char name[10];
};
int main()
{
student mike = {123,{'m','i','k','e','\0'}};
student *one = &mike;
cout <<(*one).id_num<<" "<<(*one).name<<endl;
cout << one->id_num<<" "<< one->name<<endl;//前面这两种表达都一样。->是指 // 向运算符(有指针的时候用)
return 0;
}
using namespace std;
struct student
{
int id_num;
char name[10];
};
int main()
{
student mike = {123,{'m','i','k','e','\0'}};
student *one = &mike;
cout <<(*one).id_num<<" "<<(*one).name<<endl;
cout << one->id_num<<" "<< one->name<<endl;//前面这两种表达都一样。->是指 // 向运算符(有指针的时候用)
return 0;
}
结构体做函数返回值
#include <iostream>
using namespace std;
struct student
{
int id_num;
char name[10];
};
student newone()//结构体类型的函数
{
student one = {123,{'M','i','k','e','\0'}};
return one;//结构体做返回值相当于copy一份给调用者;
}
int main()
{
student mike = newone();
cout<<mike.id_num<< " " <<mike.name<<endl;
//cout<< newone()<<endl;是不可以的
return 0;
}
using namespace std;
struct student
{
int id_num;
char name[10];
};
student newone()//结构体类型的函数
{
student one = {123,{'M','i','k','e','\0'}};
return one;//结构体做返回值相当于copy一份给调用者;
}
int main()
{
student mike = newone();
cout<<mike.id_num<< " " <<mike.name<<endl;
//cout<< newone()<<endl;是不可以的
return 0;
}
结构体变量做函数参数
#include<iostream>
using namespace std;
struct student
{
int id_num;
char name[10];
};
void renew(student one)
{
one.id_num = 20130000 + one.id_num;
for(int i = 0;one.name[i] != '\0';i++)
one.name[i] = toupper(one.name[i]);
cout << one.id_num<<" "<<one.name<<endl;
}
int main()
{
student mike = { 123,{'m','i','k','e','\0'}};
renew(mike);//把一个结构体当作变量传递给一个函数的的时候,实际是把这个 // 结构体变量所有的值都copy一份给这个函数.
cout<<mike.id_num<<" "<<mike.name<<endl;
return 0;
}
using namespace std;
struct student
{
int id_num;
char name[10];
};
void renew(student one)
{
one.id_num = 20130000 + one.id_num;
for(int i = 0;one.name[i] != '\0';i++)
one.name[i] = toupper(one.name[i]);
cout << one.id_num<<" "<<one.name<<endl;
}
int main()
{
student mike = { 123,{'m','i','k','e','\0'}};
renew(mike);//把一个结构体当作变量传递给一个函数的的时候,实际是把这个 // 结构体变量所有的值都copy一份给这个函数.
cout<<mike.id_num<<" "<<mike.name<<endl;
return 0;
}
结构体变量赋值
#include<iostream>
using namespace std;
struct student
{
int id_num;
char name[10];
};//这个分号一定不要忘记
int main()
{
student mike1 = {123, {'m','i','k','e','\0'}};
student mike2;
mike2 = mike1;//把结构体变量mike1赋值给mike2;把mike1 copy一份给mike2;
mike2.id_num = 20130000 + mike2.id_num;
for(int i = 0;mike2.name[i]! = '\0';i++ )
{
mike2.name[i] = toupper(mike2.name[i]);
cout << mike1.id_num <<" "<<mike1.name<<endl;
cout << mike2.id_num<<" "<<mike2.name<<endl;
}
retrun 0;
}
using namespace std;
struct student
{
int id_num;
char name[10];
};//这个分号一定不要忘记
int main()
{
student mike1 = {123, {'m','i','k','e','\0'}};
student mike2;
mike2 = mike1;//把结构体变量mike1赋值给mike2;把mike1 copy一份给mike2;
mike2.id_num = 20130000 + mike2.id_num;
for(int i = 0;mike2.name[i]! = '\0';i++ )
{
mike2.name[i] = toupper(mike2.name[i]);
cout << mike1.id_num <<" "<<mike1.name<<endl;
cout << mike2.id_num<<" "<<mike2.name<<endl;
}
retrun 0;
}
Abonnieren
Kommentare (Atom)