Wiki › Tham khảo ngôn ngữ › C++ STL Cheatsheet

C++ STL Cheatsheet

huunguyen huunguyen Updated Tháng tư 3, 2026 Đọc 3 phút 3 lượt xem

Template chuẩn

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // code here

    return 0;
}

ios_base::sync_with_stdio(false) và cin.tie(NULL) giúp I/O nhanh hơn ~5 lần. Luôn thêm khi dùng cin/cout.


Kiểu dữ liệu

int         // ~2.1 × 10^9
long long   // ~9.2 × 10^18
double      // số thực
bool        // true / false
char        // ký tự
string      // chuỗi

// Hằng số hữu ích
INT_MAX     // 2147483647
LLONG_MAX   // 9223372036854775807
1e9+7       // 10^9+7 (số nguyên tố dùng làm MOD)

Vector

vector<int> v;
vector<int> v(n, 0);          // n phần tử, giá trị 0
vector<int> v = {1, 2, 3};

v.push_back(x);               // thêm cuối O(1)
v.pop_back();                 // xóa cuối O(1)
v.size();                     // kích thước
v.empty();                    // kiểm tra rỗng
v.front(); v.back();          // phần tử đầu/cuối
v.resize(n);                  // đổi kích thước
v.clear();                    // xóa tất cả

// Duyệt
for (int x : v) { ... }
for (int i = 0; i < v.size(); i++) { ... }

// 2D vector
vector<vector<int>> g(n, vector<int>(m, 0));

Sort & Tìm kiếm

sort(v.begin(), v.end());                    // tăng dần
sort(v.begin(), v.end(), greater<int>());    // giảm dần
sort(v.begin(), v.end(), [](int a, int b) { return a > b; });

// Binary search (mảng đã sort)
lower_bound(v.begin(), v.end(), x);  // iterator đến phần tử >= x
upper_bound(v.begin(), v.end(), x);  // iterator đến phần tử > x

// Đổi iterator ra index
int idx = lower_bound(v.begin(), v.end(), x) - v.begin();

// Min/max
*min_element(v.begin(), v.end());
*max_element(v.begin(), v.end());
min(a, b); max(a, b);

Set / Map

// set — BST, không trùng, có thứ tự
set<int> s;
s.insert(x); s.erase(x);
s.count(x);          // 1 nếu tồn tại
s.find(x);           // iterator
*s.begin();          // nhỏ nhất
*s.rbegin();         // lớn nhất
s.lower_bound(x);    // >= x

// map — BST key-value
map<string,int> mp;
mp["key"] = 10;
mp.count("key");
for (auto& [k,v] : mp) { ... }

// Phiên bản O(1): unordered
unordered_set<int> us;
unordered_map<string,int> um;

Stack / Queue / Priority Queue

// Stack (LIFO)
stack<int> st;
st.push(x); st.pop(); st.top(); st.empty();

// Queue (FIFO)
queue<int> q;
q.push(x); q.pop(); q.front(); q.empty();

// Priority Queue
priority_queue<int> pq;              // max-heap
priority_queue<int,vector<int>,greater<int>> pq;  // min-heap
pq.push(x); pq.pop(); pq.top();

// Deque
deque<int> dq;
dq.push_front(x); dq.push_back(x);
dq.pop_front(); dq.pop_back();

String

string s = "hello";
s.length(); s.size();
s[i];
s.substr(l, len);        // chuỗi con từ l, độ dài len
s.find("ell");           // vị trí hoặc string::npos
s += "world";
to_string(42);           // int → string
stoi("42"); stoll("42"); // string → int/long long

// Duyệt
for (char c : s) { ... }

Toán học

abs(x);           // giá trị tuyệt đối
sqrt(x);          // căn bậc hai (double)
pow(a, b);        // a^b (double)
__gcd(a, b);      // GCD (C++14)
#include <numeric>
gcd(a, b); lcm(a, b);  // C++17

// Bit operations
x & 1;            // kiểm tra số lẻ
x >> 1;           // chia 2
x << 1;           // nhân 2
x & (x-1);        // xóa bit thấp nhất
__builtin_popcount(x);    // đếm số bit 1
__builtin_ctz(x);         // đếm số bit 0 cuối

Thuật toán thường dùng

// Đảo mảng
reverse(v.begin(), v.end());

// Xóa trùng sau sort
v.erase(unique(v.begin(), v.end()), v.end());

// Accumulate (tổng)
long long sum = accumulate(v.begin(), v.end(), 0LL);

// Fill
fill(a, a+n, 0);

// Hoán vị
swap(a, b);

// next_permutation
do { /* xử lý hoán vị */ } while (next_permutation(v.begin(), v.end()));

I/O nhanh

// Đọc/ghi file
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

// In số thực với độ chính xác
cout << fixed << setprecision(6) << x;
Trang này có hữu ích không? Đăng nhập để trả lời

Bình luận

Không có bình luận tại thời điểm này.

gnatmake 12.2.0 a68g 3.1.2 nasm 2.16.1 as_x64 2.47 awk 1.3.4 gcc 16.2.0 dotnet 10.0.402 g++ 16.2.0 g++-themis 16.2.0 g++17 16.2.0 g++20 16.2.0 g++23 16.2.0 clang++ 22.1.8 dmd 2.113.0 dart 3.13.6 gforth 0.7.3 gfortran 15.3.0 go 1.27.3 groovyc 5.1.3 javac 26.0.2.1 node 26.12.1 julia 1.13.2 kotlinc 2.4.21 lean 4.35.0 sbcl 2.2.9 lua 5.5.1 nim 2.2.12 fpc 3.2.2 fpc-themis 3.2.2 perl 5.42.3 php 8.5.12 pike 8.0 swipl 9.0.4 pypy3 7.3.23 python3 3.15.1 racket 8.7 ruby 4.0.8 rustc 1.99.1 csc 5.3.0 ctoj-scratch 0.0.1 sed 4.9 swiftc 6.4.1 tclsh 8.6 bun 1.4.4 deno 2.9.8 v 0.5.2 zig 0.17.0