Data Structure

[Data Structure] Set ์‚ฌ์šฉ๋ฒ•

An effort will never betray ๐Ÿ˜Ž 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 ํ™œ์šฉ

์‹ค์Šต

๊ฒฐ๊ณผ

  • ์ถ”๊ฐ€๋กœ ๊ถ๊ธˆํ•œ ์ ์ด๋‚˜ ์ˆ˜์ •ํ•  ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.
๋ฐ˜์‘ํ˜•