profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image

1) list !!!

  1. sll, dll, list ๋“ฑ์˜ ๊ธฐ๋ณธ์€ nullptr์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š”๊ฑฐ.. 
    1. count == 0 ์ด๋‚˜ next_it != nullptr ์ด๋ ‡๊ฒŒ ๊ฐ
    2. iterator์ด ์“ฐ์ด๋Š” ๊ฒฝ์šฐ์—๋Š” next_it != lst.end();
  2. auto it = lst.begin(); auto it_next = next(it); 
void deleteListDuplicates(list<int>& lst)
{
   auto it = lst.begin(); 
   auto next_it = next(it); // ++lst.begin();
   
   while(next_it != lst.end())
   {
      if(*it == *next_it)
      {
         next_it = lst.erase(next_it);
      }
      else
      {
         ++it;
         ++next_it;
      }
   }
}
void deleteListDuplicates(list<int>& aList)
{
    auto iterLeft = aList.begin();    // Cleaner to use “auto” for iterators.
    auto iterRight = ++aList.begin(); // Note the increment operator.
    auto iterEnd = aList.end();       // Avoid calling function list::end() in
    while (iterRight != iterEnd)      //   the while loop header.
    {
        if (*iterLeft == *iterRight)
        {
            iterRight = aList.erase(iterRight);
        }
        else
        {
            ++iterLeft;     // We can do this (different from the vector
            ++iterRight;    // implementation), because we are deleting
        }                   // “consecutive” duplicates.
    }
}

 

 

2) vector !! 

  1. ์ด๋ฒˆ์—๋Š” ๋ฒกํ„ฐ ! -> ์ด๋ฒˆ์—๋„ nullptr์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๊ธฐ. 
  2. ๋ฒกํ„ฐ๋Š” ์ž„์˜์ ‘๊ทผ๋ฐ˜๋ณต์ž(random access iterator) ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•จ.  ๋•Œ๋ฌธ์— it +1,์ด๋ ‡๊ฒŒ๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ++it์€ ๋ถˆ๊ฐ€๋Šฅํ•จ . 
void deleteVectorDuplicates(vector<int>& vec)
{
   auto it = vec.begin();
   
   while(it != vec.end())
   {
      auto next_it = next(it) ; //it + 1 ๊ฐ€๋Šฅ! ๊ทธ๋Ÿฌ๋‚˜ ++it, vec.begin()++์€ ๋ถˆ๊ฐ€๋Šฅ
      while(next_it != vec.end())
      {
         if(*it == *next_it )
         {
            next_it = vec.erase(next_it);
         }
         else
         {
            next_it++;
         }
      }
      it++;
   }
}

 

'เซฎโ‚หถแต” แต• แต”หถโ‚Žแƒโ™ก/coding' Related Articles +