RJ OOP Unit 6
RJ OOP Unit 6
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
// Adding elements using push_back()
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
// Accessing elements using at()
std::cout << "First element: " << numbers.at(0) << std::endl;
std::cout << "Last element: " << numbers.at(numbers.size() - 1) << std::endl;
// Checking if vector is empty
std::cout << "Is vector empty? " << (numbers.empty() ? "Yes" : "No") << std::endl;
// Removing elements using pop_back()
numbers.pop_back(); return 0;
Q5. What is An Iterator ? What Are The Types Of Iterator?
--- > In the STL (Standard Template Library) of C++, there are different types of iteration
techniques available to traverse and manipulate elements in containers
1. Forward Iteration:
Forward iteration allows traversing a container in a forward direction, visiting each
element once and proceeding to the next element. It is supported by containers like
`std::list`, `std::forward_list`, and `std::set`. The `begin()` and `end()` member functions of
these containers provide forward iterators.
2. Bidirectional Iteration:
Bidirectional iteration allows traversing a container in both forward and backward
directions. It is supported by containers like `std::list` and `std::set`. The `rbegin()` and
`rend()` member functions provide bidirectional iterators.
3. Random Access Iteration:
Random access iteration allows accessing elements in a container using indexing and
jumping directly to any element. It is supported by containers like `std::vector`, `std::deque`,
and `std::array`. The `begin()` and `end()` member functions of these containers provide
random access iterators.
4. Range-based Iteration:
Range-based iteration simplifies the iteration process by automatically inferring the
iterator type and handling the begin and end points. It is supported by all containers in the
STL and allows iterating over the elements in a container using a for-each loop..