Vectors

Vectors

 
In C++, a vector is a container that can hold a dynamic array of elements of the same data type. It provides a convenient way to store and manipulate a collection of data, without having to manually manage memory allocation or deal with pointers.
 
Vectors are defined in the <vector> header file and are implemented as a template class. This means that you can create vectors to hold elements of any data type, such as integers, floating-point numbers, strings, or even custom data types that you define yourself.
 
Here's an example of how vectors can be used in real life:
Suppose you run a small retail store and you want to keep track of the number of sales for each item in your inventory. You can use a vector to store this information.
For example, let's say you have the following items in your inventory:
  • T-shirts
  • Hats
  • Shoes
You can create a vector to hold the number of sales for each item, like so:
 
#include <vector> #include <iostream> int main() { std::vector<int> sales = { 10, 5, 3 }; std::cout << "Sales for T-shirts: " << sales[0] << std::endl; std::cout << "Sales for Hats: " << sales[1] << std::endl; std::cout << "Sales for Shoes: " << sales[2] << std::endl; return 0; }
 
In this example, the sales vector is initialized with three values: 10, 5, and 3, which represent the number of sales for T-shirts, hats, and shoes, respectively. The sales[0] element refers to the number of sales for T-shirts, sales[1] refers to the number of sales for Hats, and sales[2] refers to the number of sales for Shoes.
As your store grows and your inventory changes, you can add or remove items from the vector or change the values of the elements as needed. Vectors are useful in this scenario because they allow you to easily add or remove elements without worrying about managing memory manually.
 
IMPLEMENTATION :
 
#include <iostream> #include <vector> using namespace std; int main() { // Initialize a vector of integers vector<int> vec; // Add elements to the vector vec.push_back(10); // {10} vec.push_back(20); // {10, 20} vec.push_back(30); // {10, 20, 30} // Access elements in the vector using [] cout << "Current vector elements: "; for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } cout << endl; // Access the first and last element of the vector cout << "First Element: " << vec.front() << endl; cout << "Last Element: " << vec.back() << endl; // Remove the last element of the vector vec.pop_back(); // {10, 20} // Insert an element in the middle of the vector vec.insert(vec.begin() + 1, 15); // {10, 15, 20} // Remove an element from the middle of the vector vec.erase(vec.begin() + 1); // {10, 20} // Sort the elements in the vector sort(vec.begin(), vec.end()); // {10, 20} // Check if the vector is empty if (vec.empty()) { cout << "Vector is empty" << endl; } else { cout << "Vector is not empty" << endl; } // Clear all of the elements of the vector vec.clear(); // {} // Check the size and capacity of the vector cout << "Size of the vector: " << vec.size() << endl; cout << "Capacity of the vector: " << vec.capacity() << endl; return 0; }
 
This implementation first initializes an empty vector of integers vec. It then adds elements to the vector using the push_back() function. The elements are accessed using the [] operator, and the first and last element of the vector are accessed using the front() and back() functions. The pop_back() function removes the last element of the vector, the insert() function adds an element to the middle of the vector, and the erase() function removes an element from the middle of the vector. The sort() function is used to sort the elements in the vector. The empty() function checks if the vector is empty, and the clear() function deletes all of the elements of the vector. Finally, the size and capacity of the vector are checked using the size() and capacity() functions, respectively.