STL 예시 코드 모음

mmra-mra·2022년 10월 22일

Problem Solving

목록 보기
1/3

데이터 구조

Map

#include <map>
map<int, int> m
if(m.find()==m.end()){ m.insert(pair<int, int>(key, val)); }
else{ m[key]++; }

Deque

deque<int> dq;

dq.push_front(1);
dq.push_back(1);
dq.back();
dq.size();

deque<int>::iterator it;
for(it = dq.begin(); it != dq.end(); it++){
	cout << *it << endl;
}

List

#include <list>
list<int> l;
l.push_front(1); l.push_front(2); // [2, 1]

list<int>::iterator it = l.begin();
l.insert(it, 3); // [3, 2, 1], returns it* = 3
l.erase(it); // [3, 1], returns it* = 1

Priority Queue

struct mycmp {
    bool operator()(coord& a, coord& b) {
        int hx = board[a.x][a.y];
        int hy = board[b.x][b.y];

        if(hx > hy) { return true; }
        else { return false; }
    }
};

priority_queue<coord, vector<coord>, mycmp> pq;

수학

지수함수/로그함수

#include <cmath>
double log_k(double x, int k) {
	return log(x)/log(k); // returns logx_k
}

double log_10(double x){ return log10(x); }

double e(double x){ return exp(x); }

Pi

#include <math.h>
double pi = acos(-1) // 3.14159 ...

기타

cout 자리수

#include <iostream>
#include <iomanip>
using namespace std;

cout << setprecision(8) << x << endl; // 1.0을 1로 출력

// or

cout << fixed
cout.precision(8)
cout << x << endl; // 1.0을 1.00000000로 출력

순열

#include <algorithm>

vector<int> v = {1, 2, 3};
do{
	for(int i = 0; i < v.size(); i++){ cout << v[i] << endl; }
} while(next_permutation(v.begin(), v.end());

sort()

#inlude <algorithm>

string strings[MAXN];

bool mycmp(string& a, string& b) {
	int alen = mystrlen(a.word), blen = mystrlen(b.word);

	if (alen < blen) { return true; }
	else if (alen > blen) { return false; }
	return mystrcmp(a.word, b.word) < 0;
}

sort(strings, strings + N, mycmp);

bound()

#include <algorithm>
A[5] = [1, 2, 3, 4, 5]
upper_bound(A, A + 5, 3) // &A[3], 이상인 값
lower_bound(A, A + 5, 3) // &A[3], 초과의 값

0개의 댓글