KEMBAR78
CPP STL Data Structures | PDF | C++ | Computer Programming
0% found this document useful (0 votes)
3 views14 pages

CPP STL Data Structures

This document provides a quick reference for various C++ Standard Template Library (STL) containers, including their functions, descriptions, examples, and time complexities. It covers containers such as std::array, std::vector, std::deque, std::list, std::stack, std::queue, std::set, std::map, and their unordered counterparts. Each container's operations are summarized with their respective time complexities for efficient programming.

Uploaded by

khairegopal03
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)
3 views14 pages

CPP STL Data Structures

This document provides a quick reference for various C++ Standard Template Library (STL) containers, including their functions, descriptions, examples, and time complexities. It covers containers such as std::array, std::vector, std::deque, std::list, std::stack, std::queue, std::set, std::map, and their unordered counterparts. Each container's operations are summarized with their respective time complexities for efficient programming.

Uploaded by

khairegopal03
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/ 14

C++ STL Containers - Quick Reference

std::array
Function Description Example Time Complexity
size() Returns size of array arr.size() O(1)
at(i) Access with bounds check arr.at(2) O(1)
[] Access without bounds check arr[2] O(1)
front() First element arr.front() O(1)
back() Last element arr.back() O(1)
fill(val) Fill all with value arr.fill(0) O(n)
swap(arr2) Swap contents arr.swap(arr2) O(n)
std::vector
Function Description Example Time Complexity
push_back(val) Add to end v.push_back(5) O(1) amortized
pop_back() Remove last v.pop_back() O(1)
insert(pos,val) Insert at pos v.insert(v.begin()+2,10) O(n)
erase(pos) Erase element v.erase(v.begin()+2) O(n)
size() Get size v.size() O(1)
clear() Remove all v.clear() O(n)
[] / at() Access element v[2] / v.at(2) O(1)
std::deque
Function Description Example Time Complexity
push_back(val) Add to end dq.push_back(5) O(1)
push_front(val) Add to front dq.push_front(5) O(1)
pop_back() Remove last dq.pop_back() O(1)
pop_front() Remove first dq.pop_front() O(1)
insert(pos,val) Insert at pos dq.insert(dq.begin()+1,10) O(n)
erase(pos) Erase element dq.erase(dq.begin()) O(n)
[] / at() Access element dq[2] O(1)
std::list
Function Description Example Time Complexity
push_back(val) Add to end lst.push_back(5) O(1)
push_front(val) Add to front lst.push_front(5) O(1)
pop_back() Remove last lst.pop_back() O(1)
pop_front() Remove first lst.pop_front() O(1)
insert(it,val) Insert at pos lst.insert(it,10) O(1)
erase(it) Erase element lst.erase(it) O(1)
remove(val) Remove all with value lst.remove(5) O(n)
std::forward_list
Function Description Example Time Complexity
push_front(val) Add to front fl.push_front(5) O(1)
pop_front() Remove first fl.pop_front() O(1)
insert_after(it,val) Insert after pos fl.insert_after(it,10) O(1)
erase_after(it) Erase after pos fl.erase_after(it) O(1)
remove(val) Remove all with value fl.remove(5) O(n)
std::stack
Function Description Example Time Complexity
push(val) Add element st.push(5) O(1)
pop() Remove top st.pop() O(1)
top() Access top st.top() O(1)
empty() Check empty st.empty() O(1)
size() Get size st.size() O(1)
std::queue
Function Description Example Time Complexity
push(val) Add to back q.push(5) O(1)
pop() Remove front q.pop() O(1)
front() Access front q.front() O(1)
back() Access back q.back() O(1)
empty() Check empty q.empty() O(1)
std::priority_queue
Function Description Example Time Complexity
push(val) Add element pq.push(5) O(log n)
pop() Remove top pq.pop() O(log n)
top() Access top pq.top() O(1)
empty() Check empty pq.empty() O(1)
size() Get size pq.size() O(1)
std::set
Function Description Example Time Complexity
insert(val) Insert element s.insert(5) O(log n)
erase(val) Erase element s.erase(5) O(log n)
find(val) Find element s.find(5) O(log n)
count(val) Count element s.count(5) O(log n)
lower_bound(val) First >= val s.lower_bound(5) O(log n)
upper_bound(val) First > val s.upper_bound(5) O(log n)
size() Get size s.size() O(1)
std::multiset
Function Description Example Time Complexity
insert(val) Insert element ms.insert(5) O(log n)
erase(val) Erase all with value ms.erase(5) O(log n)
find(val) Find element ms.find(5) O(log n)
count(val) Count element ms.count(5) O(log n)
std::unordered_set
Function Description Example Time Complexity
insert(val) Insert element us.insert(5) O(1) avg
erase(val) Erase element us.erase(5) O(1) avg
find(val) Find element us.find(5) O(1) avg
count(val) Count element us.count(5) O(1) avg
std::map
Function Description Example Time Complexity
insert({k,v}) Insert key-value m.insert({1,10}) O(log n)
erase(key) Erase by key m.erase(1) O(log n)
find(key) Find by key m.find(1) O(log n)
[] Access/insert value m[1] = 5 O(log n)
std::multimap
Function Description Example Time Complexity
insert({k,v}) Insert key-value mm.insert({1,10}) O(log n)
erase(key) Erase all with key mm.erase(1) O(log n)
find(key) Find by key mm.find(1) O(log n)
std::unordered_map
Function Description Example Time Complexity
insert({k,v}) Insert key-value um.insert({1,10}) O(1) avg
erase(key) Erase by key um.erase(1) O(1) avg
find(key) Find by key um.find(1) O(1) avg
[] Access/insert value um[1] = 5 O(1) avg

You might also like