上海交通大学交大密西根 ·联合学院一 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, vector<int>and vector<LineT>are 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<int> and vector<LineT> 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 <int>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