-
[Data Structure] Set ์ฌ์ฉ๋ฒData Structure 2020. 3. 19. 23:10๋ฐ์ํ
- C++ STL์ค ํ๋์ธ Set์ ๋ํ ์ค๋ช ์ ๋๋ค.
Set
์ค๋ณต์ด ์๋ ์งํฉ์ ๋ํ๋ด๋ ์๋ฃ๊ตฌ์กฐ์ ๋๋ค.
๋ ธ๋ ๊ธฐ๋ฐ ๊ท ํ ์ปจํ ์ด๋๋ก ๊ท ํ ์ด์งํธ๋ฆฌ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
Set์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ #include< set > ์ ์ ์ธํด์ผ ํฉ๋๋ค.
Set < Type, Compare > ํํ๋ก Compare์๋ ๋น๊ต ํด๋์ค๊ฐ ๋ค์ด๊ฐ๋๋ค.
๋น๊ต ํด๋์ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ๋๋ค.
ํจ์
insert(element)
- set์ element์ ์ถ๊ฐํ๋ ํจ์
- if element is contained, then not insert
erase(element)
- set์์ key ์ ๊ฑฐํ๋ ํจ์
- if element is contained, then element erase
clear()
- set์ ์ด๊ธฐํํ๋ ํจ์
empty()
- set์ด ๋น์ด์๋์ง ํ์ธํ๋ ํจ์
- if set is empty, then return 1 else 0
size()
- set์ ํฌ๊ธฐ๋ฅผ ๋ฐํํ๋ ํจ์
- return size_type (unsigned int)
find(element)
- set์ element๋ฅผ ์ฐพ๋ ํจ์
- if element is contained, then return iterator else set.end()
count(element)
- set์ element์ ๊ฐฏ์๋ฅผ ์ฐพ๋ ํจ์
- if element is contained, then return 1 else 0
ํ์ ๋ฐฉ๋ฒ
- index๋ก ์ ๊ทผํ ์ ์๊ณ iterator๋ฅผ ์ฌ์ฉํด์ ์์ฐจ์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค
- ์์ : begin(), ๋ : end()
- ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ฌ์ฉ ์ auto ํ์ฉ
์ค์ต
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <iostream> #include <set> using namespace std; int main(){ set<int> s; if(s.empty()){ cout<<"set์ด ๋น์ด์์ต๋๋ค"<<endl; } s.insert(5); s.insert(3); s.insert(4); s.insert(1); s.insert(2); for(int elem : s){ cout<<elem<<' '; } cout<<endl; // count ํ์ฉ ๊ฐ๋ฅ if (s.find(3)!=s.end()) { s.erase(3); } for(int elem : s){ cout<<elem<<' '; } cout<<endl; return 0; } ๊ฒฐ๊ณผ
- ์ถ๊ฐ๋ก ๊ถ๊ธํ ์ ์ด๋ ์์ ํ ์ฌํญ์ด ์์ผ๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์.
๋ฐ์ํ'Data Structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Data Structure] Java ๋งํฌ๋ ๋ฆฌ์คํธ(LinkedList) ๊ตฌํ (0) 2021.01.15 [Data Structure] multimap ์ฌ์ฉ๋ฒ (0) 2020.11.08 [Data Structure] unordered_map ์ฌ์ฉ๋ฒ (6) 2020.03.16 [Data Structure] Priority_queue(์ฐ์ ์์ ํ) ์ฌ์ฉ๋ฒ (0) 2020.03.16 [Data Structure] pair(ํ์ด) ์ฌ์ฉ๋ฒ (0) 2020.03.13