Professional Documents
Culture Documents
STL
STL
- vector<int> v
- vector<int> v(n)
- vector<int> v(n, 0)
- vector<int> v = {10, 20, 30};
- v.push_back()
- v.pop_back()
- v.size()
- v[i] (or) v.at(i)
- v.insert(pos, val) // inserts val at that pos
- v.insert(pos, {1, 2, 3..}) // insert multiple val at that pos
- v.erase()
- v.clear()
- for(auto it = v.begin(); it!=end(); it++) cout << *it;
- for(auto elem : v) cout << elem
- vector<vector<int>> v(n, vector<int> m, 0) // 2d vector
- vector<vector<int>> v(n) // fixed row, variable column size
- v[i][j]
- fill(v.begin(), v.end(), num)
- v2 = v1 (assign v1 to v2)
- vector<int> v2(v1) // another way of copying vector
- reverse(v.begin(), v.end())
- vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end()) // erase by value
### array
- int a[100];
- memset(a, 0, sizeof(a)) // fill all values with 0 or -1 only.
- fill(arr, arr + n, num)
- priority_queue<int> pq // max-heap
- pq.push()
- pq.pop()
- pq.top()
- pq.size()
- pq.empty()
- priority_queue<int, vector<int>, greater<int>> pq // min-heap
- pair<int, char> p
- pair p; //default
- pair p(1, 'a'); //initialized, different data type
- pair p(1, 10); //initialized, same data type
- pair p(g3); //copy of g3
- pair <int, int> p = make_pair(1, 10)
- pair <int, int> p = {1, 10} // prefer
- p.first, p.second;
- pair<int, pair<int, char> > p1 = { 3, { 4, 'a' } };
## lambda expression
sort(v.begin(), v.end(), [](const int& a, const int& b) -> bool
{
return a > b;
});