[go: up one dir, main page]

0% found this document useful (0 votes)
9 views5 pages

RJ OOP Unit 6

The document discusses the purpose of iterators and algorithms in C++, highlighting that iterators enable sequential access to container elements while hiding their internal structure, and algorithms provide efficient data manipulation methods. It also outlines the different types of STL containers, including sequence, associative, unordered, and container adaptors, along with their respective classes. Additionally, it covers the functions of the std::vector and types of iterators available in the STL, such as forward, bidirectional, random access, and range-based iteration.

Uploaded by

Sarita Dhakane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

RJ OOP Unit 6

The document discusses the purpose of iterators and algorithms in C++, highlighting that iterators enable sequential access to container elements while hiding their internal structure, and algorithms provide efficient data manipulation methods. It also outlines the different types of STL containers, including sequence, associative, unordered, and container adaptors, along with their respective classes. Additionally, it covers the functions of the std::vector and types of iterators available in the STL, such as forward, bidirectional, random access, and range-based iteration.

Uploaded by

Sarita Dhakane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1. What is purpose of iterator And Algorithm?

Q. What Is Mejor Components Of STL?


--- > Iterators:
An iterator is an object that enables traversal through a container or collection of data. It
allows you to access elements of the container sequentially without exposing the
underlying implementation details. Iterators provide a uniform interface for iterating over
different data structures, such as lists, arrays, dictionaries, and more.
The main purposes of iterators are:
1) Sequentially accessing elements of a container.
2) Hiding the internal structure of a container.
3) Providing a common interface for iteration across different data structures.
By using iterators, you can write code that is independent of the specific container type,
making your code more reusable and modular. Iterators typically provide methods like
next() or support the iter() function to iterate over the collection.
Algorithms:
Algorithms refer to a set of well-defined steps or procedures for solving a specific problem
or performing a particular task. In the context of programming, algorithms are commonly
used to manipulate and process data in collections or containers
1) Performing common operations on collections, such as sorting, searching, filtering,
and transforming data.
2) Standardizing and providing efficient implementations for common data processing
tasks.
3) Improving code readability, maintainability, and performance by using proven
algorithms instead of reinventing the wheel.
Algorithms are designed to solve specific problems efficiently and accurately. They can be
applied to various data structures and can significantly simplify the process of manipulating
and transforming data in collections.
Q2. what is stl list different types of stl container??
Q. what is container .? List the container classes in c++ , Explain ??
--- > STL (Standard Template Library) is a library in C++ that provides a collection of generic
algorithms and container classes. One of the container classes in STL is `std::list`.
1. Sequence Containers:
- `std::vector`: A dynamically resizable array.
- `std::deque`: A double-ended queue that allows efficient insertion and deletion at both
ends.
- `std::list`: A doubly-linked list for efficient insertion and deletion at any position.
2. Associative Containers:
- `std::set`: A container that stores unique elements in a sorted order.
- `std::map`: A container that stores key-value pairs in a sorted order based on the keys.
- `std::multiset`: A container that allows duplicate elements in a sorted order.
- `std::multimap`: A container that allows duplicate key-value pairs in a sorted order based
on the keys.
3. Unordered Containers:
- `std::unordered_set`: A container that stores unique elements in an unordered manner.
- `std::unordered_map`: A container that stores key-value pairs in an unordered manner.
- `std::unordered_multiset`: A container that allows duplicate elements in an unordered
manner.
4. Container Adaptors:
- `std::stack`: A container adaptor that provides LIFO (Last-In, First-Out) behavior.
- `std::queue`: A container adaptor that provides FIFO (First-In, First-Out) behavior.
- `std::priority_queue`: A container adaptor that provides access to elements based on
priority..
Q3. Write A program to Implement Map
in STL
int main() {
#include <iostream>
Map map;
#include <map>
map.insert(1, "John");
class Map {
map.insert(2, "Alice");
private:
map.insert(3, "Bob");
std::map<int, std::string> data;
map.displayAll();
public:
std::cout << "Value at key 2: " <<
void insert(int key, const std::string& map.get(2) << std::endl;
value) {
map.remove(1);
data.insert(std::make_pair(key,
map.displayAll();
value));
return 0;
}
}
void remove(int key) {
data.erase(key);
}
std::string get(int key) {
auto it = data.find(key);
if (it != data.end()) {
return it->second;
}
return "";
}
void displayAll() {
for (const auto& entry : data) {
std::cout << "Key: " << entry.first <<
", Value: " << entry.second << std::endl;
}
}
};
Q4. STATE Function Of Vactor STL. Write program to Explain The Same?
--- > The `std::vector` is a commonly used container in the STL (Standard Template Library)
1. `push_back()` - Adds an element to the end of the vector.
2. `pop_back()` - Removes the last element from the vector.
3. `size()` - Returns the number of elements in the vector.
4. `empty()` - Checks if the vector is empty.
5. `at()` - Accesses the element at a specified index with bounds checking.
6. `front()` - Returns a reference to the first element.
7. `back()` - Returns a reference to the last element.
8. `clear()` - Removes all elements from the vector.
9. `erase()` - Removes elements at a specified position or range.
10. `insert()` - Inserts elements at a specified position or range.
11. `resize()` - Changes the size of the vector.

#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..

You might also like