上海交通大学交大密西根 联合学院 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vector syntax and rules You need to #include <vector>to use vectors. The name of a vector (e.g. gradeForStudent)follows the naming convention for identifiers. ·Vectors can be“global”or "local”. ·The“index”also called the“subscript”?is the thing that goes inside the ]'s
Vector syntax and rules Vector syntax and rules • You need to #include <vector> to use vectors. • The name of a vector (e.g. gradeForStudent) follows the naming convention for identifiers. • Vectors can be “global” or “local”. • The “index” also called the “subscript” is the thing that goes inside the [ ]’s
上海交通大学交大密西根 联合学院 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vector syntax and rules If you violate the bounds of a vector (write data either below it or above it),one of two things can happen - Either your program will crash,or The data occupying memory adjacent to the vector will be corrupted,the program will carry on and operate on or with corrupted data. At least in the first situation you know something is wrong.In the second you don't and that could be dangerous
Vector syntax and rules Vector syntax and rules • If you violate the bounds of a vector (write data either below it or above it), one of two things can happen – Either your program will crash, or – The data occupying memory adjacent to the vector will be corrupted, the program will carry on and operate on or with corrupted data. • At least in the first situation you know something is wrong. In the second you don’t and that could be dangerous
上海交通大学交大密西根 联合学院· UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vector syntax and rules The index of a vector starts from 0,it is therefore,i'th element of vector some Vector is someVector[i-1]. Since it expect an integer as an index/subscript,you can use integer expressions for it. When a vector is declared all of its elements are set to zero.You can initialize it by putting the default value as the second parameter of the vector as: vector <int>intV (100,-1);//all elements are set to -1 Once declared,you can think of vectors as regular variables.For example, vector<float>vector2 vectorl;
Vector syntax and rules Vector syntax and rules • The index of a vector starts from 0, it is therefore, i’th element of vector someVector is someVector[i - 1]. • Since it expect an integer as an index/subscript, you can use integer expressions for it. • When a vector is declared all of its elements are set to zero. You can initialize it by putting the default value as the second parameter of the vector as: vector <int> intV (100, -1); // all elements are set to -1 • Once declared, you can think of vectors as regular variables. For example, vector<float> vector2 = vector1;
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vectors and functions Remember that it is usually useful to think of vectors as simple variables.This is especially true for vectors and functions. Take all the rules you have learned about functions and variables and apply them to functions and vectors
Vectors and functions Vectors and functions • Remember that it is usually useful to think of vectors as simple variables. This is especially true for vectors and functions. Take all the rules you have learned about functions and variables and apply them to functions and vectors
上海交通大学交大密西根 联合学院一 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Passing vectors as Parameters You can pass a vector to a function just like a regular variable.It could be passed by value or passed by reference. It is strongly suggested that you should always pass a vector variable as a reference.If you don't want it to be changed in the function,specify it as a const
Passing Passing vectors as Parameters s as Parameters • You can pass a vector to a function just like a regular variable. It could be passed by value or passed by reference. • It is strongly suggested that you should always pass a vector variable as a reference. If you don’t want it to be changed in the function, specify it as a const