Array
| 구분 | Swift Array | C++ vector |
| 초기 선언 | var a: [Int] = [] | vector<int> v; |
| 초기 선언 | var a = [1,2,3] | vector<int> v{1,2,3}; |
| 초기 선언 | Array(repeating:0, count:5) | vector<int> v(5,0); |
| 개수 | a.count | v.size() |
| 상태 | a.isEmpty | v.empty() |
| 접근 | a[i] | v[i] |
| 추가 | a.append(x) | v.push_back(x) |
| 삽입 | a.insert(x, at:i) | v.insert(v.begin()+i, x) |
| 수정 | a[i] = x | v[i] = x |
| 제거 | a.remove(at:i) | v.erase(v.begin()+i) |
| 제거 | a.removeLast() | v.pop_back() |
| 초기화 | a.removeAll() | v.clear() |
Dictionary
| 구분 | Swift Dictionary | C++ map |
| 초기 선언 | var d: [String:Int] = [:] | map<string,int> m; |
| 초기 선언 | var d = ['a':1] | map<string,int> m{{'a',1}}; |
| 개수 | d.count | m.size() |
| 상태 | d.isEmpty | m.empty() |
| 접근 | d['a'] | m['a'] |
| 추가 | d['b']=2 | m['b']=2 |
| 확인 | d['a'] != nil | m.find('a')!=m.end() |
| 제거 | d['a']=nil | m.erase('a') |
Set
| 구분 | Swift Set | C++ set |
| 초기 선언 | var s: Set<Int> = [] | set<int> s; |
| 초기 선언 | var s: Set = [1,2] | set<int> s{1,2}; |
| 개수 | s.count | s.size() |
| 상태 | s.isEmpty | s.empty() |
| 추가 | s.insert(x) | s.insert(x) |
| 확인 | s.contains(x) | s.count(x) |
| 제거 | s.remove(x) | s.erase(x) |
| 초기화 | s.removeAll() | s.clear() |
'Programming Languages > Swift' 카테고리의 다른 글
| Concurrency (0) | 2025.12.26 |
|---|---|
| Protocols / Extensions/ Error Handling (0) | 2025.12.26 |
| Class / Struct (0) | 2025.12.26 |
| Functions (함수) / Closures (0) | 2025.12.26 |
| Swift vs C/C++ 문법 비교 (0) | 2025.12.19 |