- begin(): Returns iterator to the first element.
- end(): Returns iterator to the past-the-end element.
- push_back(): Appends an element to the end.
void push_back(const T& value)
- pop_back(): Removes the last element.
- 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.
- size(): Returns number of elements.
- empty(): Checks if vector is empty.
- at(): Accesses element with bounds checking.
#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 << " ";
}
- length(): Returns string length.
- size(): Same as
length(). - 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;
}
- 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.
- size(): Returns number of elements.
- 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.
- 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)
- push(): Inserts an element at the top.
void push(const T& value)
- pop(): Removes the top element.
- 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.
- size(): Returns the number of elements.
#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;
}
- push(): Inserts an element at the back.
void push(const T& value)
- pop(): Removes the front element.
- 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.
- size(): Returns the number of elements.
#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;
}
- sqrt(): Returns square root.
- 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.
- floor(): Rounds down.
- round(): Rounds to nearest integer.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(25) << endl;
cout << pow(2, 3) << endl;
cout << floor(2.9) << endl;
return 0;
}
- 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.
- new: Allocates memory.
- delete: Frees allocated memory.
- new[]: Allocates array.
- delete[]: Frees allocated array.
#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.
- srand(): Seeds random generator.
void srand(unsigned int seed)
- time(): Returns current time.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
for (int i = 0; i < 5; i++) {
cout << rand() % 100 << " ";
}
return 0;
}
- isalpha(): Checks if character is a letter.
- isdigit(): Checks if character is a digit.
- isalnum(): Checks if alphanumeric.
- isspace(): Checks if whitespace.
- islower(): Checks if lowercase.
- isupper(): Checks if uppercase.
- tolower(): Converts to lowercase.
- toupper(): Converts to uppercase.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char c = 'A';
if (isupper(c)) cout << "Uppercase" << endl;
cout << (char)tolower(c) << endl;
return 0;
}