#include <bits/stdc++.h>
using namespace std;
int a[3] = {1, 2, 3};
int a2[] = {1, 2, 3, 4};
int a3[10];
int main(){
for(int i = 0; i < 3; i++) cout << a[i] << " ";
cout <<'\n';
for(int i : a) cout << i << " ";
for(int i = 0; i < 4; i++) cout << a2[i] << " ";
cout <<'\n';
for(int i : a2) cout << i << " ";
for(int i = 0; i < 10; i++) a3[i] = i;
cout <<'\n';
for(int i : a3) cout << i << " ";
return 0;
}
/*
1 2 3 (a)
1 2 3 1 2 3 4 (a + a2)
1 2 3 4 (a2)
0 1 2 3 4 5 6 7 8 9 (a3)
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main(){
for(int i = 1; i <= 10; i++)v.push_back(i);
for(int a : v) cout << a << " ";
cout << "\n";
for(int i = 0; i < v.size(); i++){
cout << &v[i] << '\n';
}
v.pop_back();
for(int a : v) cout << a << " ";
cout << "\n";
v.erase(v.begin(), v.begin() + 3);
for(int a : v) cout << a << " ";
cout << "\n";
auto a = find(v.begin(), v.end(), 100);
if(a == v.end()) cout << "not found" << "\n";
fill(v.begin(), v.end(), 10);
for(int a : v) cout << a << " ";
cout << "\n";
v.clear();
cout << "아무것도 없을까?\n";
for(int a : v) cout << a << " ";
cout << "\n";
return 0;
}
/*
/tmp/k7wTAJUMUe.o
1 2 3 4 5 6 7 8 9 10
0x55d296744f20
0x55d296744f24
0x55d296744f28
0x55d296744f2c
0x55d296744f30
0x55d296744f34
0x55d296744f38
0x55d296744f3c
0x55d296744f40
0x55d296744f44
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9
not found
10 10 10 10 10 10
아무것도 없을까?
*/
iterator erace (const_iterator position);
iterator erace (const_iterator first, const_iterator last);
📖 참고 📖 범위 기반 for 루프
- for (range_declaration : range_expression)
loop_statement- 🗒️ 예시 : for(int a : v)
- vector 내에 있는 요소들을 탐색
- vector 뿐만 아니라 Array, 연결리스트, 맵, 셋을 탐색할 때도 사용
- for(int i = 0; i < v.size(); i++) int a = v[i]; 와 동일
#include <bits/stdc++.h>
using namespace std;
vector<int> v(5, 100);
int main(){
for(int a : v) cout << a << " ";
cout << "\n";
return 0;
}
/*
100 100 100 100 100
*/
📖 참고 📖 노드
- data, next로 이루어진 구조체
- 어떠한 값을 담는 Data, 노드와 노들르 잇는 Next 포인터로 이루어짐
class Node { public: int data; Node* next; Node(){ data = 0; next = NULL; } Node(int data){ this->data = data; this->next = NULL; } };
#include <bits/stdc++.h>
using namespace std;
list<int> a;
void print(list <int> a){
for(auto it : a) cout << it << " ";
cout << '\n';
}
int main(){
for(int i = 1; i <= 3; i++)a.push_back(i);
for(int i = 1; i <= 3; i++)a.push_front(i);
auto it = a.begin(); it++;
a.insert(it, 1000);
print(a);
it = a.begin(); it++;
a.erase(it);
print(a);
a.pop_front();
a.pop_back();
print(a);
cout << a.front() << " : " << a.back() << '\n';
a.clear();
return 0;
}
/*
3 1000 2 1 1 2 3
3 2 1 1 2 3
2 1 1 2
2 : 2
*/
iterator insert(const_iterator positon, const value_type&val);
iterator erase(const_iterator positon);
#include<bits/stdc++.h>
using namespace std;
stack<string> stk;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
stk.push("엄");
stk.push("준");
stk.push("식");
stk.push("화");
stk.push("이");
stk.push("팅");
while(stk.size()){
cout << stk.top() << "\n";
stk.pop();
}
}
/*
팅
이
화
식
준
엄
*/
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int main(){
for(int i = 1; i <= 10; i++)q.push(i);
while(q.size()){
cout << q.front() << ' ';
q.pop();
}
return 0;
}
/*
1 2 3 4 5 6 7 8 9 10
*/
그래프
정점 (vertex)
간선 (edge)
그래프는 트리보다 더 포괄적인 개념입니다. 즉, 트리는 그래프라고 부를 수 있으나 모든 그래프를 트리라고 부를 순 없습니다.
그래프는 각 요소를 나타내는 노드와 노드 사이의 관계를 나타내는 엣지로 나타낼 수 있습니다.
트리는 그래프와 동일하나, 사이클이 없는 그래프를 의미하며 계층 구조를 나타낼 때 용이합니다.
루트를 방문하는 작업을 V
왼쪽 서브 트리 방문을 L
오른쪽 서브트리 방문을 R
루트를 서브 트리에 앞서서 먼저 방문하면 전위 순회
루트를 왼쪽과 오른쪽 서브 트리 중간에 방문하면 중위 순회
루트를 서브 트리 방문 후에 방문하면 후위 순회
개념
조건
시간 복잡도
성능 개선하는 방법
#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
string a[] = {"주홍철", "양영주", "박종선"};
int main(){
for(int i = 0; i < 3; i++){
mp.insert({a[i], i + 1});
}
cout << mp["주홍철"] << '\n';
cout << mp.size() << '\n';
mp.erase("주홍철");
auto it = mp.find("kundol");
if(it == mp.end()){
cout << "맵에서 해당요소는 없습니다.\n";
}
// 이렇게도 삽입할 수 있습니다.
mp["kundol"] = 100;
for(auto it : mp){
cout << (it).first << " : " << (it).second << '\n';
}
mp.clear();
cout << "map의 사이즈는 : " << mp.size() << "입니다.\n";
return 0;
}
/*
1
3
맵에서 해당요소는 없습니다.
kundol : 100
박종선 : 3
양영주 : 2
map의 사이즈는 : 0입니다.
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
set<int> st2;
st2.insert(2);
st2.insert(1);
st2.insert(2);
for(auto it : st2){
cout << it << '\n';
}
return 0;
}
/*
1
2
*/
let a = [1, 2, 42, 4, 12, 14, 17, 13, 37]
const hashf = num => num % 20
a = a.map(e => hashf(e))
console.log(a)
/*
[
1, 2, 2, 4, 12,
14, 17, 13, 17
]
*/
let a = ["dopa", "paka", "ralo"]
const hashf = (str, size) => {
let hash = 3;
for(let i = 0; i < str.length; i++){
hash *= str.charCodeAt(i)
hash %= size
}
return hash
}
a = a.map(e => hashf(e, 20))
console.log(a)
console.log('a'.charCodeAt(0))
// [ 0, 8, 12 ]
#include <bits/stdc++.h>
using namespace std;
int hashF(string str){
int ret = str.size();
for(char a : str){
ret += (int)a;
}
return ret % 10;
}
int main(){
// 만약 내부적으로 구현이 되어있지 않다면 이런식으로 정수타입의 해시키를 만들어서
// 해시테이블을 구현해야 합니다.
unordered_map<int, string> umap;
umap[hashF("paka")] = "paka";
umap[hashF("kundol")] = "kundol";
//unordered_map은 해시테이블로 구현이 되어있기 때문에 내부적으로 string 값을
받아도 int타입의 해시키를 만들고
//이를 기반으로 테이블로 만들어버립니다.
unordered_map<string, string> umap2;
umap2["paka"] = "paka";
umap2["kundol"] = "kundol";
for(auto element : umap){
cout << element.first << " :: " << element.second << '\n';
}
for(auto element : umap2){
cout << element.first << " :: " << element.second << '\n';
}
cout << umap[7] << "\n";
cout << umap2["kundol"] << "\n";
return 0;
}
/*
9 :: kundol
7 :: paka
kundol :: kundol
paka :: paka
paka
kundol
*/
루트 노드만을 제거할 수 있음
1️⃣ : 루트 노드를 제거함
2️⃣ : 루트 자리에 가장 마지막 노드 삽입
3️⃣ : 올라간 노드와 그의 자식노드와 비교
4️⃣ : 규칙을 만족하면 그대로 두고, 그렇지 않으면 자식노드와 교환함
📖 참고 📖
- ✏️