Array - C P P

Array


  • An Array is a data structure used to store blocks of information in contiguous memory allocation of same data type
  • Subarray
  • Subsequence
    • dataType A[N] => Declare an Array of size "N"
      • Giving a Aiable size in an array is a bad practice, as memory is given at run-time in stack
      • Size should be known at compile-time, as size required in run-time may be bigger than capacity of stack
    • dataType A[2] = {value1, value2} => Initialize an Array of size "n" with "n" value
    • dataType A[5] = {value1, value2} => Initialize an Array of size "n" with first "n" value, Rest will be initialized by "0"
    • dataType A[] = {value1, value2, value3} => Initialize an Array with "n" value
    • dataType A[n] = {} => Initialize an Array of size "n" with all value equal to "0"
    • dataType A[n] = {0} => Initialize an Array of size "n" with all value equal to "0", For any value other than "0" can use loops
    • fill_n (A, n, value); = {0} => Initialize an Array till first "n" elements with all value equal to "value"
    • next_permutation(n, m) => Returns next lexicographically greater value for given array of value, Parameter "n" is starting and "m" is ending index
    • functionName(A) => When calling a function, we send the starting address of the array, Value will get changed in the array if updated in function
  • STL Commands
    • #include<array> => Library to include
      • Generally takes O(1) time complexity
    • array<int, n> A = {value1, value2} => Initialize
    • A.size() => Returns size of array
    • A.at(n) => Returns value at nth position
      • A[n] => Returns value at nth position
    • A.empty() => Returns boolean value true is array is empty
    • A.front() => Returns first element of the array
    • A.back() => Returns last element of the array
  • Loops
    • For Loop
          for (int i = 0; i < arraySize; ++i) {
              cout << A[i] << " ";
          }
      
    • Range-based
          for(auto i: A) {
              cout << A[i] << " ";
          }
      
    • For Each
          for_each(A, A + arraySize, [](int i) {
              cout << A[i] << " ";
          });
      
    • Iterator
          for(auto itr = begin(A); itr != end(A); ++itr) {
              cout << *itr << endl;
          }
      

Character Array


  • char A[N] => Declaring a character array
  • char* A[3] = {"value1", "value2", "value3"}
  • string A[3] = {"value1", "value2", "value3"}
  • char A[3][10] = {"value1", "value2", "value3"}
  • array<string> A{"value1", "value2", "value3"};
  • vector<string> A{"value1", "value2", "value3"};
  • cin >> A; => Taking input
    • Adds a null character /0 just like string in the end, Used as a terminator
    • Stops execution when given a newline/space/tab

2D Array


  • Mapping 2D array to 1D array
    • Index of 1D = col * i + j = = Total Column * Row + Column
    • Row index in 2D = i/col
    • Col index in 2D = i % col
  • We need to specify the column size when passing a 2D array as a parameter
    • void func(int A[][N]) {} => To be able to calculate the element by pointer
      • A[2][3] = *(&A[0][0] + 2*N + 3)
    • *(A + i*cols + j) => Use pointers instead
  • Commands
    • dataType A[n][m] => Declaring an 2D Array of n row and m column
    • dataType A[n][m] = {{value1}, {value2}} => Initialize an 2D Array
    • dataType A[n][m] = {value1, value2, value3, value4} => Initialize an 2D Array Row-wise
    • cin >> A[n][m] => Taking input
    • cout << A[n][m] => Getting output of nth row and mth column
  • Loops
    • For Loop
          for (int i = 0; i < R; ++i) {
              for (int j = 0; j < C; ++j) {
                  cout << A[i][j] << " ";
              }
          }
      
    • Range-based
          for (auto &row: A) {
              for (auto i: row) {
                  cout << i << " ";
              }
              cout << endl;
          }
      
Share: