上海交通大学交大密西根 ■■ 联合学院·一 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 <vector>
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 <vector>
上海交通大学交大密西根 联合学院· 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: vector<int>intVector(MAX_ELE_NUM); vector<LineT>lineVector(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<int> intVector (MAX_ELE_NUM); vector<LineT> lineVector (100);