1) list !!!
- sll, dll, list ๋ฑ์ ๊ธฐ๋ณธ์ nullptr์ธ์ง ์๋์ง ํ์ธํ๋๊ฑฐ..
- count == 0 ์ด๋ next_it != nullptr ์ด๋ ๊ฒ ๊ฐ
- iterator์ด ์ฐ์ด๋ ๊ฒฝ์ฐ์๋ next_it != lst.end();
- 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 !!
- ์ด๋ฒ์๋ ๋ฒกํฐ ! -> ์ด๋ฒ์๋ nullptr์ธ์ง ์๋์ง ํ์ธํ๊ธฐ.
๋ฒกํฐ๋ ์์์ ๊ทผ๋ฐ๋ณต์(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' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] STL Algorithms(8)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.04 |
---|---|
[C++] Associative Containers(7)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.04 |
[C++] DLL(4)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.02 |
[C++] SLL(3)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.01 |
[C++] STL Pair, Nested Containers ์ค์ฒฉ ์ปจํ ์ด๋ (0) | 2024.03.31 |