DTL

Array

Introduction

An array is the most basic data structure that allows us to store different variables of similar data type in a contiguous memory location. They exist in all programming languages, and are used as the basis for most other data structures.

Advantages

  • Code optimization: We can retrieve or sort the data efficiently.
  • Random access: We can get any data located at an index position.

Disadvantages

  • Size limit: We can store only the fixed size of elements in the array. So, it doesn't increase or decrease its size at runtime.

Using array with DTL

Different ways to initialize an array

  1. Initializing an empty array:
  2. #include "array.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::array<int, 6> myStaArr;
    
      // Pointer
      dby::array<int, 6>* myPtrArr = new dby::array<int, 6>();
      delete myPtrArr;
    
      return 0;
    }
  3. Initializing from an array:
  4. #include "array.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::array<int, 5> myStaArrOne = { 1, 2, 3, 4, 5 };
      dby::array<int, 5> myStaArrTwo { 1, 2, 3, 4, 5 };
    
      // Pointer
      dby::array<int, 5>* myPtrArr = new dby::array<int, 5>({ 1, 2, 3, 4, 5 });
      delete myPtrArr;
    
      return 0;
    }
  5. Initializing with a particular value:
  6. #include "array.hpp"
    #include <iostream>
    
    int main() {
      // Static
      dby::array<int, 5> myStaArr(99); 
      // [ 99, 99, 99, 99, 99 ]
    
      // Pointer
      dby::array<int, 5>* myPtrArr = new dby::array<int, 5>(99);
      // [ 99, 99, 99, 99, 99 ]
      delete myPtrArr;
      return 0;
    }

Accessing stored data in the array

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

Iterating over an array

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

Other array methods

  • front(): Returns a reference to the first element in the array.
  • back(): Returns a reference to the last element in the array.
  • front(): Returns a reference to the first element in the array.
  • back(): Returns a reference to the last element in the array.
  • fill(T value): Fill all the indexes of the array with a similar value.
  • reverse(): Reverses the order of the elements in the array.
  • 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 array 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 array satisfies a 'condition' function.
  • count_if(bool(*condition)(T value)): Returns the number of elements in the array for which the 'condition' function returns true