Vector - C P P

Theory


  • Vectors are dynamic arrays
  • They have the ability to resize itself when it gets filled
    • The size of the vector gets doubled each time when they get filled

STL Commands


  • #include<vector> => Library to include
  • vector<int> v => Initialize a vector, Memory allocated is 0
  • vector<int> v(n, m) => Initialize a vector of size 'n' with all values equal to 'm'
  • vector<int> v1(v) => Initialize a vector by copying another vector
  • vector<int> v[n] => Initialize a vector of array elements of size 'n'
  • vector<pair<int, int>> v => Initialize a vector containing a pair
  • V.push_back(val) => Insert element at the end
  • V.pop_back() => Returns the last element of the vector and removes it
  • V.at(n) => Returns value at nth position
    • v[n] => Returns value at nth position
  • V.capacity() => Returns size of memory allocated, Total number of element that can be inserted
  • V.size() => Returns number of element present
  • V.front() => Returns first element of the vector
  • V.back() => Returns last element of the vector
  • V.clear() => Size becomes 0 but not Capacity
  • V.begin() => Returns an iterator pointing to the first element in the vector
  • V.end() => Returns an iterator pointing to the theoretical element that follows the last element in the vector
  • V.size() => Returns the number of elements in the vector
  • V.max_size() => Returns the maximum number of elements that the vector can hold
  • V.capacity() => Returns the size of storage space currently allocated to the vector expressed as number of elements
  • V.resize(n) => Resizes the container so that it contains ‘n’ elements
  • V.empty() => Returns whether the container is empty
  • V.at(n) => Returns the value in that position
  • V.shrink_to_fit() => Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity
  • V.reference_operator[n] => Returns a reference to the element at position ‘n’ in the vector
  • V.assign() => It assigns new value to the vector elements by replacing old ones
  • V.insert() => It inserts new elements before the element at the specified position
  • V.erase(n) => Removes the element present at position, n should be an iterator pointing to the position
    • V.erase(n, m) => Removes the elements in the range from start to end inclusive of the start and exclusive of the end
  • sort(n, m, greater<int>()) => Sort the elements in descending order, third parameter is used to specify order in which elements are to be sorted
    • sort(n, m, functionName) => We can also write our own comparator boolean function and pass it as a third parameter
    • `sort(n, m, & {return condition; })
    • sort(n, m) => Sorts the elements in ascending, Parameter n is starting and m is ending index
  • V.clear() => It is used to remove all the elements of the vector container
  • accumulate(n, m, 0) => To get the sum of the vector
  • count(n, m, o) => Returns count of the number of elements
  • *max_element(n, m) => To get Max element of the vector, Returns iterator to the element
  • *min_element(n, m) => To get Min element of the vector, Returns iterator to the element
  • partial_sum(n, m, n) => To convert the vector into a prefix sum vector, Vector received by doing cumulative sum
    • V.emplace() => It extends the container by inserting new element at position
    • V.emplace_back() => It insert a new element in vector container, the new element is added to the end of the vector
  • Loops
    • Range-based
          for(auto i: v) {
              cout << i << " ";
          }
      
    • Iterator
          vector<int> :: iterator itr;
          for(itr = V.begin(); itr != V.end(); ++itr) {
              cout << *itr << endl;
          }
      

2D Vector


  • vector<vector<int>> v => Initialize a 2D vector
  • vector<vector<int>> v = {{}, {}, {}}; => Initialize and Declare
  • v[n][m] => Access the values
  • Loops
    • Range-based
          for (auto row: v) {
              for (auto i: row) {
                  cout << i << " ";
              }
              cout << endl;
          }
      
    • Iterator
          for (vector<vector<int>>::rowItr = V.begin(); rowItr != V.end(); ++rowItr) {
              for (vector<int>::iterator itr = rowItr->begin(); itr != rowItr->end(); ++itr) {
                  cout << *itr << " ";
              }
              cout << endl;
          }
      
Share: