logo资料库

SSD5:数据结构.pdf

第1页 / 共140页
第2页 / 共140页
第3页 / 共140页
第4页 / 共140页
第5页 / 共140页
第6页 / 共140页
第7页 / 共140页
第8页 / 共140页
资料共140页,剩余部分请下载后查看
[Trial version] Unit 1. Transitioning to C++
[Trial version] 1.1 C++ Introduced
[Trial version] C++ Introduced
[Trial version] C++ Background
[Trial version] Compiling and Running a C++ Program
[Trial version] 1.2 Data Structures and Algorithms in C++
[Trial version] Data Structures and Algorithms in C++
[Trial version] What are Data Structures and Algorithms?
[Trial version] Problem Solving with Data Structures and Algorithms
[Trial version] 1.3 Basic C++ Programming
[Trial version] Basic C++ Programming
[Trial version] Data Types
[Trial version] Specifying Classes
[Trial version] Input and Output
[Trial version] The Preprocessor
[Trial version] A Side-By-Side Example
[Trial version] 1.4 Memory Management
[Trial version] Memory Management
[Trial version] Pointers
[Trial version] Parameter Passing Mechanisms
[Trial version] Dynamic Memory Management
[Trial version] 1.5 Mechanisms for Code Reuse and Abstraction
[Trial version] 1.5 Mechanisms for Code Reuse and Abstraction
[Trial version] Inheritance
[Trial version] Polymorphism
[Trial version] Templates
[Trial version] Exception Handling
[Trial version] Unit 2. Linear Structures
[Trial version] 2.1 Using the Standard string class
[Trial version] Using the Standard string class
[Trial version] 2.2 The STL and Basic Containers
[Trial version] The STL and Basic Containers
[Trial version] Introduction to the Standard Template Library
[Trial version] Using the STL vector Container
[Trial version] Using the STL deque Container
[Trial version] 2.3 Linked Lists
[Trial version] Linked Lists
[Trial version] A Linked Structure
[Trial version] A C++ Implementation
[Trial version] Using the STL list Container
[Trial version] 2.4 Queues
[Trial version] Queues
[Trial version] First-In, First-Out
[Trial version] Using the STL queue Adapter
[Trial version] 2.5 Stacks
[Trial version] Stacks
[Trial version] Last-In, First-Out
[Trial version] Using the STL stack Adapter
[Trial version] Unit 3. Recursion
[Trial version] 3.1 The Basic Concept of Recursion
[Trial version] The Basic Concept of Recursion
[Trial version] 3.2 Problem Solving with Recursion
[Trial version] Problem Solving with Recursion
[Trial version] Divide and Conquer
[Trial version] Backtracking
[Trial version] Unit 4. Sorting, Searching, and Complexity
[Trial version] 4.1 Sorting and Searching
[Trial version] Sorting and Searching
[Trial version] Linear vs. Binary Search
[Trial version] Basic Sorting Algorithms
[Trial version] Fast Sorting Algorithms
[Trial version] Using Hash Tables
[Trial version] 4.2 Complexity
[Trial version] Complexity
[Trial version] Asymptotic Analysis
[Trial version] Unit 5. Trees and Graphs
[Trial version] 5.1 Trees
[Trial version] Trees
[Trial version] Introduction to Trees
[Trial version] Using Tree Structures
[Trial version] Using the Tree Based STL Containers
[Trial version] 5.2 Graphs
[Trial version] Graphs
[Trial version] Introduction to Graphs
[Trial version] Fundamental Graph Algorithms
[Trial version] Data Structures and Algorithms
[Trial version] Readings
[Trial version] Coding Conventions
[Trial version] The Auction Project
###E###
###E###
###E###
###E###
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html an executing copy of a program. Edit The first step involved in taking a program from source to execution is the creation of a file that contains the source code. The program that is used to create a source code file is called an editor. Editors that programmers use range from simple and generic text editors (such as Notepad in Windows or vi in UNIX) to sophisticated editors that typically come as part of Integrated Development Environments (IDEs). These sophisticated editors are quite powerful since they provide functionality that is geared towards the creation and maintenance of source code. The syntax coloring in Listing 1 is one example of the type of functionality that sophisticated editors provide. Preprocess Preprocessing involves the modification of C++ source code files prior to compilation. The first two lines from Listing 1 contain commands called preprocessor directives, which inform the preprocessor to perform some action. In Listing 1, the first two lines instruct the preprocessor to include the contents of files into the program source code. Preprocessing also involves the text substitution of macros. A more detailed discussion of the preprocessor can be found in 1.3.4 The Preprocessor. Compile Preprocessing usually is performed automatically, just before the compile step. Compiling is a complex process that converts the preprocessed source code into object code. Part of the compile process involves verifying that the syntax of the source code is valid. Often, when a program is compiled (especially the first time it is compiled), something is wrong with the syntax. This is referred to as a "compile error." When faced with a compile error, a programmer must return to the first step of this process and edit the source code to remove the error. The software tool used to compile source code, not surprisingly, is known as a compiler. An example of a C++ compiler is the GNU Compiler Collection, or GCC. The GNU Compiler Collection actually compiles many different programming languages, one of which is C++. GCC is also free software. This compiler can be obtained through the Cygwin environment. Link Linking is a step that is typically performed by the same tool that compiles a program. The linking process involves combining the object code produced by the compiler with other precompiled library code. The result of the operation of the linker is an executable image. This executable image is a file that contains the compiled and linked object code of the program, stored in persistent storage (the hard drive). Execute After the program source code has been edited, preprocessed, compiled, and linked into an executable image, the program is ready to be executed, or run. Errors encountered at this point are known as "runtime errors," and are typically more difficult to correct than compile errors, since they may involve problems in the logic of the program. Page 5
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Compiling and Running in Cygwin Quite often, however, the steps preprocess, compile, and link are often informally grouped together and referred to as "compiling." It is easy to see why this is done when we consider that the tools that programmers use often perform these groups of related tasks together. For example, using GCC through Cygwin, we can preprocess, compile, and link all in one step. The following command line assumes the source code file is named hello.cpp. $ g++ hello.cpp Example 1 Compiling hello.cpp As a result of the execution of the above command, the source-code file hello.cpp will first be preprocessed, then compiled, and then linked to produce an executable image named a.exe. The default filename a.exe can be overridden using the -o compiler option. $ g++ hello.cpp -o hello.exe Example 2 Overriding the default output file name Additional options (-ansi and -pedantic) inform the compiler that it should only compile programs that are ANSI C++ compliant and that it should issue warnings when it encounters code that violates the ANSI standard. Using these options helps to locate and prevent certain types of errors. $ g++ -ansi -pedantic hello.cpp -o hello.exe Example 3 Conforming to the ANSI standard We can run the program now that it has been compiled into an executable. From the command shell, we issue the following command: $ ./hello.exe Example 4 Running the compiled program The dot and slash preceding the filename may not be necessary. It simply instructs the command shell to look for the file hello.exe in the current directory. The following screen shot depicts the results of the compilation and execution of the program. Page 6
###E###
###E###
分享到:
收藏