Data structures

A data structure is a group of data elements grouped together under one name. These data elements, known asmembers, can have different types and different lengths. Data structures can be declared in C++ using the following syntax:

struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names;

 

Where type_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces {}, there is a list with the data members, each one is specified with a type and a valid identifier as its name.

For example:

struct product { int weight; double price; } ; product apple; product banana, melon;

 

This declares a structure type, called product, and defines it having two members: weight and price, each of a different fundamental type. This declaration creates a new type (product), which is then used to declare three objects (variables) of this type: applebanana, and melon. Note how once product is declared, it is used just like any other type.

Right at the end of the struct definition, and before the ending semicolon (;), the optional field object_names can be used to directly declare objects of the structure type. For example, the structure objects applebanana, and melon can be declared at the moment the data structure type is defined: 

struct product { int weight; // 声明 属性 。structure type name double price; } apple, banana, melon; // 声明 物品。object of this type

 

In this case, where object_names are specified, the type name (product) becomes optional: struct requires either atype_name or at least one name in object_names, but not necessarily both.

It is important to clearly differentiate between what is the structure type name (product), and what is an object of this type (applebanana, and melon). Many objects (such as applebanana, and melon) can be declared from a single structure type (product).

Once the three objects of a determined structure type are declared (applebanana, and melon) its members can be accessed directly. The syntax for that is simply to insert a dot (.) between the object name and the member name. For example, we could operate with any of these elements as if they were standard variables of their respective types: 

apple.weight apple.price banana.weight banana.price melon.weight melon.price

 

Each one of these has the data type corresponding to the member they refer to: apple.weightbanana.weight, andmelon.weight are of type int, while apple.pricebanana.price, and melon.price are of type double.

Here is a real example with structure types in action:

 
// 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 = ; 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 ; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }

 

Enter title: Alien Enter year:  My favorite movie is:  A Space Odyssey () And yours is: Alien ()

 

The example shows how the members of an object act just as regular variables. For example, the member yours.year is a valid variable of type int, and mine.title is a valid variable of type string.

But the objects mine and yours are also variables with a type (of type movies_t). For example, both have been passed to function printmovie just as if they were simple variables. Therefore, one of the features of data structures is the ability to refer to both their members individually or to the entire structure as a whole. In both cases using the same identifier: the name of the structure.

Because structures are types, they can also be used as the type of arrays to construct tables or databases of them:

 
// array of structures
#include <iostream> #include <string> #include <sstream>
using namespace std; struct movies_t { string title; int year; } films []; void printmovie (movies_t movie); int main () { string mystr; int n; for (n=; n<; 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=; n<; n++) printmovie (films[n]); return ; }
void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }
 

 

 
Enter title: Blade Runner Enter year:  Enter title: The Matrix Enter year:  Enter title: Taxi Driver Enter year:  You have entered these movies: Blade Runner () The Matrix () Taxi Driver ()

 

 

Pointers to structures

Like any other type, structures can be pointed to by its own type of pointers:

 
struct movies_t { string title; int year; }; movies_t amovie; movies_t * pmovie;

 

Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects of structure typemovies_t. Therefore, the following code would also be valid:

pmovie = &amovie;

 

The value of the pointer pmovie would be assigned the address of object amovie.

Now, let's see another example that mixes pointers and structures, and will serve to introduce a new operator: the arrow operator (->):

 
// 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 ; }

 

Enter title: Invasion of the body snatchers Enter year:  You have entered: Invasion of the body snatchers ()

 

The arrow operator (->) is a dereference operator that is used exclusively with pointers to objects that have members. This operator serves to access the member of an object directly from its address. For example, in the example above:

pmovie->title

 

is, for all purposes, equivalent to: 
(*pmovie).title

 

Both expressions, pmovie->title and (*pmovie).title are valid, and both access the member title of the data structure pointed by a pointer called pmovie. It is definitely something different than:

 
*pmovie.title

which is rather equivalent to:

 
*(pmovie.title)

This would access the value pointed by a hypothetical pointer member called title of the structure object pmovie (which is not the case, since title is not a pointer type). The following panel summarizes possible combinations of the operators for pointers and for structure members:

Expression What is evaluated Equivalent
a.b Member b of object a  
a->b Member b of object pointed to by a (*a).b
*a.b Value pointed to by member b of object a *(a.b)

 

Nesting structures

Structures can also be nested in such a way that an element of a structure is itself another structure:

struct movies_t { string title; int year; }; struct friends_t { string name; string email; movies_t favorite_movie; } charlie, maria; friends_t * pfriends = &charlie;

After the previous declarations, all of the following expressions would be valid:

charlie.name maria.favorite_movie.title charlie.favorite_movie.year pfriends->favorite_movie.year

 

(where, by the way, the last two expressions refer to the same member).

(转) Data structures的更多相关文章

  1. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  2. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  3. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  4. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  5. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  6. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  7. 无锁数据结构(Lock-Free Data Structures)

    一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...

  8. [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构

    10.2 How would you design the data structures for a very large social network like Facebook or Linke ...

  9. Manipulating Data Structures

    Computer Science An Overview _J. Glenn Brookshear _11th Edition We have seen that the way data struc ...

  10. Objects and Data Structures

    Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...

随机推荐

  1. OpenGL ES 2.0 剪裁测试

    剪裁测试:可以在渲染时用来限制绘制区域,通过此技术可以在屏幕(帧缓冲)上指定一个矩形区域. //启用剪裁测试 GLES20.glEnable(GL10.GL_SCISSOR_TEST); //设置区域 ...

  2. 【0】Laravel 5.1 简介

    1.简介 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以 ...

  3. 阿里云Centos7使用yum安装MySQL5.6的正确姿势

    阿里云Centos7使用yum安装MySQL5.6 阿里云Centos7使用yum安装MySQL5.6 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tom ...

  4. [转发]Gulp开发教程(翻译)

    Building With Gulp =================== 转载出处 原文地址 翻译出处 对网站资源进行优化,并使用不同浏览器测试并不是网站设计过程中最有意思的部分,但是这个过程中的 ...

  5. Nutshell.ThreadWorkerPool .Net线程池设计

    功能描述: 支持创建多个线程池,并统一管理 支持不同线程池的容量控制,以及最少活动线程的设置 支持不同线程池中活动线程的闲时设置,即线程空闲时间到期后即自动被回收 结构设计: ThreadWorker ...

  6. java生产者消费者并发协作

    随着职务转变,代码荒废很久了,很多时间都是在沟通需求,作为一名技术员,不写代码就感觉是在自废武功,慢慢颓废了很多,今天重新回顾了下JAVA线程知识,基础知识就不梳理了,网上也很多,主要关键几个状态位( ...

  7. 为什么1Byte=8bit

    Byte是字节的意思,而字节在早期计算机内部是用标准ASCII码来表示的根据当时情况确定至多有128种需要表示的字符(当时是IBM的标准,现在普遍是255),也就是2的7次方用二进制的0和1来表示就需 ...

  8. 自学Python的点滴

    1.第一天 注释 ——任何在#符号右面的内容都是注释. 注释主要作为提供给程序读者的笔记. 程序应该包含这两行 #!/user/bin/python #Filename:**.py 2.在程序中打开P ...

  9. 关于Android界面编程与视图(View)组件

    UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...

  10. XJOI网上同步训练DAY2 T2

    [问题描述] 火车司机出秦川跳蚤国王下江南共价大爷游长沙.每个周末勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个