[go: up one dir, main page]

0% found this document useful (0 votes)
28 views3 pages

CPP Vector Cheatsheet

This cheat sheet provides a comprehensive overview of C++ vector functions, including basic operations, iteration methods, and ways to modify vectors. It also covers sorting, searching, and other useful functions, along with one-liners and tricks for efficient vector manipulation. Essential headers to include for these functions are also mentioned.

Uploaded by

dev0607sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

CPP Vector Cheatsheet

This cheat sheet provides a comprehensive overview of C++ vector functions, including basic operations, iteration methods, and ways to modify vectors. It also covers sorting, searching, and other useful functions, along with one-liners and tricks for efficient vector manipulation. Essential headers to include for these functions are also mentioned.

Uploaded by

dev0607sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Vector Functions Cheat Sheet

✅ Basic Operations

• v.size()
• Returns number of elements in the vector.
• v.empty()
• Returns true if vector is empty.
• v.clear()
• Removes all elements from vector.
• v.push_back(x)
• Adds element x at the end.
• v.pop_back()
• Removes the last element.
• v[i] or v.at(i)
• Accesses element at index i .

🔁 Iteration

for (int i = 0; i < v.size(); ++i) // Index-based loop


for (int x : v) // Range-based loop

🛠️ Modifying Vectors

• v.insert(v.begin() + i, x)
• Inserts x at index i .
• v.erase(v.begin() + i)
• Erases element at index i .
• v.erase(v.begin() + i, v.begin() + j)
• Erases range [i, j) .
• v.resize(n)
• Resizes vector to n elements.
• v.assign(n, x)
• Replaces vector with n copies of x .

🔄 Sorting & Reversing

• sort(v.begin(), v.end())
• Sorts in ascending order.
• sort(v.rbegin(), v.rend())

1
• Sorts in descending order.
• reverse(v.begin(), v.end())
• Reverses the vector.

🔍 Searching & Finding (requires <algorithm> )

• find(v.begin(), v.end(), x)
• Finds first occurrence of x .
• binary_search(v.begin(), v.end(), x)
• Checks if x is present (sorted).
• lower_bound(v.begin(), v.end(), x)
• Returns iterator to first element ≥ x .
• upper_bound(v.begin(), v.end(), x)
• Returns iterator to first element > x .

📆 Other Useful Functions

• v.front()
• First element.
• v.back()
• Last element.
• swap(v1, v2)
• Swaps contents of two vectors.
• v1 == v2
• Checks equality of two vectors.
• max_element(v.begin(), v.end())
• Iterator to maximum element.
• min_element(v.begin(), v.end())
• Iterator to minimum element.
• accumulate(v.begin(), v.end(), 0)
• Sum of all elements (requires <numeric> ).

✨ One-Liners & Tricks

• Remove all even elements:

v.erase(remove_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }),


v.end());

• Create vector of 10 elements all initialized to 5:

2
vector<int> v(10, 5);

• Reverse sort:

sort(v.rbegin(), v.rend());

Pro Tip: Always include the required headers:

#include <vector>
#include <algorithm>
#include <numeric>

Let me know if you want STL map/set cheatsheet next!

You might also like