*********************************************************
原文:http://www.cplusplus.com/doc/tutorial/
*********************************************************
数据结构体
|
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
struct product {
int weight;
float price;
} ;
product apple;
product banana, melon;
|
struct product {
int weight;
float price;
} apple, banana, melon;
|
apple.weight
apple.price
banana.weight
banana.price
melon.weight
melon.price
|
他们每一个的数据类型同这些成员他们本身的类型一致: apple.weight, banana.weight 和 melon.weight是int型;而apple.price, banana.price 和 melon.price 是 float型.
/
/ example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout <<
"Enter title: "
;
getline (cin,yours.title);
cout <<
"Enter year: "
;
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout <<
"My favorite movie is:/n "
;
printmovie (mine);
cout <<
"And yours is:/n "
;
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout <<
" ("
<< movie.year <<
")/n"
;
}
|
Enter title: Alien
Enter year: 1979
My favorite movie is:
2001 A Space Odyssey (1968)
And yours is:
Alien (1979)
|
// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define N_MOVIES 3
struct movies_t {
string title;
int year;
} films [N_MOVIES];
void printmovie (movies_t movie);
int main ()
{
string mystr;
int n;
for (n=0; n<N_MOVIES; n++)
{
cout <<
"Enter title: "
;
getline (cin,films[n].title);
cout <<
"Enter year: "
;
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
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: Blade Runner
Enter year: 1982
Enter title: Matrix
Enter year: 1999
Enter title: Taxi Driver
Enter year: 1976
You have entered these movies:
Blade Runner (1982)
Matrix (1999)
Taxi Driver (1976)
|
struct movies_t {
string title;
int year;
};
movies_t amovie;
movies_t * pmovie;
|
pmovie = &amovie;
|
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;
cout <<
"Enter title: "
;
getline (cin, pmovie->title);
cout <<
"Enter year: "
;
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
cout <<
"/nYou have entered:/n"
;
cout << pmovie->title;
cout <<
" ("
<< pmovie->year <<
")/n"
;
return 0;
}
|
Enter title: Invasion of the body snatchers
Enter year: 1978
You have entered:
Invasion of the body snatchers (1978)
|
pmovie->title
|
(*pmovie).title
|
两个表达式pmovie->title 和 (*pmovie).title都是有效的,而且都意味着我们在计算被叫做pmovie的指针指向的数据结构体的成员title。你必须清楚同:
*pmovie.title
|
*(pmovie.title)
|
表达式
|
含义
|
等价于
|
a.b
|
对象
a
的成员
b
|
|
a->b
|
被
a
指向的对象的成员
b
|
(*a).b
|
*a.b
|
对象
a
的成员
b
所指向的值
|
*(a.b)
|
s
truct movies_t {
string title;
int year;
};
struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;
friends_t * pfriends = &charlie;
|
c
harlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year
|