结构struct
struct test{
char a[10];
int b;
}abc = {"abcef",5};
记录一下,结构体的赋值可以按位来赋值,如果结构后边的}后没分号,可以接写结构体变量名,并赋值。
#include <iostream>
using namespace std;
struct test //定义了一个名为test的结构体
{
int a, b; // 定义了结构体的成员a和b
};
//结构和类后边的}后都要加上";"这一点和c#不同
void main()
{
test pn1;
test pn2; //定义了两个结构体的变量 pn1和pn2
pn2.a = 10; //同下
pn2.b=3; //能过成员操作符点(.)给结构体变量pn2中的成员b赋值。
pn1 = pn2; //把pn2中的所有成员值copy给具有相同结构的结构体变量pn1.
cout << pn1.a << "|" << pn1.b << endl; //打出pn1的两个成员的值
cout << pn2.a << "|" << pn2.b << endl; //打出pn2

test *point; //定义结构指针
point = &pn2; //指针指向结构体变量pn2的内存地址. &是引用符
cout << pn2.a << "|" << pn2.b << endl;
point->a = 99; //能过结构指针修改结构体变量pn2成员a的值
cout << pn2.a << "|" << pn2.b << endl;
cout << point->a << "|" << point->b << endl;
cin.get();
/* 结构的声明:
struct 结构名
{
成员1
成员2
};
结构体声明的时候不点内存空间,只有当你用你定义的结构体定义结构体变量的时候才分配内存
结构指针通过 ->符号来访问成员。
把结构理解成一种自己定义的数据类型就可以了,和int double没有区别的类型。
*/
}
#include <iostream>
#include <string>
using namespace std;
struct test //定义结构
{
char name[10];
float score;
};
void print_score(test pn) //以结构变量作为行参
{ cout << pn.name << "|" <<pn.score << endl; }
void print_score(test *pn) // 定义了函数和上边的函数形成重载他们的区别在说形参类形不同 (以指针作为行参)
{ cout << pn->name << "|" << pn->score << endl;}
void main()
{
test a[2] = {{"marry",88.5},{"jarck",98.5}}; //声音了一个test形的数组并给他们赋值.
int num = sizeof(a)/sizeof(test);
for(int i=0;i != num;i++)
{ print_score(a[i]); }
for(int i=0;i != num;i++)
{ print_score(&a[i]); }
cin.get();
}
/* 没搞明白test a[2] = {{"marry",88.5},{"jarck",98.5}};是怎么赋值的,test中又没有构造函数?
void print_score(test *pn) 效率高好void print_score(test pn) 因为直接内存操作避免了栈空间开辟结构变量空间的需求
节省内存。
*/
#include <iostream>
using namespace std;
struct test
{
char name[10];
float score;
};
void print_score(test &pn) //以结构变量进行传递 //pn引用的是a[i]吗?
{ cout << pn.name << "|" << pn.score << endl; }
void main()
{
test a[2] = {{"marry",88.5},{"jrack",98.5}};
int num = sizeof(a)/sizeof(test);
for(int i = 0;i != num; i++)
{ print_score(a[i]); }
cin.get();
}
/*
利用引用传递的好处多,它的效率和指针相差无几,但引用操作方式和值传递基呼一样,种种优势都说明善用引用可以作到程序的
易读和易操作,它的优势尤其在结构很大的时候,避免传递变量很大的值,节省内存,提高效率。
*/
#include <iostream>
using namespace std;
struct test
{
char name[10];
float score;
};
void print_score(test &pn)
{ cout << pn.name << "|" << pn.score << endl; }
test get_score() //这个函数是为了让用户输入值 反回值是结构体变量
{
test pn;
cin >> pn.name >> pn.score; //给结构体变量的成员赋值。
return pn;
}
void main()
{
test a[2];
int num = sizeof(a)/sizeof(test);
for(int i = 0; i != num; i++)
{ a[i] = get_score(); }
cin.get();
for(int i = 0; i != num; i++)
{ print_score(a[i]); }
cin.get();
}
---------------------------------------------------------
#include <iostream>
using namespace std;
struct test
{
char name[10];
float score;
};
void print_score(test &pn)
{ cout << pn.name << "|" << pn.score << endl; }
void get_score(test &pn)
{ cin >> pn.name >> pn.score; }
void main()
{
test a[2];
int num = sizeof(a)/sizeof(test);
for(int i=0; i != num; i++)
{ get_score(a[i]); }
cin.get();
for(int i = 0; i != num; i++)
{ print_score(a[i]); }
cin.get();
}
/*
第二个效率要高于第一个,
get_score函数 代码一中调用的时候在内部要在栈空间开廦一个名为pn的结构体变量,程序pn的时候又在次在内存
栈内存空间内自动生成一个临时结构体变量temp,他是一个copy.
代码二中不开廦任何新的内存空间,也没有任何临时变量生成。
代码一 在main()函数中,必须对返回的结构体变量进行一次结构体变量与结构体变量的直接的相互赋值操作。
代码二中由于是通过内存地址直接操作,所认完全没有这一过程,提高了效率。
*/
#include <iostream>
using namespace std;
struct test
{
char name[10];
float score;
};
test a;
test &get_score(test &pn)
{
cin >> pn.name >> pn.score;
return pn;
}
void print_score(test &pn)
{ cout << pn.name << "|" << pn.score << endl; }
void main()
{
test &sp=get_score(a);
cin.get();
cout << sp.name << "|" << sp.score;
cin.get();
}
/*
调用get_score(a)结束并返回的时候,函数内部并没有临时变量产生,返回直接把全局结构变量的a的内存地址赋给结构引用sp。
*/

浙公网安备 33010602011771号