Lecture Introduction to Vectors By Kamil Shaheen
Introduction to vector in C++ o Vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. o we can change the size of the vector during the execution of a program as per our requirements. o To use vectors, we need to include the vector header file in our program. o #include <vector>
Vector Declaration o vector<datatype> vector_name; o The type parameter <dataype> specifies the type of the vector. It can be any data type such as int ,float, char etc… o vector <int> num; o Here num is the name of the vector. o Notice that we have not specified the size of the vector during the declaration. This is because the size of a vector can grow dynamically so it is not necessary to
Vector Initialization  There are different ways to initialize a vector in C++.
Method-1  Initializer List vector<int> my_vector1={1,2,3,4,5};  Uniform Initialization vector<int> my_vector2 {1,2,3,4,5}; Here, we are initializing the vector by providing values directly to the vector. Now, both my_vector1, my_vector2 are initialized with values 1, 2, 3, 4, 5. Note: There maybe version issue in Dev C++
Method-2  Initialization vector<int> my_vector1(5,12); Here, 5 is the size of the vector and 12 is the value. This code creates an int vector with size 5 and initializes the vector with the value of 12. So, the vector is equivalent to: vector<int> my_vector1={12,12,12,12,12};
First Program of Vector
Basic Vector Operations Vector class provides various methods to perform different operations on vectors. Here some commonly used vector operations (CRUD): Add elements Access elements Change elements Remove elements
1- Add Elements to a Vector To add a single element into a vector, we use the push_back() function. It inserts an element into the end of the vector. For example, vector<int> my_vector1={1,2,3,4,5}; my_vector1.push_back(6); my_vector1.push_back(7);
2- Access Elements of a Vector • In C++, we use the index number to access the vector elements. Here, we use the at() function to access the element from the specified index. For example,
3- Update Vector Element • We can change an element of the vector using the same at() function.
4. Delete Elements Vectors •To delete a single element from a vector, we use the pop_back() function. For example,
Vector Functions
END

cpp programing language exercise Vector.ppt

  • 1.
  • 2.
    Introduction to vectorin C++ o Vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. o we can change the size of the vector during the execution of a program as per our requirements. o To use vectors, we need to include the vector header file in our program. o #include <vector>
  • 3.
    Vector Declaration o vector<datatype>vector_name; o The type parameter <dataype> specifies the type of the vector. It can be any data type such as int ,float, char etc… o vector <int> num; o Here num is the name of the vector. o Notice that we have not specified the size of the vector during the declaration. This is because the size of a vector can grow dynamically so it is not necessary to
  • 4.
    Vector Initialization  Thereare different ways to initialize a vector in C++.
  • 5.
    Method-1  Initializer List vector<int>my_vector1={1,2,3,4,5};  Uniform Initialization vector<int> my_vector2 {1,2,3,4,5}; Here, we are initializing the vector by providing values directly to the vector. Now, both my_vector1, my_vector2 are initialized with values 1, 2, 3, 4, 5. Note: There maybe version issue in Dev C++
  • 6.
    Method-2  Initialization vector<int> my_vector1(5,12); Here,5 is the size of the vector and 12 is the value. This code creates an int vector with size 5 and initializes the vector with the value of 12. So, the vector is equivalent to: vector<int> my_vector1={12,12,12,12,12};
  • 7.
  • 8.
    Basic Vector Operations Vectorclass provides various methods to perform different operations on vectors. Here some commonly used vector operations (CRUD): Add elements Access elements Change elements Remove elements
  • 9.
    1- Add Elementsto a Vector To add a single element into a vector, we use the push_back() function. It inserts an element into the end of the vector. For example, vector<int> my_vector1={1,2,3,4,5}; my_vector1.push_back(6); my_vector1.push_back(7);
  • 10.
    2- Access Elementsof a Vector • In C++, we use the index number to access the vector elements. Here, we use the at() function to access the element from the specified index. For example,
  • 12.
    3- Update VectorElement • We can change an element of the vector using the same at() function.
  • 13.
    4. Delete ElementsVectors •To delete a single element from a vector, we use the pop_back() function. For example,
  • 15.
  • 16.