728x90
반응형

1️⃣ 입출력 기본
(1) C 방식 입출력
#include <stdio.h> // 또는 <cstdio>
int temp;
scanf("%d", &temp);
printf("Hello %d\n", temp);
설명
- scanf : 표준 입력을 받아 변수에 저장
- printf : 형식 지정자로 출력 (%d, %s 등 사용)
(2) C++ 방식 입출력 (권장)
#include <iostream>
using namespace std;
int temp;
cin >> temp;
cout << "Hello " << temp << endl;
설명
- cin : 입력 스트림
- cout : 출력 스트림
- endl : 줄바꿈 + 출력 버퍼 비우기
2️⃣ 코테 시작 기본 템플릿
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
return 0;
}
설명
- 자주 쓰는 헤더를 한 번에 모아둔 코테 기본 템플릿
- Visual Studio 경고를 피하기 위해 _CRT_SECURE_NO_WARNINGS 사용
3️⃣ pair (두 값 묶기)
#include <vector>
pair<int, char> p;
// 입력
scanf("%d %c", &p.first, &p.second);
// 생성
p = make_pair(3, 'b');
설명
- pair<T1, T2> : 서로 다른 타입 두 개를 하나로 묶음
- p.first : 첫 번째 값
- p.second : 두 번째 값
- make_pair(a,b) : pair를 쉽게 생성
4️⃣ vector (가변 배열)
기본 연산
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v1 = {1, 2, 3};
v1.push_back(4); // 맨 뒤에 추가
v1.pop_back(); // 맨 뒤 삭제
int a = v1.front(); // 첫 원소 반환
int b = v1.back(); // 마지막 원소 반환
int sz = v1.size(); // 원소 개수 반환
v1.erase(v1.begin()); // 첫 원소 삭제
v1.clear(); // 전체 삭제
reverse(v1.begin(), v1.end()); // 순서 뒤집기
}
설명 요약
함수역할
| push_back | 뒤에 추가 |
| pop_back | 뒤에서 삭제 |
| front | 첫 값 |
| back | 마지막 값 |
| size | 길이 |
| erase | 특정 위치 삭제 |
| clear | 전체 삭제 |
| reverse | 순서 뒤집기 |
vector에 pair 넣기
vector<pair<int,char>> v2;
v2.push_back(make_pair(1,'a'));
설명
- 벡터 안에 pair를 저장할 수 있음
5️⃣ 2차원 벡터 입력받기
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> v;
vector<int> temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
int x;
cin >> x;
temp.push_back(x);
}
v.push_back(temp);
temp.clear();
}
}
설명
- vector<vector<int>> : 2차원 배열처럼 사용
- temp.clear()로 다음 행을 위해 비움
6️⃣ 정적 크기 + 0 초기화
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n); // 길이 n, 모두 0
vector<vector<int>> v2(n, vector<int>(n, 0)); // n×n 2차원 0 초기화
}
설명
- vector<int>(n) : n개 생성, 자동으로 0
- vector<int>(n,0) : n개를 모두 0으로 초기화
7️⃣ Queue (큐)
#include <queue>
using namespace std;
int main() {
queue<int> q1;
queue<pair<int,char>> q2;
q1.push(4); // 뒤에 추가
q1.pop(); // 앞에서 제거
q2.push(make_pair(1,'a'));
}
설명
함수역할
| push | 뒤에 삽입 |
| pop | 앞에서 제거 |
| front | 맨 앞 조회 |
| back | 맨 뒤 조회 |
| empty | 비었는지 확인 |
| size | 개수 확인 |
pair가 들어간 queue 접근
queue<pair<int,int>> q;
q.push({0,0});
int x = q.front().first;
int y = q.front().second;
q.pop();
설명
- q.front()로 pair를 꺼내고 .first, .second로 접근
8️⃣ Stack (스택)
#include <stack>
using namespace std;
int main() {
stack<int> s1;
stack<pair<int,char>> s2;
s1.push(4); // 위에 추가
s1.pop(); // 위에서 제거
s2.push({1,'a'});
}
설명
함수역할
| push | top에 추가 |
| pop | top 제거 |
| top | top 조회 |
| empty | 비었는지 |
| size | 크기 |
9️⃣ Set (중복 없는 집합)
#include <set>
#include <cstdio>
using namespace std;
int main() {
set<int> s;
s.insert(1);
s.insert(2);
s.insert(6);
for (auto it = s.begin(); it != s.end(); it++) {
printf("%d ", *it);
}
printf("\n");
auto it = s.find(6);
if (it != s.end()) printf("%d\n", *it);
}
설명
함수역할
| insert | 삽입 |
| find | 원소 찾기 |
| begin/end | 순회용 반복자 |
| size | 개수 |
| empty | 비었는지 |
특징: 자동 정렬 + 중복 불가
🔟 Map (key → value)
#include <map>
#include <cstdio>
using namespace std;
int main() {
map<char,int> m;
m.insert({'a',1});
m['e'] = 6;
m['f'] = 7;
for (auto it = m.begin(); it != m.end(); it++) {
printf("<%c %d> ", it->first, it->second);
}
printf("\n");
m.erase('a');
}
설명
함수역할
| insert | 삽입 |
| m[key] | 값 접근/수정 |
| erase | 삭제 |
| find | 검색 |
| begin/end | 순회 |
특징: key 기준 자동 정렬
1️⃣1️⃣ Sort (정렬)
배열 정렬
#include <algorithm>
using namespace std;
int arr[10] = {9,3,5,4,1,10,8,6,7,2};
sort(arr, arr+10);
설명
- sort(first, last) : 오름차순 정렬
내림차순 정렬
bool compare(int a, int b) {
return a > b;
}
sort(arr, arr+10, compare);
설명
- 세 번째 인자에 비교 함수 넣으면 정렬 기준 변경
pair의 두 번째 값 기준 정렬
bool compare(pair<int,int> a, pair<int,int> b) {
return a.second > b.second;
}
설명
- second 기준으로 정렬
1️⃣2️⃣ 문자열 다루기
#include <string>
#include <algorithm>
using namespace std;
string reverseStr(string s) {
string t = "";
for (int i = s.size()-1; i >= 0; i--)
t += s[i];
return t;
}
int main() {
string s;
cin >> s;
transform(s.begin(), s.end(), s.begin(), tolower);
transform(s.begin(), s.end(), s.begin(), toupper);
}
설명
함수역할
| size() | 문자열 길이 |
| transform | 대소문자 변환 |
| tolower/toupper | 문자 변환 |
1️⃣3️⃣ 방향 벡터 (DFS/BFS 필수)
int dx[4] = {-1, 0, 1, 0}; // 상, 우, 하, 좌
int dy[4] = {0, 1, 0, -1};
int dx8[8] = {-1,1,0,0,-1,1,1,-1};
int dy8[8] = {0,0,1,-1,1,1,-1,-1};
설명
- 격자 탐색에서 이동 방향 정의
1️⃣4️⃣ 2차원 벡터 출력
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
설명
- 이중 for문으로 행렬 출력
1️⃣5️⃣ stack, queue 비우기
while(!s.empty()) s.pop();
while(!q.empty()) q.pop();
설명
- 모든 원소 제거
728x90
반응형
'coding test - C++ > 기본기문제' 카테고리의 다른 글
| [C++] 기본 문법 (매크로, 구조체, 연산) (3) | 2026.02.10 |
|---|---|
| [C++] 주요 알고리즘 정리 (BFS, DFS, 다익스트라...) (1) | 2026.02.07 |