上海交通大学交大密西根 ■■ 联合学院·一 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