`
linleizi
  • 浏览: 227504 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

C++数据结构学习笔记七

 
阅读更多
C++数据结构学习笔记七
一个数据结构是组合到同一定义下的一组不同类型的数据,各个数据类型的长度可能不同。
形式是:
"struct model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;
model_name是结构类型的模块名称。Object_name为可选参数,是一个或多个具体结构对象的标识。"
例如:
"struct products {
char name [3];
float price;
};
products apple;
products orange, melon;
定义了结构模块products,它包含两个域:name 和 price,每一个域是不同的数据类型。
声明了3个该类型的对象:apple, orange 和 melon。
另一种声明方式:
struct pooducts {
char name [30];
float price;
} apple, orange, melon;"
"操作对象的域:
apple.name
apple.price
orange.name
orange.price
melon.name
melon.price"
例子:
"#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct movies_t {
char title [50];
int year;
} mine, yours;
void printmovie (movies_t movie);

int main () {
char buffer [50];
strcpy (mine.title, ""2001 A Space 0dyssey"");
mine.year = 1968;
cout << Enter title: "";
cin.getline (yours.title, 50);
cout << ""Enter year: "";
cin.getline (buffer, 50);
yours.year = atoi (buffer);
cout << ""My favourite movie is:\n"";
printmovie (mine);
cout << ""And yours:\n"";
printmovie (yours);
return 0;
}

void printmovie (movies_t movie) {
cout << movie.title;
cout << ""("" << movie.year << "")\n"";
}
输出结果:
Enter title: Alien
Enter year: 1979
My favourite movie is:
2001 A Space Odyssey (1968)
And yours:
Alien (1979)"
结构的重要优点之一就是我们既可以单独引用它的元素,也可以引用整个结构数据块。
结构数组例子:
"#include <iostream.h>
#include <stdlib.h>
#define N_MOVIES 5

struct movies_t {
char title [50];
int year;
} films [N_MOVIES];

void printmovie (movies_t movie);
int main () {
char buffer [50];
int n;
for (n = 0; n < N_MOVIES; n++) {
cout << ""Enter title: "";
cin.getline (films[n].title, 50);
cout << ""Enter year: "";
cin.getline (buffer, 50);
films[n].year = atoi (buffer);
}
cout << ""\nYou have entered these movies:\n"";
for (n = 0; n < N_MOVIES; n++)
printmovie (films[n]);
return 0;
}

void printmovie (movies_t movie) {
cout << movie.title;
cout << ""("" << movie.year << "")\n"";
}
输出结果:Enter title: Alien
Enter year: 1979
Enter title: Blade Runner
Enter year: 1982
Enter title: Matrix
Enter year: 1999
Enter title: Rear Window
Enter year: 1954
Enter title: Taxi Driver
Enter year: 1975

You have entered these movies:
Alien (1979)
Blade Runner (1982)
Matrix (1999)
Rear Window (1954)
Taxi Driver (1975)"
结构指针
"指针必须被声明为一个指向结构的指针:
struct movies_t {
char title [50];
int year;
};
movies_t amovie;
movies_t * pmovie;
这里 amovie 是一个结构类型movies_t 的对象,而pmovie 是一个指向结构类型movies_t 的对象的指针。所以,同基本数据类型一样,以下表达式正确的:
pmovie = &amovie; "
例子:
"#include <iostream.h>
#include <stdlib.h>

struct movies_t {
char title [50];
int year;
};

int main () {
char buffer[50];

movies_t amovie;
movies_t * pmovie;
pmovie = & amovie;

cout << ""Enter title: "";
cin.getline (pmovie->title, 50);
cout << ""Enter year: "";
cin.getline (buffer, 50);
pmovie->year = atoi (buffer);

cout << ""\nYou have entered: \n"";
cout << pmovie->title;
cout << ""("" << pmovie->year << "")\n"";

return 0;
}
输出结果:Enter title: Matrix
Enter year: 1999

You have entered:
Matrix (1999)"
上面的代码中引入了一个重要的操作符:->。这是一个引用操作符,常与结构或类的指针一起使用,以便引用其中的成员元素,这样就避免使用很多括号。例如,我们用:
pmovie->title
来代替:
(*pmovie).title
结构嵌套
"结构可以嵌套使用,即一个结构的元素本身又可以是另一个结构类型。例如:
struct movies_t {
char title [50];
int year;
}

struct friends_t {
char name [50];
char email [50];
movies_t favourite_movie;
} charlie, maria;
friends_t * pfriends = &charlie;
可以使用下面表达式:
charlie.name
maria.favourite_movie,title
charlie.favourite_movie.year
pfriends->favourite_movie.year"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics