Vector (<vector>) Methods

Common Methods

  • begin(): Returns iterator to the first element.
    • iterator begin()
  • end(): Returns iterator to the past-the-end element.
    • iterator end()
  • push_back(): Appends an element to the end.
    • void push_back(const T& value)
  • pop_back(): Removes the last element.
    • void pop_back()
  • insert(): Inserts element before pos.
    • iterator insert(iterator pos, const T& value)
  • erase(): Removes element(s) from vector.
    • iterator erase(iterator pos)
    • iterator erase(iterator first, iterator last)
  • clear(): Removes all elements.
    • void clear()
  • size(): Returns number of elements.
    • size_t size()
  • empty(): Checks if vector is empty.
    • bool empty()
  • at(): Accesses element with bounds checking.
    • T& at(size_t pos)
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3};
    v.push_back(4);
    v.insert(v.begin() + 1, 10);
    v.erase(v.begin());

    for (auto it = v.begin(); it != v.end(); ++it)
        cout << *it << " ";
}

String (<string>) Methods

Common Methods

  • length(): Returns string length.
    • size_t length()
  • size(): Same as length().
    • size_t size()
  • substr(): Returns substring.
    • string substr(size_t pos, size_t count = npos) (count optional)
    • count is the length of the substring, or it defaults to the end of the string
  • find(): Finds substring.
    • size_t find(const string& str, size_t pos = 0) (pos optional)
    • pos is the starting position in the string where the search begins
  • replace(): Replaces portion of string.
    • string& replace(size_t pos, size_t len, const string& str)
  • append(): Appends string.
    • string& append(const string& str)
  • insert(): Inserts string at pos.
    • string& insert(size_t pos, const string& str)
  • erase(): Erases portion.
    • string& erase(size_t pos = 0, size_t len = npos) (pos & len optional)
#include <string>
#include <iostream>
using namespace std;

int main() {
    string s = "Hello World";
    s.replace(6, 5, "C++");
    cout << s << endl;
}

Map (<map>) Methods

  • insert(): Inserts key-value pair.
    • pair<iterator, bool insert(const value_type& val)
  • erase(): Removes element(s).
    • size_t erase(const key_type& key)
    • iterator erase(iterator pos)
  • find(): Finds element by key.
    • iterator find(const key_type& key)
  • count(): Returns number of elements with key.
    • size_t count(const key_type& key)
  • clear(): Removes all elements.
    • void clear()
  • size(): Returns number of elements.
    • size_t size()

Set (<set>) Methods

  • insert(): Inserts value.
    • pair<iterator, bool> insert(const value_type& val)
  • erase(): Removes element(s).
    • size_t erase(const key_type& key)
    • iterator erase(iterator pos)
  • find(): Finds value.
    • iterator find(const key_type& key)
  • count(): Checks existence.
    • size_t count(const key_type& key)
  • clear(): Removes all elements.
    • void clear()

Algorithm (<algorithm>) Methods

  • sort(): Sorts range.
    • void sort(RandomIt first, RandomIt last, Compare comp = ) (comp optional)
  • reverse(): Reverses range.
    • void reverse(BidirectionalIt first, BidirectionalIt last)
  • find(): Finds value.
    • InputIt find(InputIt first, InputIt last, const T& value)
  • count(): Counts occurrences.
    • size_t count(InputIt first, InputIt last, const T& value)
  • max_element(): Finds max.
    • ForwardIt max_element(ForwardIt first, ForwardIt last)
  • min_element(): Finds min.
    • ForwardIt min_element(ForwardIt first, ForwardIt last)

Stack (<stack>) Methods

Common Methods

  • push(): Inserts an element at the top.
    • void push(const T& value)
  • pop(): Removes the top element.
    • void pop()
  • top(): Accesses the top element.
    • T& top()
    • const T& top() const : for const stack, returns a read-only access to the top element
  • empty(): Checks if stack is empty.
    • bool empty()
  • size(): Returns the number of elements.
    • size_t size()
#include <iostream>
#include <stack>
using namespace std;


int main() {
stack<int> st;
st.push(10);
st.push(20);
cout << "Top: " << st.top() << endl; // 20
st.pop();
cout << "New Top: " << st.top() << endl; // 10
return 0;
}

Queue (<queue>) Methods

Common Methods

  • push(): Inserts an element at the back.
    • void push(const T& value)
  • pop(): Removes the front element.
    • void pop()
  • front(): Accesses the first element.
    • T& front()
    • const T& front() const : for const queue, returns a read-only access to the front element
  • back(): Accesses the last element.
    • T& back()
    • const T& back() const : for const queue, returns a read-only access to the back element
  • empty(): Checks if queue is empty.
    • bool empty()
  • size(): Returns the number of elements.
    • size_t size()
#include <iostream>
#include <queue>
using namespace std;


int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << "Front: " << q.front() << endl; // 1
cout << "Back: " << q.back() << endl; // 3
q.pop();
cout << "New Front: " << q.front() << endl; // 2
return 0;
}

Math (<cmath>) Functions

Common Functions

  • sqrt(): Returns square root.
    • double sqrt(double x)
  • pow(): Raises base to power exp.
    • double pow(double base, double exp)
  • abs(): Returns absolute value.
    • int abs(int x)
    • double fabs(double x)
  • ceil(): Rounds up.
    • double ceil(double x)
  • floor(): Rounds down.
    • double floor(double x)
  • round(): Rounds to nearest integer.
    • double round(double x)
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << sqrt(25) << endl;   // 5
    cout << pow(2, 3) << endl;  // 8
    cout << floor(2.9) << endl; // 2
    return 0;
}

Memory Management (<cstdlib> / new & delete)

C-style Functions

  • malloc(): Allocates memory.
    • void* malloc(size_t size)
  • calloc(): Allocates zero-initialized memory.
    • void* calloc(size_t num, size_t size)
  • realloc(): Resizes allocated memory.
    • void* realloc(void* ptr, size_t size)
  • free(): Frees allocated memory.
    • void free(void* ptr)

C++-style Operators

  • new: Allocates memory.
    • int* ptr = new int;
  • delete: Frees allocated memory.
    • delete ptr;
  • new[]: Allocates array.
    • int* arr = new int[5];
  • delete[]: Frees allocated array.
    • delete[] arr;
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
    int* arr = (int*)malloc(5 * sizeof(int));
    free(arr);

    int* nums = new int[5];
    delete[] nums;
    return 0;
}

Random Numbers (<cstdlib> / <ctime>)

  • rand(): Returns random integer.
    • int rand()
  • srand(): Seeds random generator.
    • void srand(unsigned int seed)
  • time(): Returns current time.
    • time_t time(time_t* arg)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));
    for (int i = 0; i < 5; i++) {
        cout << rand() % 100 << " "; // Random 0-99
    }
    return 0;
}

Character Classification (<cctype>)

Common Functions

  • isalpha(): Checks if character is a letter.
    • int isalpha(int c)
  • isdigit(): Checks if character is a digit.
    • int isdigit(int c)
  • isalnum(): Checks if alphanumeric.
    • int isalnum(int c)
  • isspace(): Checks if whitespace.
    • int isspace(int c)
  • islower(): Checks if lowercase.
    • int islower(int c)
  • isupper(): Checks if uppercase.
    • int isupper(int c)
  • tolower(): Converts to lowercase.
    • int tolower(int c)
  • toupper(): Converts to uppercase.
    • int toupper(int c)
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char c = 'A';
    if (isupper(c)) cout << "Uppercase" << endl;
    cout << (char)tolower(c) << endl; // 'a'
    return 0;
}