DTL

Vector

Introduction

Vectors allows us to store different variables of similar data type in a contiguous memory location with the ability to resize itself when an element is inserted or deleted. They are also known as dynamic arrays.

Advantages

  • Code optimization: We can retrieve or sort the data efficiently.
  • Random access: We can get any data located at an index position.
  • Resizable: Can increase or decrease its size at runtime.

Disadvantages

  • Inefficent when inserting or removing an element.

Using vector with DTL

Different ways to initialize a vector

  1. Initializing an empty vector:
  2. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec;
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>();
      delete myPtrVec;
    
      return 0;
    }
  3. Initializing like arrays:
  4. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVecOne = { 1, 2, 3, 4, 5 };
      dby::vector<int> myStaVecTwo { 1, 2, 3, 4, 5 };
      
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 1, 2, 3, 4, 5 });
      delete myPtrVec;
    
      return 0;
    }
  5. Specifying initial size:
  6. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec(5);
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>(5);
      delete myPtrVec;
    
      return 0;
    }
  7. Specifying initial size and initializing all values:
  8. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec(5, 99);
      // myStaVec = [ 99, 99, 99, 99, 99 ]
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>(5, 99);
      // myPtrVec = [ 99, 99, 99, 99, 99 ]
      delete myPtrVec;
    
      return 0;
    }
  9. Initializing with an existing array:
  10. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      int array[] = { 1, 2, 3, 4, 5 };
      std::size_t size = sizeof(array) / sizeof(array[0]);
    
      // Static
      dby::vector<int> myStaVec(array, size);
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>(array, size);
      delete myPtrVec;
      
      return 0;
    }

Accessing stored data in the vector

  1. Using brackets:
  2. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec = { 27, 14, 83, 95, 60 };
      
      std::cout << "Element at pos 0: " << myStaVec[0] << '\n';
      myStaVec[0] = 21;
      std::cout << "Element at pos 0: " << myStaVec[0] << "\n\n";
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 27, 14, 83, 95, 60 });
      
      std::cout << "Element at pos 0: " << (*myPtrVec)[0] << '\n';
      (*myPtrVec)[0] = 21;
      std::cout << "Element at pos 0: " << (*myPtrVec)[0] << '\n';
    
      delete myPtrVec;
      return 0;
    }
  3. Using at():
  4. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec = { 27, 14, 83, 95, 60 };
      
      std::cout << "Element at pos 0: " << myStaVec.at(0) << '\n';
      myStaVec.at(0) = 12;
      std::cout << "Element at pos 0: " << myStaVec.at(0) << "\n\n";
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 27, 14, 83, 95, 60 });
      
      std::cout << "Element at pos 0: " << myPtrVec->at(0) << '\n';
      myPtrVec->at(0) = 12;
      std::cout << "Element at pos 0: " << myPtrVec->at(0) << '\n';
    
      delete myPtrVec;
      return 0;
    }

Iterating over a vector

  1. For loop:
  2. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec = { 1, 2, 3, 4, 5 };
      
      for (std::size_t i = 0; i < myStaVec.size(); ++i) {
        std::cout << myStaVec[i] << ' ';
      }
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 1, 2, 3, 4, 5 });
      
      for (std::size_t i = 0; i < myPtrVec->size(); ++i) {
        std::cout << myPtrVec->at(i) << ' ';
      }
    
      delete myPtrVec;
      return 0;
    }
  3. Range-based For loop:
  4. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec = { 1, 2, 3, 4, 5 };
      
      for (int val : myStaVec) {
        std::cout << val << ' ';
      }
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 1, 2, 3, 4, 5 });
      
      for (int val : *myPtrVec) {
        std::cout << val << ' ';
      }
    
      delete myPtrVec;
      return 0;
    }
  5. For each loop:
  6. #include "vector.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::vector<int> myStaVec = { 1, 2, 3, 4, 5 };
      myStaVec.for_each([](int val){ std::cout << val << ' '; });
    
      // Pointer
      dby::vector<int>* myPtrVec = new dby::vector<int>({ 1, 2, 3, 4, 5 });
      myPtrVec->for_each([](int val){ std::cout << val << ' '; });
    
      delete myPtrVec;
      return 0;
    }

Other vector methods

  • empty(): Returns whether the vector is empty or not.
  • begin(): Returns a pointer to the first element in the vector.
  • end(): Returns a pointer to the theoretical element that follows the last element in the vector.
  • front(): Returns a reference to the first element in the vector.
  • back(): Returns a reference to the last element in the vector.
  • push_back(T value): Push an element into the vector from the back.
  • emplace(T value, std::size_t index): Inserts a new element at the specified position.
  • pop_back(): Pop or remove an element from the back.
  • resize(std::size_t new_size): Resizes the vector so that it contains 'new_size' elements.
  • erase(std::size_t index): Remove an element from the vector at the specified position.
  • clear(): Remove all the elements of the vector.
  • reverse(): Reverses the order of the elements in the vector.
  • filter(bool(*condition)(T value), void(*doThis)(T value)): If the 'condition' function is true it applies the 'doThis' function.
  • search(T value, bool(*compare)(T first_val, T second_val)): Find if a value is stored in the vector by making a comparison. 'Compare' is an optional parameter to modify the compare behavior between two elements.
  • search_if(bool(*condition)(T value)): Find if any element of the vector satisfies a 'condition' function.
  • count_if(bool(*condition)(T value)): Returns the number of elements in the vector for which the 'condition' function returns true