.1 set µ¥ÀÌÅÍ Ãß»ó(data abstraction) [Image] Sets, Ordered and Not setÀº °ªµéÀÇ ÄÝ·º¼ÇÀÌ´Ù. set µ¥ÀÌÅÍ ±¸Á¶¸¦ ±¸ÇöÇϴµ¥ »ç¿ëµÇ´Â ÄÁÅ×À̳ʴ ¼ø¼­¸¦ À¯ÁöÇÏ¿© °ªµéÀ» °ü¸®Çϱ⠶§¹®¿¡, ¿ø¼ÒÀÇ »ðÀÔ°ú »èÁ¦»Ó¸¸ ¾Æ´Ï¶ó, ƯÁ¤ °ªÀÌ ÄÝ·º¼Ç¿¡ µé¾îÀÖ´ÂÁöÀÇ ¿©ºÎ¸¦ °Ë»çÇϴµ¥ ÀÖ¾î ÃÖÀûÈ­µÇ¾î ÀÖ´Ù. ÀÌ·¯ÇÑ ¿¬»êµéÀº °¢°¢ logarithmic Ƚ¼ö·Î ¼öÇàµÈ´Ù. ¹Ý¸é¿¡ list, vector, deque¿¡¼­´Â °¢°¢ÀÇ ¿¬»êµéÀÌ ÃÖ¾ÇÀÇ °æ¿ì¿¡ ÄÁÅ×À̳ʿ¡ ´ã±ä ¸ðµç ¿ø¼ÒµéÀ» »ìÇÇ°Ô µÉ¼öµµ ÀÖ´Ù. ÀÌ·¯ÇÑ ÀÌÀ¯¶§¹®¿¡, »ðÀÔ, »èÁ¦¿Í °ªÀÇ Æ÷ÇÔ¿©ºÎ¿¡ ´ëÇÑ °Ë»ç°¡ Áß¿äÇÑ ¹®Á¦¿¡¼­´Â setÀ» ¼±ÅÃÇØ¾ß ÇÑ´Ù. list¿¡¼­Ã³·³, setµµ »çÀÌÁî¿¡ Á¦ÇÑÀ» ¹ÞÁö ¾Ê°í, ÄÝ·º¼ÇÀ¸·ÎºÎÅÍ ¿ø¼ÒµéÀ» ÷°¡Çϰųª »èÁ¦ÇÒ ¶§ ´Ã¾î³ª°Å³ª ÁÙ¾îµç´Ù. Ç¥ÁØ ¶óÀ̺귯¸®´Â µÎ°¡Áö Á¾·ùÀÇ setÀ» Á¦°øÇϴµ¥, set ÄÁÅ×À̳ʿ¡¼­´Â ¸ðµç °ªµéÀÌ À¯ÀÏÇØ¾ß ÇÏ°í, set¿¡ ÀÌ¹Ì µé¾îÀÖ´Â °ªÀ» »ðÀÔÇÏ´Â °ÍÀº ¹«½Ã°¡ µÈ´Ù. ÇÑÆí, multiset ÄÁÅ×À̳ʿ¡¼­´Â °°Àº °ªÀÌ ¿©·¯°³ À־ À̸¦ Çã¿ëÇÑ´Ù. 8.1.1 Include Files setÀ̳ª multisetÀ» »ç¿ëÇÒ ¶§´Â set Çì´õÈ­ÀÏÀ» ¹Ýµå½Ã Æ÷ÇÔ½ÃÄÑ¾ß ÇÑ´Ù. #include [Image] [Image] [Image][Image] [Image] 8.2 set°ú multiset ¿¬»ê [Image] Set°ú Bag set°ú multisetÀÌ Á¦°øÇÏ´Â ¸â¹ö ÇÔ¼öµéÀ» ÀÚ¼¼È÷ ¾Ë¾Æº»´Ù. ¸â¹ö ÇÔ¼öµéÀÌ ±âÃÊÀûÀÎ ¿¬»êµéÀ» Á¦°øÇÏÁö¸¸, 13Àå°ú 14Àå¿¡¼­ ¼³¸íÇÏ´Â generic ¾Ë°í¸®µëÀ» »ç¿ëÇÔÀ¸·Î½á ÀÌµé µ¥ÀÌÅÍ ±¸Á¶¸¦ Á»´õ À¯¿ëÇÏ°Ô »ç¿ëÇÒ ¼ö ÀÖ´Ù. 8.2.1 setÀÇ ¼±¾ð°ú ÃʱâÈ­ set´Â ÅÛÇø´ ŸÀÔÀ¸·Î ù¹ø° ÀÎÀÚ·Î ¿ø¼Ò ŸÀÔÀ», µÎ¹ø° ÀÎÀÚ·Î Å°°ªÀ» ºñ±³ÇÏ´Â ¿¬»êÀÚ¸¦ ¸í½ÃÇØÁÖ¾î¾ß ÇÑ´Ù. µÎ¹ø° ÀÎÀÚ´Â »ý·«°¡´ÉÇѵ¥, µû·Î ¸í½ÃÇÏÁö ¾ÊÀ¸¸é, ŰŸÀÔ¿¡¼­ Á¤ÀÇÇÏ°í ÀÖ´Â less-than ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù. ¿ø¼ÒÀÇ Å¸ÀÔÀº int ¶Ç´Â double°ú °°Àº primitive ŸÀÔÀ̳ª, Æ÷ÀÎÅÍ Å¸ÀÔ ¶Ç´Â »ç¿ëÀÚ Á¤ÀÇ Å¸ÀÔÀÌ µÉ ¼ö ÀÖ´Ù. ¿ø¼Ò ŸÀÔÀº »óµî ¿¬»êÀÚ(==)¿Í less-than ¿¬»êÀÚ(<)¸¦ °¡Áö°í ÀÖ¾î¾ß ÇÑ´Ù. [Image] Initializing Sets with Iterators setÀº ÃʱⰪ¾øÀÌ ¼±¾ðµÉ ¼öµµ ÀÖ°í, ÇѽÖÀÇ ¹Ýº¹ÀÚ¸¦ ½á¼­ ´Ù¸¥ ÄÁÅ×À̳ʿ¡ ´ã±ä °ªµé·Î ÃʱâÈ­ÇÒ ¼öµµ ÀÖ´Ù. ÀÌ µÎ°¡Áö °æ¿ì ¸ðµÎ ºñ±³ ÇÔ¼ö¸¦ ÀÎÀÚ·Î »ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç, »ý·«°¡´ÉÇÏ´Ù. ÀÌ ºñ±³ÇÔ¼ö´Â ÅÛÇø´ ÀÎÀÚ·Î ¸í½ÃµÈ ºñ±³ ÇÔ¼ö¸¦ µ¤¾î¾´´Ù. ÀÌ·±½ÄÀ¸·Î ºñ±³ÇÔ¼ö¸¦ ÁöÁ¤ÇÔÀ¸·Î½á, °ªÀº °°Áö¸¸ ¼ø¼­¸¦ ´Ù¸£°Ô ¸Å±â´Â µÎ°³ ÀÌ»óÀÇ setÀ» ´Ù·ç´Â °æ¿ì¿¡ À¯¿ëÇÏ´Ù. set ¸â¹ö ÇÔ¼öµéÀÌ ¿©·¯°³ °³Ã¼È­µÇ´Â °ÍÀ» ¸·À» ¼ö Àֱ⠶§¹®ÀÌ´Ù. º¹»ç »ý¼ºÀÚ¸¦ »ç¿ëÇÏ¿© ´Ù¸¥ setÀÇ º¹»çº»ÀÌ µÇ´Â setÀ» ¸¸µé ¼ö ÀÖ´Ù. set set_one; set > set_two; set set_three(greater()); set > gset; set gset(less()); set set_four (aList.begin(), aList.end()); set set_five (aList.begin(), aList.end(), greater()); set set_six (set_four); // copy constructor ÇÑ setÀ» ´Ù¸¥ set¿¡ ´ëÀÔÇÒ ¼öµµ ÀÖ°í, swap() ¿¬»êÀ» ½á¼­ µÎ set°£ÀÇ °ªµéÀ» ±³È¯ÇÒ ¼öµµ ÀÖ´Ù. set_one = set_five; set_six.swap(set_two); 8.2.2 ŸÀÔ Á¤ÀÇ set°ú multiset Ŭ·¡½º´Â ¸¹Àº ŸÀÔ Á¤ÀǵéÀ» Æ÷ÇÔÇÑ´Ù. À̵éÀº ¼±¾ð¹®¿¡¼­ °¡Àå ¸¹ÀÌ »ç¿ëµÈ´Ù. ¿¹¸¦ µé¾î, Á¤¼ö set¿¡ »ç¿ëÇÒ ¹Ýº¹ÀÚ´Â ´ÙÀ½°ú °°ÀÌ ¼±¾ðµÈ´Ù. set::iterator location; iterator»Ó¸¸ ¾Æ´Ï¶ó, ´ÙÀ½ ŸÀԵ鵵 Á¤ÀǵǾî ÀÖ´Ù. value_type The type associated with the elements the set maintains. const_iterator An iterator that does not allow modification of the underlying sequence. reverse_iterator An iterator that moves in a backward direction. const_reverse_iterator A combination constant and reverse iterator. reference A reference to an underlying element. const_reference A reference to an underlying element that will not permit modification. size_type An unsigned integer type, used to refer to the size of containers. value_compare A function that can be used to compare two elements. difference_type A signed integer type, used to describe the distance between iterators. allocator_type An allocator used by the container or all storage management. 8.2.3 »ðÀÔ [Image] Pair µ¥ÀÌÅÍ Å¸ÀÔ list³ª vector¿Í ´Þ¸® set¿¡ ¿ø¼Ò¸¦ »õ·Î Ãß°¡ÇÏ·Á¸é ´ÜÇÑ°¡Áö ¹æ¹ý¹Û¿¡ ¾ø´Ù. insert() ¸â¹ö ÇÔ¼ö¸¦ ½á¼­ setÀ̳ª multiset¿¡ °ªÀ» »ðÀÔÇÒ ¼ö ÀÖ´Â °ÍÀÌ´Ù. multisetÀÇ °æ¿ì¿¡´Â ¹æ±Ý »ðÀÔÇÑ °ªÀ» °¡¸®Å°´Â ¹Ýº¹ÀÚ¸¦ ¸®ÅÏÇÑ´Ù. setÀÇ °æ¿ì¿¡´Â pair¸¦ ¸®ÅÏÇϴµ¥, ù¹ø° Çʵå´Â ¹Ýº¹ÀÚÀÌ°í, µÎ¹ø° Çʵå´Â ºÎ¿ï°ªÀ¸·Î ¿ø¼Ò°¡ »ðÀԵǾú´Â ÁöÀÇ ¿©ºÎ¸¦ Ç¥½ÃÇÑ´Ù. set¿¡¼­´Â ¿ø¼Ò¸¦ »ðÀÔÇÒ ¶§ ÀÌ¹Ì ÄÝ·º¼Ç³»¿¡ ÇØ´ç ¿ø¼Ò°¡ ÀÖÀ¸¸é, »ðÀÔÀÌ ÀÌ·ç¾îÁöÁö ¾Ê±â ¶§¹®ÀÌ´Ù. set_one.insert(18); if (set_one.insert(18).second) cout << "element was inserted" << endl; else cout << "element was not inserted " << endl; ´Ù¸¥ ÄÁÅ×À̳ʿ¡ µé¾îÀÖ´Â °ªµéÀ» »ðÀÔÇÒ ¶§´Â ÇѽÖÀÇ ¹Ýº¹ÀÚ¸¦ »ç¿ëÇÑ´Ù. set_one.insert(set_three.begin(), set_three.end()); pair µ¥ÀÌÅÍ ±¸Á¶´Â °ªµéÀÇ ÅõÇÃÀÌ´Ù. ù¹ø° °ªÀº first¶ó´Â Çʵå¸íÀ¸·Î Á¢±ÙÇÏ°í, µÎ¹ø° °ªÀº second¶ó´Â Çʵå¸íÀ¸·Î Á¢±ÙÇÑ´Ù. make_pair()¶ó´Â ÇÔ¼ö´Â pair Ŭ·¡½ºÀÇ °³Ã¼¸¦ ¸¸µå´Â ÀÛ¾÷À» °£ÆíÇÏ°Ô ÇØÁØ´Ù. template struct pair { T1 first; T2 second; pair (const T1 & x, const T2 & y) : first(x), second(y) { } }; template inline pair make_pair(const T1& x, const T2& y) { return pair(x, y); } ¿¹¸¦ µé¾î, »õ·Î¿î ¿ø¼ÒÀÇ Å°ºÎºÐÀÌ ÀÌ¹Ì Á¸ÀçÇÏ´Â Å°°ªÀÎÁö ¾Ë¾Æº¸±â À§Çؼ­, Å°°ªµéÀÌ ¼­·Î ÀÏÄ¡ÇÏ´ÂÁö¸¦ °Ë»çÇÒ ¶§, »óµî(==) ¿¬»êÀÚ¸¦ »ç¿ëÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó, ºñ±³ ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù. Å°°ªµéÀÇ ¼ø¼­¸¦ ¸Å±â±âÀ§ÇØ »ç¿ëµÇ´Â ºñ±³ ÇÔ¼ö°¡ ¾ç¹æÇâÀ¸·Î ¸ðµÎ °ÅÁþÀ̸é, µÎ Å°°ªÀº ¼­·Î °°´Ù°í º»´Ù. ´Ù½Ã ¸»Çؼ­, Compare(key1, key2)°¡ °ÅÁþÀÌ°í, Compare(key2, key1)µµ °ÅÁþÀ̸é, key1°ú key2´Â »óµîÀ̶ó°í º¸´Â °ÍÀÌ´Ù. 8.2.4 set¿¡¼­ÀÇ »èÁ¦ set¿¡¼­ °ªÀ» »èÁ¦ÇÒ ¶§´Â erase() ¸â¹ö ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù. ÀÎÀڴ ƯÁ¤ °ªÀ̰ųª, °ªÀ» °¡¸®Å°´Â ¹Ýº¹ÀÚ ¶Ç´Â °ªµéÀÇ range¸¦ ÁöÁ¤ÇÏ´Â ÇѽÖÀÇ ¹Ýº¹ÀÚÀÏ ¼ö ÀÖ´Ù. ù¹ø° ÇüŸ¦ multiset¿¡ »ç¿ëÇÒ ¶§´Â ÀÎÀÚ·Î ÁÖ¾îÁø °ª°ú ÀÏÄ¡ÇÏ´Â ¸ðµç ¿ø¼ÒµéÀÌ »èÁ¦µÇ°í, ¸®ÅÏ°ªÀº »èÁ¦µÈ ¿ø¼ÒµéÀÇ °¹¼ö¸¦ ÀǹÌÇÑ´Ù. // erase element equal to 4 set_three.erase(4); // erase element five set::iterator five = set_three.find(5); set_three.erase(five); // erase all values between seven and eleven set::iterator seven = set_three.find(7); set::iterator eleven = set_three.find(11); set_three.erase (seven, eleven); ÄÁÅ×À̳ʰ¡ °¡Áö°í ÀÖ´Â ¿ø¼ÒµéÀÇ Å¸ÀÔÀÌ ¼Ò¸êÀÚ¸¦ °¡Áö°í ÀÖ´Ù¸é, ¿ø¼Ò°¡ »èÁ¦µÇ±â Àü¿¡ ¼Ò¸êÀÚ¸¦ È£ÃâÇÏ°Ô µÈ´Ù. 8.2.5 °Ë»ö°ú Ä«¿îÆà size() ¸â¹öÇÔ¼ö´Â ÄÁÅ×À̳ʰ¡ °¡Áö°í ÀÖ´Â ¿ø¼ÒÀÇ °¹¼ö¸¦ ¹ÝȯÇÑ´Ù. empty() ¸â¹ö ÇÔ¼ö´Â ÄÁÅ×À̳ʰ¡ ºñ¾î ÀÖÀ» ¶§ ÂüÀ» ¸®ÅÏÇϴµ¥, size()¸¦ 0°ú ºñ±³ÇÏ´Â °Íº¸´Ù ´ëü·Î ´õ ºü¸£´Ù. find() ¸â¹ö ÇÔ¼ö´Â ƯÁ¤°ªÀ» ÀÎÀÚ·Î ÃëÇؼ­ set³»¿¡ ÇØ´ç °ªÀÌ Á¸ÀçÇϸé, ±× °ªÀÇ À§Ä¡¸¦ °¡¸®Å°´Â ¹Ýº¹ÀÚ¸¦ ¸®ÅÏÇÏ°í, set³»¿¡ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é, end-of-set¿¡ ÇØ´çÇÏ´Â ¹Ýº¹ÀÚ¸¦ ¸®ÅÏÇÑ´Ù(ÀÌ´Â end()°¡ ¸®ÅÏÇÏ´Â °ª°ú °°Àº °ÍÀÌ´Ù). °°Àº °ªÀÌ ¿©·¯°³ Á¸ÀçÇÒ ¼ö ÀÖ´Â multisetÀÇ °æ¿ì¿¡´Â ÀÌµé °ªÁß¿¡¼­ ÀûÀýÇÑ °ªÀ» ¸®ÅÏÇÑ´Ù. set::iterator five = set_three.find(5); if (five != set_three.end()) cout << "set contains a five" << endl; lower_bound()¿Í upper_bound() ¸â¹ö ÇÔ¼ö´Â multiset°ú °°ÀÌ ¾²ÀÏ ¶§ ¾ÆÁÖ ¾µ¸ð°¡ ÀÖ´Ù. set¿¡ »ç¿ëÇϸé, ´ÜÁö find() ÇÔ¼ö¸¦ Èä³»³»´Â °Í¿¡ Áö³ªÁö ¾Ê´Â´Ù. lower_bound() ¸â¹ö ÇÔ¼ö´Â ÀÎÀÚ·Î ÁÖ¾îÁø Å°°ª°ú ÀÏÄ¡Çϴ ù¹ø° ¿ø¼Ò¸¦ ¸®ÅÏÇÏ°í, ¹Ý¸é¿¡ upper_bound() ¸â¹ö ÇÔ¼ö´Â Å°°ª°ú ÀÏÄ¡ÇÏ´Â ¸¶Áö¸· ¿ø¼Ò¸¦ Áö³ª¼­ ù¹ø° ¿ø¼Ò¸¦ ¹ÝȯÇÑ´Ù. ¸¶Áö¸·À¸·Î, equal_range() ¸â¹ö ÇÔ¼ö´Â lower bound¿Í upper bound¸¦ Æ÷ÇÔÇÏ´Â ¹Ýº¹ÀÚÀÇ pair¸¦ ¸®ÅÏÇÑ´Ù. count() ¸â¹ö ÇÔ¼ö´Â ÀÎÀÚ·Î ÁÖ¾îÁö´Â °ª°ú ÀÏÄ¡ÇÏ´Â ¿ø¼ÒÀÇ °¹¼ö¸¦ ¸®ÅÏÇÑ´Ù. setÀÇ °æ¿ì¿¡´Â ÀÌ °ªÀÌ 0 ¶Ç´Â 1ÀÎ ¹Ý¸é, multisetÀÇ °æ¿ì¿¡´Â À½¼ö°¡ ¾Æ´Ñ ÀÓÀÇÀÇ °ªÀÌ µÉ ¼ö ÀÖ´Ù. 0ÀÌ ¾Æ´Ñ Á¤¼ö°ªÀº ÂüÀ¸·Î °£ÁÖÇϹǷÎ, count() ÇÔ¼ö¸¦ ¿ø¼ÒÀÇ Æ÷ÇÔ °Ë»ç¿¡ »ç¿ëÇÒ ¼öµµ ÀÖ´Ù. ¶Ç´Â find()ÀÇ ¸®ÅÏ°ªÀ» end-of-collection ¹Ýº¹ÀÚ¿Í ºñ±³ÇÏ¿© Æ÷ÇÔ °Ë»ç¸¦ ÇÒ ¼öµµ ÀÖ´Ù. if (set_three.count(5)) cout << "set contains a five" << endl; 8.2.6 ¹Ýº¹ÀÚ [Image] No Iterator Invalidation begin()°ú end() ¸â¹ö ÇÔ¼ö´Â set°ú multiset¿¡ ´ëÇÑ ¹Ýº¹ÀÚ¸¦ »ý¼ºÇÑ´Ù. À̵é ÇÔ¼öµéÀÌ ¸¸µé¾î³»´Â ¹Ýº¹ÀÚµéÀº »ó¼öÀ̾î¾ß Çϴµ¥, ÀÌ´Â setÀÇ ¿ø¼Ò¿¡ »õ·Î¿î °ªÀ» ´ëÀÔÇÏ°Ô µÇ¾î set¿¡ ´ëÇÑ ¼ø¼­ °ü°è°¡ ±úÁö´Â °ÍÀ» ¸·±â À§Çؼ­ÀÌ´Ù. ÀÌµé ¹Ýº¹ÀÚµéÀ» »ç¿ëÇÏ¿©, setÀ» ¼±¾ðÇßÀ» ¶§ ¸í½ÃÇÑ ºñ±³ ¿¬»êÀÚ·Î Á¤ÇØÁø ¼ø¼­´ë·Î ¿ø¼ÒµéÀ» Á¢±ÙÇÒ ¼ö ÀÖ´Ù. rbegin()°ú rend() ¸â¹ö ÇÔ¼ö´Â ¿ª¼øÀ¸·Î ¿ø¼ÒµéÀ» Á¢±ÙÇÏ´Â ¹Ýº¹ÀÚµéÀ» »ý¼ºÇÑ´Ù. 8.2.7 set ¿¬»ê ÀüÅëÀûÀÎ ÁýÇÕ ¿¬»êÀÎ ºÎºÐÁýÇÑ °Ë»ç, union, intersection, difference ¿¬»êµéÀº ¸â¹öÇÔ¼ö·Î Á¦°øµÇÁö ¾ÊÁö¸¸, ´ë½Å¿¡ ordered ±¸Á¶¿¡ »ç¿ëµÇ´Â generic ¾Ë°í¸®µëÀ¸·Î ±¸ÇöµÇ¾î ÀÖ´Ù. À̵é ÇÔ¼öµé¿¡ °üÇؼ­´Â 14.7Àý¿¡ ÀÚ¼¼È÷ ¼³¸íµÇ¾î ÀÖ´Ù. ´ÙÀ½Àº À̵é ÇÔ¼öµéÀÌ set°ú multiset Ŭ·¡½º¿Í ÇÔ²² »ç¿ëÇÏ´Â ¹ýÀ» °£·«È÷ ¼³¸íÇÏ°í ÀÖ´Ù. 8.2.7.1 ºÎºÐÁýÇÕ °Ë»ç includes()ÇÔ¼ö´Â ÇÑ ÁýÇÕÀÌ ´Ù¸¥ ÁýÇÕÀÇ ºÎºÐÁýÇÕÀÎÁö ¾Ë¾Æº¸´Âµ¥ »ç¿ëµÈ´Ù. multisetÀÇ °æ¿ì¿¡´Â µÎ¹ø° ÁýÇÕ³»¿¡¼­ ÀÏÄ¡ÇÏ´Â ¿ø¼ÒÀÇ °¹¼ö°¡ ù¹ø° ¿ø¼ÒÀÇ °¹¼ö¸¦ ÃÊ°úÇØ¾ß ÇÑ´Ù. ÀÎÀÚÀÇ °¹¼ö´Â Æ÷ÇԵǴ ÁýÇÕÀ» ³ªÅ¸³»´Â ¹Ýº¹ÀÚ Çѽְú Æ÷ÇÔÇÏ´Â ÁýÇÕÀ» ³ªÅ¸³»´Â ¹Ýº¹ÀÚ ÇѽÖÇؼ­, ¸ðµÎ 4°³°¡ ÇÊ¿äÇÏ´Ù. if (includes(set_one.begin(), set_one.end(), set_two.begin(), set_two.end())) cout << "set_one is a subset of set_two" << endl; ¿ø¼Ò¸¦ ºñ±³Çϴµ¥ »ç¿ëµÇ´Â °ÍÀº less-than ¿¬»êÀÚ(<)À̸ç, setÀ» ¼±¾ðÇÒ ¶§ ¸í½ÃÇÑ ºñ±³ ¿¬»êÀÚ¸¦ »ç¿ëÇÏ´Â °ÍÀÌ ¾Æ´Ï´Ù. ÀÌ°ÍÀÌ ¸¾¿¡ ¾Èµé¸é, ´Ù¸¥ ÇüÅÂÀÇ includes() ÇÔ¼ö¸¦ »ç¿ëÇÏ¸é µÈ´Ù. ÀÌ°ÍÀº µÎ ÁýÇÕÀÇ ¿ø¼ÒÀÇ ¼ø¼­¸¦ ¸Å±â±â À§Çؼ­ »ç¿ëµÇ´Â ºñ±³ ÇÔ¼ö°¡ ÀÎÀÚ·Î Ãß°¡µÇ¾î, ÃÑ 5°³ÀÇ ÀÎÀÚ¸¦ ÇÊ¿ä·Î ÇÑ´Ù. 8.2.7.2 ÇÕÁýÇÕ°ú ±³ÁýÇÕ set_union() ÇÔ¼ö´Â µÎ ÁýÇÕÀÇ ÇÕÁýÇÕÀ» ±¸¼ºÇϴµ¥ »ç¿ëµÈ´Ù. ¿¬»ê¿¡ °ü¿©µÇ´Â µÎÁýÇÕÀº ¹Ýº¹ÀÚÀÇ ½ÖÀ¸·Î Ç¥½ÃÇÏ°í, ÇÕÁýÇÕÀÇ °á°ú´Â 5¹ø° ÀÎÀÚ·Î ¸í½ÃµÈ Ãâ·Â ¹Ýº¹ÀÚ·Î º¹»çµÈ´Ù. °á°ú¸¦ setÀ¸·Î ±¸¼ºÇϱâ À§Çؼ­, »ðÀÔ ¹Ýº¹ÀÚ¸¦ »ç¿ëÇÏ¿© Ãâ·Â ¹Ýº¹ÀÚ¸¦ ¸¸µé¾î¾ß ÇÑ´Ù.(»ðÀÔ ¹Ýº¹ÀÚ¿¡ °üÇؼ­´Â 2.4ÀýÀ» Âü°í) // union two sets, copying result into a vector vector v_one (set_one.size() + set_two.size()); set_union(set_one.begin(), set_one.end(), set_two.begin(), set_two.end(), v_one.begin()); // form union in place set temp_set; set_union(set_one.begin(), set_one.end(), set_two.begin(), set_two.end(), inserter(temp_set, temp_set.begin())); set_one.swap(temp_set); // temp_set will be deleted set_intersection() ÇÔ¼öµµ ÀÌ¿Í ºñ½ÁÇϸç, µÎÁýÇÕÀÇ ±³ÁýÇÕÀ» Çü¼ºÇÑ´Ù. includes() ÇÔ¼ö¿¡¼­Ã³·³ ¿ø¼Ò¸¦ ºñ±³ÇÒ ¶§´Â ¼±¾ð½Ã ¸í½ÃÇÑ ºñ±³ ¿¬»êÀÚ´ë½Å¿¡ less-than ¿¬»êÀÚ(<)¸¦ »ç¿ëÇÑ´Ù. ÀÌ°ÍÀÌ ºÎÀûÀýÇÏ´Ù°í »ý°¢µÇ¸é, 6¹ø° ÀÎÀÚ·Î ºñ±³ ¿¬»êÀÚ¸¦ ¿ä±¸ÇÏ´Â ´Ù¸¥ ÇüÅÂÀÇ set_union()°ú set_intersection()À» »ç¿ëÇÏ¸é µÈ´Ù. multisetÀÇ ÇÕÁýÇÕ ¿¬»êÀº µÎÁýÇÕÀ» ÇÕº´ÇÏ´Â ¿¬»ê°ú´Â ±¸º°µÇ¾î¾ß ÇÑ´Ù. ÇÑ ÁýÇÕÀÌ 7À» 3°³ Æ÷ÇÔÇÏ°í ÀÖ°í, ´Ù¸¥ ÁýÇÕÀÌ 7À» 2°³ Æ÷ÇÔÇÏ°í ÀÖ´Ù°í °¡Á¤ÇÏÀÚ. À̵éÀÇ ÇÕÁýÇÕÀº 7À» 3°³¸¸ Æ÷ÇÔÇÏÁö¸¸ ÇÕº´ÇÑ °á°ú¿¡´Â 5°³°¡ Æ÷ÇԵȴÙ. ÇÕº´Àº merge() ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ´Ù(14.6Àý Âü°í). ÀÌ ÇÔ¼öÀÇ ÀÎÀÚ´Â set_union() ÇÔ¼ö¿Í µ¿ÀÏÇÏ´Ù. 8.2.7.3 Â÷ÁýÇÕ Â÷ÁýÇÕ ¿¬»ê¿¡´Â µÎ°¡Áö ÇüÅ°¡ ÀÖ´Ù. ´Ü¼ø Â÷ÁýÇÕÀº ù¹ø° ÁýÇÕÀÇ ¿ø¼ÒÁß¿¡¼­ µÎ¹ø° ÁýÇÕ¿¡ ¼ÓÇÏÁö ¾Ê´Â ¿ø¼ÒµéÀ» ³ªÅ¸³½´Ù. ´ëĪ Â÷ÁýÇÕÀº µÎ¹ø° ÁýÇÕÀÇ ¿ø¼Ò¸¦ Á¦¿ÜÇÑ Ã¹¹ø° ÁýÇÕÀÇ ¿ø¼Òµé°ú ù¹ø° ÁýÇÕÀÇ ¿ø¼Ò¸¦ Á¦¿ÜÇÑ µÎ¹ø° ÁýÇÕÀÇ ¿ø¼ÒµéÀ» ÇÕÇÑ °ÍÀÌ´Ù. À̵éÀº °¢°¢ set_difference()¿Í set_symmetric_difference() ÇÔ¼ö·Î ±¸¼ºµÈ´Ù. À̵é ÇÔ¼öµéÀº set_union()¿Í »ç¿ë¹ýÀÌ ºñ½ÁÇÏ´Ù. 8.2.8 ±âŸ generic ¾Ë°í¸®µë setÀº ¼ø¼­¸¦ ¸Å±â°í, »ó¼ö ¹Ýº¹ÀÚ¸¦ °¡Áö±â ¶§¹®¿¡, 13Àý°ú 14Àý¿¡ ¼³¸íÇÏ°í ÀÖ´Â generic ÇÔ¼öÁß¿¡´Â set¿¡ Àû¿ëÇÒ ¼ö ¾ø°Å³ª º°·Î À¯¿ëÇÏÁö ¾Ê´Â °ÍµéÀÌ ¸¹´Ù. ±×·¯³ª, ´ÙÀ½ Ç¥´Â set µ¥ÀÌÅÍ Å¸ÀÔ°ú ÇÔ²² »ç¿ëµÉ ¼ö ÀÖ´Â ¸î°¡Áö ÇÔ¼öµéÀ» º¸¿©ÁÖ°í ÀÖ´Ù. ¿ëµµ À̸§ ÇØ´çÀý Copy one sequence into another copy 13.2.2 Find an element that matches a condition find_if 13.3.1 Find a sub-sequence within a set search 13.3.3 Count number of elements that satisfy condition count_if 13.6.1 Reduce set to a single value accumulate 13.6.2 Execute function on each element for_each 13.8.1 8.3 ¿¹Á¦ ÇÁ·Î±×·¥ - öÀÚ °Ë»ç±â A simple example program that uses a set is a spelling checker. The checker takes as arguments two input streams; the first representing a stream of correctly spelled words (that is, a dictionary), and the second a text file. First, the dictionary is read into a set. This is performed using a copy() and an input stream iterator, copying the values into an inserter for the dictionary. Next, words from the text are examined one by one, to see if they are in the dictionary. If they are not, then they are added to a set of misspelled words. After the entire text has been examined, the program outputs the list of misspelled words. void spellCheck (istream & dictionary, istream & text) { typedef set > stringset; stringset words, misspellings; string word; istream_iterator dstream(dictionary), eof; // first read the dictionary copy (dstream, eof, inserter(words, words.begin())); // next read the text while (text >> word) if (! words.count(word)) misspellings.insert(word); // finally, output all misspellings cout << "Misspelled words:" << endl; copy (misspellings.begin(), misspellings.end(), ostream_iterator(cout, "\n")); } An improvement would be to suggest alternative words for each misspelling. There are various heuristics that can be used to discover alternatives. The technique we will use here is to simply exchange adjacent letters. To find these, a call on the following function is inserted into the loop that displays the misspellings. void findMisspell(stringset & words, string & word) { for (int I = 1; I < word.length(); I++) { swap(word[I-1], word[I]); if (words.count(word)) cout << "Suggestion: " << word << endl; // put word back as before swap(word[I-1], word[I]); } } 8.4 bitset Ãß»ó(abstraction) bitsetÀº set°ú vectorÀÇ crossÀÌ´Ù. vector°ú °°ÀÌ, Ãß»óÀº ÀÌÁø°ªÀÇ ÁýÇÕÀÌ´Ù. ±×·¯³ª, ³í¸®Àû bitº° ¿¬»êÀ» »ç¿ëÇÏ¿© bitset»ó¿¡ set ¿¬»êÀ» ¼öÇàÇÒ ¼ö ÀÖ´Ù. bitset Ŭ·¡½º´Â ¿ø¼Ò¸¦ Á¢±ÙÇϴµ¥ »ç¿ëÇÒ ¹Ýº¹ÀÚ¸¦ Á¦°øÇÏÁö ¾Ê´Â´Ù. 8.4.1 Include È­ÀÏ #include 8.4.2 bitsetÀÇ ¼±¾ð°ú ÃʱâÈ­ bitsetÀº ÅÛÇø´ Ŭ·¡½ºÀÌÁö¸¸, ÅÛÇø´ ÀÎÀÚ°¡ ŸÀÔÀÌ ¾Æ´Ï¶ó Á¤¼ö°ªÀÌ µÈ´Ù. ÀÌ °ªÀº setÀÌ Æ÷ÇÔÇÏ´Â ºñÆ®ÀÇ °¹¼ö¸¦ ³ªÅ¸³½´Ù. bitset<126> bset_one; // create a set of 126 bits Á» ´Ù¸¥ ±â¹ýÀ¸·Î setÀÇ »çÀÌÁ »ý¼ºÀÚÀÇ ÀÎÀÚ·Î ¸í½ÃÇÒ ¼öµµ ÀÖ´Ù. ½ÇÁ¦ »çÀÌÁî´Â ÅÛÇø´ ÀÎÀÚ·Î »ç¿ëµÈ °ª°ú »ý¼ºÀÚ ÀÎÀÚ·Î »ç¿ëµÈ °ª Áß ´õ ÀÛÀº °ªÀ¸·Î Á¤ÇØÁø´Ù. ÀÌ·¸°Ô ÇÏ´Â °ÍÀº ÇÁ·Î±×·¥¿¡¼­ µÎ°³ ÀÌ»óÀÇ ¼­·Î ´Ù¸¥ »çÀÌÁîÀÇ bit vector¸¦ Æ÷ÇÔÇÏ´Â °æ¿ì¿¡ À¯¿ëÇÏ´Ù. ÅÛÇø´ ÀÎÀÚ·Î ´õ Å«°ªÀ» »ç¿ëÇÔÀ¸·Î½á, ÇØ´ç Ŭ·¡½º¿¡ ´ëÇÑ ¸Þ½îµå¸¦ Çѹø¸¸ »ý¼ºÇÏ°Ô µÈ´Ù. ±×·¯³ª, ½ÇÁ¦ »çÀÌÁî´Â »ý¼ºÀÚ°¡ °áÁ¤ÇÏ°Ô µÈ´Ù. bitset<126> bset_two(100); // this set has only 100 elements »ý¼ºÀÚÀÇ ¼¼¹ø° ÇüÅ´ '0'°ú '1'À̶ó´Â ¹®ÀÚ·Î ÀÌ·ç¾îÁø ¹®ÀÚ¿­À» ÀÎÀÚ·Î ¹Þ´Â´Ù. ÀÌ ¹®ÀÚ¿­¿¡ ¼ÓÇÏ´Â ¹®ÀÚ¿Í °°Àº °¹¼öÀÇ ¿ø¼Ò¸¦ °¡Áö´Â bitsetÀ» ¸¸µé°í, ¹®ÀÚ¿­¿¡ ¸í½ÃµÈ °ªµé·Î ÃʱâÈ­ÇÑ´Ù. bitset<126> small_set("10101010"); // this set has 8 elements 8.4.3 Á¢±Ù°ú °Ë»ç bitsetÀÇ °¢°¢ÀÇ bit´Â ÷ÀÚ ¿¬»êÀ» »ç¿ëÇÏ¿© Á¢±ÙÀÌ °¡´ÉÇÏ´Ù. bit°¡ 1ÀÎÁö 0ÀÎÁö¸¦ ¾Ë¾Æº¸±â À§Çؼ­´Â test() ¸â¹ö ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù. bitset¿¡ ÇÑ bit°¡ 'on'ÀÎÁö¸¦ ¾Ë¾Æº¸±â À§Çؼ­´Â any() ¸â¹ö ÇÔ¼ö¸¦ »ç¿ëÇÏ°í ºÒ°ªÀ» ¸®ÅÏÇÑ´Ù. any()ÀÇ ¹Ý´ë´Â none() ¸â¹ö ÇÔ¼öÀÌ´Ù. bset_one[3] = 1; if (bset_one.test(4)) cout << "bit position 4 is set" << endl; if (bset_one.any()) cout << "some bit position is set" << endl; if (bset_one.none()) cout << "no bit position is set" << endl; set() ÇÔ¼ö´Â ƯÁ¤ bit¸¦ ¼¼ÆÃÇϴµ¥ »ç¿ëµÈ´Ù. 'bset_one.set(I)'´Â 'bset_one[I] = true'°ú µ¿ÀÏÇÏ´Ù. ¾Æ¹« ÀÎÀÚ ¾øÀÌ ÀÌ ÇÔ¼ö¸¦ È£ÃâÇÏ¸é ¸ðµç ºñÆ®¸¦ 'on'À¸·Î ¼¼ÆÃÇÑ´Ù. reset() ÇÔ¼öµµ »ç¿ë¹ýÀÌ ºñ½ÁÇѵ¥, ÀÎÀÚ·Î ¸í½ÃµÈ À§Ä¡ÀÇ ºñÆ®¸¦ 'off'·Î ¼¼ÆÃÇÏ°í, ÀÎÀÚ°¡ ¾øÀ¸¸é, ¸ðµç ºñÆ®¸¦ 'off'·Î ¼¼ÆÃÇÑ´Ù. flip() ÇÔ¼öµµ Á¦°øµÇ´Âµ¥ ºñÆ®¸¦ °¡¸®Å°´Â ·¹ÆÛ·±½º¿¡ ´ëÇÑ ¸â¹ö ÇÔ¼ö·Î¼­ Á¦°øµÈ´Ù. bset_one.flip(); // flip the entire set bset_one.flip(12); // flip only bit 12 bset_one[12].flip(); // reflip bit 12 size() ¸â¹ö ÇÔ¼ö´Â bitsetÀÇ »çÀÌÁ ¸®ÅÏÇÏ°í, count() ¸â¹ö ÇÔ¼ö´Â ¼¼ÆõǾî ÀÖ´Â ºñÆ®µéÀÇ °¹¼ö¸¦ ¸®ÅÏÇÑ´Ù. 8.4.4 set ¿¬»ê bitset¿¡ ´ëÇÑ set ¿¬»êÀº bit-wise ¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© ±¸ÇöµÇ´Âµ¥, Á¤¼ö°ª¿¡ ´ëÇØ ¼öÇàµÇ´Â ¹æ½Ä°ú ºñ½ÁÇÏ´Ù. bitset¿¡ ºÎÁ¤ ¿¬»êÀÚ(~ ¿¬»êÀÚ)¸¦ Àû¿ëÇϸé ÀÎÀÚ·Î ÁöÁ¤µÈ bitsetÀÇ ¿ø¼ÒµéÀÇ ¹Ý´ë°ªÀ» Æ÷ÇÔÇÏ´Â »õ·Î¿î bitsetÀ» ¸®ÅÏÇÑ´Ù. µÎ bitsetÀÇ ±³ÁýÇÕÀº and ¿¬»êÀÚ(& ¿¬»êÀÚ)¸¦ ÅëÇØ ÀÌ·ç¾îÁø´Ù. ´ëÀÔÇüÅÂÀÇ ¿¬»êÀÚµµ °¡´ÉÇϸç, À̶§ Áº¯ÀÌ µÎÁýÇÕÀÇ disjunctionÀÌ µÈ´Ù. bset_three = bset_two & bset_four; bset_five &= bset_three; µÎ ÁýÇÕÀÇ ÇÕÁýÇÕµµ or ¿¬»êÀÚ(| ¿¬»êÀÚ)¸¦ »ç¿ëÇÏ¿© ºñ½ÁÇÑ ¹æ½ÄÀ¸·Î ÀÌ·ç¾îÁø´Ù. exclusive-or´Â exclusive-or ¿¬»êÀÚ(^ ¿¬»êÀÚ)¸¦ »ç¿ëÇÏ¿© ÀÌ·ç¾îÁø´Ù. ¿ÞÂÊ ½¬ÇÁÆ® ¿¬»êÀÚ(<< ¿¬»êÀÚ)°ú ¿À¸¥ÂÊ ½¬ÇÁÆ® ¿¬»êÀÚ(>> ¿¬»êÀÚ)´Â bitsetÀ» ¿ÞÂÊ ¶Ç´Â ¿À¸¥ÂÊÀ¸·Î ½¬ÇÁÆ®Çϴµ¥ »ç¿ëµÈ´Ù. ÇÑ ºñÆ®¸¦ ¿ÞÂÊÀ¸·Î 'n'¸¸Å­ ½¬ÇÁÆ®Çϸé, »õ·Î¿î ºñÆ® À§Ä¡ I´Â ÀÌÀüÀÇ I-nÀÇ °ªÀÌ µÈ´Ù(?). »õ·Î¿î À§Ä¡µéÀº 0°ªµé·Î ½¬ÇÁÆ®µÈ´Ù. 8.4.5 º¯È¯ to_ulong() ¸â¹ö ÇÔ¼ö´Â bitsetÀ» 'unsigned long'À¸·Î ¹Ù²Û´Ù. ´õ ¸¹Àº ¿ø¼Ò¸¦ °¡Áö´Â bitset¿¡ ÀÌ ¿¬»êÀ» ¼öÇàÇÏ¸é ¿¡·¯¸¦ ¹ß»ýÇÑ´Ù. to_string() ¸â¹ö ÇÔ¼ö´Â bitsetÀ» 'string' ŸÀÔÀ¸·Î ¹Ù²Û´Ù. À̶§ stringÀº bitsetÀÇ ¿ø¼Ò°¹¼ö¸¸Å­ÀÇ ¹®ÀÚ¸¦ °¡Áö°Ô µÈ´Ù. °¢°¢ÀÇ 0ºñÆ®´Â ¹®ÀÚ '0'À¸·Î ¹Ù²Ù°í, 1ºñÆ®´Â ¹®ÀÚ '1'·Î ¹Ù²Û´Ù.