上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string

上海交通大学交大密西根 ■■ 联合学院·一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101 Introduction to Computer and Programming Vectors
Vg101 Introduction to Computer and Introduction to Computer and Programming Programming Vectors

上海交通大学交大密西根 联合学院· ■ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Limits on Array Array is not a primitive type,therefore many operations does not apply to array.For example, you cannot assign a array to another array You cannot compare between two arrays Array has fixed number of elements and cannot be expanded if there are more elements want to be added and cannot shrink if there will be less elements need to be put in. Array is not a class and has no internal data and operation related like size of the array
Limits on Array Limits on Array • Array is not a primitive type, therefore many operations does not apply to array. For example, – you cannot assign a array to another array – You cannot compare between two arrays • Array has fixed number of elements and cannot be expanded if there are more elements want to be added and cannot shrink if there will be less elements need to be put in. • Array is not a class and has no internal data and operation related like size of the array

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Introduction Today we will start another topic in C++: vectors. Vectors and Arrays Both vector and array are ordered collection of date item of the same type. Ordered We can refer to the elements by their position 1st.2nd… Same Type All ints,all doubles,all strings
Introduction Introduction • Today we will start another topic in C++: vectors

上海交通大学交大密西根 联合学院·一 ■ 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Library vector Type A vector is a library type.The library takes care of managing the memory associated with the use of a vector. To use a vector,we must include the appropriate header: #include
Library Library vector Type • A vector is a library type. The library takes care of managing the memory associated with the use of a vector. • To use a vector, we must include the appropriate header: #include

上海交通大学交大密西根 联合学院· 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Declare a vector A vector is a class template.Templates let us write a single class or function definition that can be used on a variety of types. ● To declare objects from a class template,we must supply additional information.In the case of vector,we must say what type of objects the vector will contain: vectorintVector(MAX_ELE_NUM); vectorlineVector(100);
Declare a Vector Declare a Vector • A vector is a class template. Templates let us write a single class or function definition that can be used on a variety of types. • To declare objects from a class template, we must supply additional information. In the case of vector, we must say what type of objects the vector will contain: vector intVector (MAX_ELE_NUM); vector lineVector (100);

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Template and Type A vector is not a type;it is a template that we can use to define any number of types. Each of vector type specifies an element type.Hence, vectorand vectorare types
Template and Type Template and Type • A vector is not a type; it is a template that we can use to define any number of types. • Each of vector type specifies an element type. Hence, vector and vector are types

上海交通大学交大密西根 联合学院· 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Operations on vectors v.empty():Returns true if v is empty; V.size():Returns number of elements in v v.push back(t):Adds element with value t to end of y v[n]:Returns element at position n in v 。 v1 v2:Replaces elements in v1 by a copy of elements in v2 v1 ==v2:Returns True if v1 and v2 are equal !=and >=Have their normal meanings
Operations on vectors Operations on vectors • v.empty(): Returns true if v is empty; • v.size(): Returns number of elements in v • v.push_back(t): Adds element with value t to end of v • v[n]: Returns element at position n in v • v1 = v2: Replaces elements in v1 by a copy of elements in v2 • v1 == v2: Returns True if v1 and v2 are equal • !=, , and >=: Have their normal meanings

上海交通大学交大密西根 联合学院· ■ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Subscripting a vector Like in an array,objects in a vector are not named. They can be accessed by using the subscript operator. The vector subscript operator takes a value and returns the element at that position in the vector.Elements in a vector are numbered beginning with 0.The following example uses a for loop to reset each element in the vector to 0: for i=0;i!=intVec.size ji++) intVec[i]=0; 为
Subscripting a vector Subscripting a vector • Like in an array, objects in a vector are not named. They can be accessed by using the subscript operator. • The vector subscript operator takes a value and returns the element at that position in the vector. Elements in a vector are numbered beginning with 0. The following example uses a for loop to reset each element in the vector to 0:

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Adding Elements to a vector The push_back operation takes an element value and adds that value as a new element at the back of a vector.It "pushes"an element onto the "back"of the vector: vector intVec; f∥empty vector for0=0;i10;i*+) intVec.push_back (i); //append to the vector console-printLine (intVec[i)'); B
Adding Elements to a Adding Elements to a vector • The push_back operation takes an element value and adds that value as a new element at the back of a vector. It "pushes" an element onto the "back" of the vector:

上海交通大学交大密西根 联合学院·一 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Common Pitfalls Subscripting Does Not Add Elements vector sint>intVec; //empty vector for0=0;i<10gi*+) intVec[i]=i;//disaster,intVec does not have memory reserved B Problem with this is you go beyond the boundary of the vector and it is a serious error and very hard to find it out
Common Pitfalls Common Pitfalls • Subscripting Does Not Add Elements • Problem with this is you go beyond the boundary of the vector and it is a serious error and very hard to find it out
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_I.运算符重载.pdf