profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image
๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ + 28
list_img
์ตœ๊ทผ ์œ„์‹œ๋ฆฌ์ŠคํŠธ ๊ณต๊ฐœ ส• •แดฅ• ส”
user-img ์ฐœ๋‹
2024.05.12
1. ํ”Œ๋žซํผ ๋ฅ˜ ์‹ ๋ฐœ๋“ค !!  ์•Œ์•„๋ณธ๊ฑด ์ด์ •๋„? ์›ป์ง€ํ˜•ํƒœ์˜ ์‹ ๋ฐœ์€ ๋‚˜์—๊ฒŒ ๋„ˆ๋ฌด ๋ถˆํŽธํ•ด์„œ ์ด๋ ‡๊ฒŒ ์Šค๋ ˆ๋น ๊ฐ™์€ ํ”Œ๋žซํผ ํ˜•์‹์˜ ์‹ ๋ฐœ์ด ๊ฐ–๊ณ ์‹ถ๋‹ค ๋„˜ ๊ธฐ์—ฝ์ž๋‚˜  ์˜ˆ์‚ฐ 500๋ถˆ ์ดํ•˜  ! ^ใ…^   2. ๋Œ€์ถฉ ์–ด๊ทธ๋ถ€์ธ  .... ๊ฒจ์šธ ๋Œ€๋น„ ^ใ…^ ์˜ˆ์‚ฐ 150๋ถˆ ์ดํ•˜ !  3.์—๋ฅด๋ฉ”์Šค ๋ฏธ๋‹ˆํŒ ์š”์ฆ˜ ํ•‘ํฌ ์ƒ‰์— ๊ฝ‚ํ˜€์„œ ๋ฏธ๋‹ˆํŒ ํ•‘ํฌ๊ฐ€ ๋ˆˆ์— ๋“ค์–ด์˜จ๋‹ค.. ๋„˜๊ธฐ์—ฝ์ž๋‚˜ ์ƒค์›Œํ• ๋•Œ๋„ ์•…์„ธ์‚ฌ๋ฆฌ ์•ˆ๋นผ๋Š” ์‚ฌ๋žŒ์œผ๋กœ์„œ ํœ˜๋šคํ…œ์ด ํ•„์š”ํ•œ๋ฐ ๋ฉ€๋กœ ์‚ฌ์•ผ๋ ์ง€ ๊ณ ๋ฏผ์ด๋‹ด ..  ์˜ˆ์‚ฐ 500๋ถˆ ์ดํ•˜ !  4. ์ง€๊ฐ‘  wocํ˜•ํƒœ ๋„ˆ๋ฌด ๋‹ต๋‹ตํ•ด์„œ ๋ชป๊ฐ–๊ณ ๋‹ค๋‹ˆ๊ฒ ์Œ ๋ฌต์งํ•˜๊ณ  ( ๊ทธ์น˜๋งŒ ์ž‘๊ณ ) ์—์–ดํƒœ๊ทธ ๋„ฃ์„ ๋™์ „ ์ง€๊ฐ‘ ์ง€ํผ์žˆ๊ณ  ...์ด๋Ÿฐ ์•Œ๋œฐ์‚ด๋œฐ ๊ธฐ์—ฌ์šด ์ง€๊ฐ‘์ด ํ•„์š”ํ•˜๋‹ด  ์…€๋ฆฐ๋Š๋Š” ๊ฑ ์š”์ฆ˜ ๊ฝ‚ํ˜€์„œ ๋Œ์•„๋ดค๋Š”๋ฐ์นด๋“œ์ง€๊ฐ‘ ๋˜‘๋”ฑ์ด๋กœ ๋–ผ์–ด์ง€๋Š”๊ฒƒ๋„ ๊ทธ๋ ‡๊ณ  ๋ญ”๊ฐ€ ๋งˆ์Œ์— ์•ˆ๋“ ๋‹ค.. ๊ฑฐ์ถ”์žฅ..
list_img
[C++] Example - 2 !! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.04
Exam 2 - STL Functions to Know Functions to know how to use WITHOUT a description. STL vector/list/set/multiset/map/multimap Constructors (all those included in the lessons) begin/end/rbegin/rend clear empty operator= size erase(first, last) STL vector at assign(first, last), assign(n, val) erase(position), erase(first, last) front, back insert(position, val), insert(position, n, val) operator[]..
list_img
[C++] Queues(10)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.04
list_img
[C++] Stacks(9)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.04
list_img
[C++] STL Algorithms(8)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.04
1) ๋ฒกํ„ฐ !!! 2) ๋ฆฌ์ŠคํŠธ !! 3) vector
list_img
[C++] Associative Containers(7)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.04
1) multiset!! int difference(const set& s) { return *(--s.end()) - *s.begin(); //set์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•จ //end()๋ผ๋Š” ๋ฐ˜๋ณต์ž๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ --s.end()๋กœ ํ•จ!! } int difference(const set& aSet) { return (*aSet.crbegin() - *aSet.cbegin()); //-> ์ด๊ฒŒ ๋” ์ง๊ด€์ ์ธ๋“ฏ //crbegin()์€ ์—ญ๋ฐฉํ–ฅ๋ฐ˜๋ณต์ž์ค‘ ๊ฐ€์žฅ ํฐ ์š”์†Œ ! (reverse) //๋˜‘๊ฐ™์ด ํฐ๊ฐ’์—์„œ ์ž‘์€๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•จ. } 2) map !!!! // 10์˜ ๋ฐฐ์ˆ˜๋Š” 10,0 ์ผ์ผ€ / ์•„๋‹ˆ๋ฉด 12,10 ์ผ์ผ€ void multiplesOfTen(map..
list_img
[C++] Sequence Containers(6)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.02
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& 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; }..
list_img
[C++] DLL(4)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.02
1. front() #include "DoublyList.h" int DoublyList::front() const { return first -> getData(); } 2. back() #include "DoublyList.h" int DoublyList::back() const { return last -> getData(); } 3.insertBack() #include "DoublyList.h" void DoublyList::insertBack(int newData) { //Node* newNode = new Node(newData, nullptr); if(first == nullptr) { first = new DLLNode(newData, nullptr, nullptr); last = fir..
list_img
[C++] SLL(3)!! ์—ฐ์Šตํ•˜๊ธฐ ^ใ……^
user-img ์ฐœ๋‹
2024.04.01
1) #include "AnyList.h" // 1. insertBack -> ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๋ชฉ๋ก์˜ ๋งจ๋์— val์„ ๋„ฃ๋Š” ํ•จ์ˆ˜ // 2. new node ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— parameter ์— ์ €์žฅ๋œ ๊ฐ’์„ ์ €์žฅ -> Node* newNode = new Node(val); // 3. insertBack() ์‚ฌ์šฉ๋ฒ• !! void AnyList::insertBack(int val) { Node* newNode = new Node(val, nullptr); if(first == nullptr) { first = newNode; } else { Node* current = first; for(int i = 1; i getNext(); } cur..
list_img
[C++] STL Pair, Nested Containers ์ค‘์ฒฉ ์ปจํ…Œ์ด๋„ˆ
user-img ์ฐœ๋‹
2024.03.31
STL Pair pair combines two values in a single unit pair ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋‘๊ฐœ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง - first, second (์ „๋ถ€ public) #include ๋ฅผ ๊ผญ ๋„ฃ์–ด์•ผ๋Œ default constructor pair pairObj; overloaded constructor (with two parameters) pair pairObj(T1, T2); copy constructor pair pairObj(otherPairObj); EX1) #include ... void someFuction() { pair pair1(3, 5.4); cout

1. ํ”Œ๋žซํผ ๋ฅ˜ ์‹ ๋ฐœ๋“ค !! 

 

์•Œ์•„๋ณธ๊ฑด ์ด์ •๋„? 

์›ป์ง€ํ˜•ํƒœ์˜ ์‹ ๋ฐœ์€ ๋‚˜์—๊ฒŒ ๋„ˆ๋ฌด ๋ถˆํŽธํ•ด์„œ ์ด๋ ‡๊ฒŒ ์Šค๋ ˆ๋น ๊ฐ™์€ ํ”Œ๋žซํผ ํ˜•์‹์˜ ์‹ ๋ฐœ์ด ๊ฐ–๊ณ ์‹ถ๋‹ค 

๋„˜ ๊ธฐ์—ฝ์ž๋‚˜ 

 

์˜ˆ์‚ฐ 500๋ถˆ ์ดํ•˜  ! ^ใ…^ 

 

 

2. ๋Œ€์ถฉ ์–ด๊ทธ๋ถ€์ธ  

.... ๊ฒจ์šธ ๋Œ€๋น„ ^ใ…^

 

์˜ˆ์‚ฐ 150๋ถˆ ์ดํ•˜ ! 

 

3.์—๋ฅด๋ฉ”์Šค ๋ฏธ๋‹ˆํŒ 

์š”์ฆ˜ ํ•‘ํฌ ์ƒ‰์— ๊ฝ‚ํ˜€์„œ ๋ฏธ๋‹ˆํŒ ํ•‘ํฌ๊ฐ€ ๋ˆˆ์— ๋“ค์–ด์˜จ๋‹ค.. 

๋„˜๊ธฐ์—ฝ์ž๋‚˜ 

์ƒค์›Œํ• ๋•Œ๋„ ์•…์„ธ์‚ฌ๋ฆฌ ์•ˆ๋นผ๋Š” ์‚ฌ๋žŒ์œผ๋กœ์„œ ํœ˜๋šคํ…œ์ด ํ•„์š”ํ•œ๋ฐ 

๋ฉ€๋กœ ์‚ฌ์•ผ๋ ์ง€ ๊ณ ๋ฏผ์ด๋‹ด .. 

 

์˜ˆ์‚ฐ 500๋ถˆ ์ดํ•˜ ! 

 

4. ์ง€๊ฐ‘ 

 

wocํ˜•ํƒœ ๋„ˆ๋ฌด ๋‹ต๋‹ตํ•ด์„œ ๋ชป๊ฐ–๊ณ ๋‹ค๋‹ˆ๊ฒ ์Œ 

๋ฌต์งํ•˜๊ณ  ( ๊ทธ์น˜๋งŒ ์ž‘๊ณ ) ์—์–ดํƒœ๊ทธ ๋„ฃ์„ ๋™์ „ ์ง€๊ฐ‘ ์ง€ํผ์žˆ๊ณ  ...

์ด๋Ÿฐ ์•Œ๋œฐ์‚ด๋œฐ ๊ธฐ์—ฌ์šด ์ง€๊ฐ‘์ด ํ•„์š”ํ•˜๋‹ด 

 

์…€๋ฆฐ๋Š๋Š” ๊ฑ ์š”์ฆ˜ ๊ฝ‚ํ˜€์„œ ๋Œ์•„๋ดค๋Š”๋ฐ

์นด๋“œ์ง€๊ฐ‘ ๋˜‘๋”ฑ์ด๋กœ ๋–ผ์–ด์ง€๋Š”๊ฒƒ๋„ ๊ทธ๋ ‡๊ณ  ๋ญ”๊ฐ€ ๋งˆ์Œ์— ์•ˆ๋“ ๋‹ค.. 

๊ฑฐ์ถ”์žฅ ์Šค๋Ÿฌ์›Œ... 

 

์•„์ง ์™ ๋งˆ์Œ์— ๋“œ๋Š”๊ฒŒ ์—†์–ด์„œ ๋„์ €ํžˆ ๋ชป์ฐพ์œผ๋ฉด 

๋‹ค์‹œ ๋งˆ๋—‘ํ‚ด์œผ๋กœ ๋Œ์•„๊ฐˆ๋“ฏ 

๋ฉ”ํƒˆ ๋กœ๊ณ  ๋ฌด๊ฑฐ์›Œ์„œ ์ž˜ ์žƒ์–ด๋ฒ„๋ฆฌ์ง€๋„ ์•Š๊ณ  ๊ทธ๊ฒŒ ์กฐ์•—๋Š”๋ฐ 

์ด๋ฒˆ ์ง€๊ฐ‘์€ ๋„ˆ๋ฌด ๊ฐ€๋ฒผ์›Œ์„œ ์ž˜ ์žƒ์–ด๋ฒ„๋ฆด๊ฑฐ๊ฐ™์• ์„œ ๋ฌด์„ญ๋Œœ 

 

์˜ˆ์‚ฐ 100๋งˆ๋„Œ ์ดํ•˜ 

 

 

5. ๋ฏธ๋‹ˆ๊ฐ€๋ฐฉ 

ํœ˜๋šค๋งˆ๋šค ์ž˜๋“ค๊ณ ๋‹ค๋‹ ๋ฏธ๋‹ˆ ๋ฐฑํŒฉ !!!!!!!

๋””์ž์ด๋„ˆ ๋ธŒ๋žœ๋“œ๋Š” ๊ท€์ฐฎ์•„์„œ ์•ˆ์•Œ์•„๋ดค๊ณ  

๊ฑ ๋Œ€์ถฉ ์ด๋‘๊ฐœ ์ค‘์— ํ•˜๋‚˜์‚ด๊ฒ…๋ฏธ 

 

6. ์ปค์—ฌ์šด ํ‚ค๋ง 

์ด๊ฑฐ ๋„ˆ๋ฌด ๊ฐ–๊ณ ์‹œํผ์„œ ์ฃฝ๊ฒŸ์„œ . 

๋„˜ ๊ธฐ์—ฝ์ž๋‚˜ 

 

7. ๊ทธ๋‹ค์Œ์— ๊ฐ–๊ณ ์‹ถ์€๊ฒƒ๋“ค...

๊ธฐ์ฐฌ์•„์„œ ์‚ฌ์ง„์„ ๋ชป์˜ฌ๋ฆฌ๊ฒ ์œผ ใ…

๋‹ค์ด์Šจ ์—์–ด์ŠคํŠธ๋ ˆ์ดํŠธ๋„ˆ ์‚ฌ๊ณ ์‹ถ๊ณ  

๋ก์‹œ๋•… ๋„ค์ผ ์—์„ผ์Šค ์‚ฌ๊ณ ์‹ถ๊ณ  

๋ธ”๋ฃจ ๋งˆ์ดํฌ๋ž‘ 

์˜ฅํ† ๋ฒ„๋”” .. ํ•‘ํฌ ์žฌ์ž…๊ณ  ๋˜๋ฉด ์‚ด๊พธ์•ผ .. 

๋ณด์กฐ๋ฐฐํ„ฐ๋ฆฌ๋„ ์‚ฌ์•ผ๋˜๋Š”๋ฐ ๋จผ๊ฐ€ ๋ฉด์„ธ์ ์—์„œ ์‚ด๋ผ๊ณ  ์กด๋ฒ„์ค‘ 

๋ณด๋‹ค๋‚˜ ๋ด‰๊ณ ๋ฐ๊ธฐ๋Š” ๊ทธ๋ƒฅ ์‚ด๋ ค๊ตฌ . . 

'spam musubi ๐Ÿ™' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋ง‘๋ˆˆ๊ด‘ ๋ฌผ๊ฐœ  (0) 2023.09.17
'spam musubi ๐Ÿ™' Related Articles +

 

STL_Functions_to_Know.pdf
0.27MB

<Functions to know how to use WITHOUT a description.>

Exam 2 - STL Functions to Know

Functions to know how to use WITHOUT a description.

 

  • STL vector/list/set/multiset/map/multimap
    • Constructors (all those included in the lessons)
    • begin/end/rbegin/rend
    • clear
    • empty
    • operator=
    • size
    • erase(first, last)
  • STL vector
    • at
    • assign(first, last), assign(n, val)
    • erase(position), erase(first, last)
    • front, back
    • insert(position, val), insert(position, n, val)
    • operator[]
    • pop_back, push_back
    • reserve, resize
    • swap

 

  • STL list
    • assign(first, last), assign(n, val)
    • front, back
    • pop_back, pop_front, push_back. push_front
    • erase(position)
    • insert(position, val), insert(position, n, val)
    • merge (list x)
    • remove(val), remove_if(predicate), sort()
    • splice(position, x), splice(position, x, i), splice, position, x, first, last)
    • swap(list x)
    • unique()
  • STL set/multiset/map/multimap
    • count
    • erase(val)
    • insert(val), insert(first, last), insert(initializer list)
    • swap
  • STL map/multimap
    • maps only: operator[]
    • insert(make_pair)
    • find

 

  • STL stack
    • Constructor
    • empty
    • pop
    • push
    • size
    • swap (member function)
    • top
  • STL queue
    • Constructor
    • back
    • empty
    • front
    • pop
    • push
    • size
    • swap (member function)
  • STL algorithms
    • advance(position, n)
    • next (position, n)

 


 

Functions to know how to use WITH a given description.

On the exam, you will have descriptions for the functions listed below.

Here is an example of an STL function description:

std::fill_n
OutputIterator fill_n (OutputIterator first, Size n, const T& val);

Fill sequence with value
Assigns val to the first n elements of the sequence pointed by first.
Parameters
- first: Output iterators to the initial position in a sequence of at least n elements that support being assigned a value of type T.
- n: Number of elements to fill.
- val: Value to be used to fill the range.
Return value
An iterator pointing to the element that follows the last element filled.

 

 

  • STL algorithms
  •    
  • About these functions:
    • adjacent_find
      • Some do not work with all containers. How can you find out which ones do work? By reading their description in cplusplus.com OR by trying them.
      • Some require a predicate function. Make sure you try them both ways by writing a non-member function and by using a lambda expression.
    • all_of, any_of
    • copy, copy_backward, copy_if, copy_n
    • count, count_if
    • fill, fill_n
    • find, find_if
    • for_each
    • is_sorted
    • remove, remove_if
    • replace, replace_if
    • reverse
    • rotate
    • sort(first, last), sort(first, last, comp)
    • swap
    • unique

 

 

The questions will cover:

Linked lists (one or two questions only).

The STL (see the “STL Functions to know” section for more details).

 

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

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

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

1) ๋ฒกํ„ฐ !!! 

 

 

2) ๋ฆฌ์ŠคํŠธ !! 

 

 

3) vector

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

1) multiset!!

int difference(const set<int>& s) 
{
    return *(--s.end()) - *s.begin();
    //set์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•จ
    //end()๋ผ๋Š” ๋ฐ˜๋ณต์ž๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ --s.end()๋กœ ํ•จ!!
}

int difference(const set<int>& aSet)
{
	return (*aSet.crbegin() - *aSet.cbegin()); //-> ์ด๊ฒŒ ๋” ์ง๊ด€์ ์ธ๋“ฏ
    //crbegin()์€ ์—ญ๋ฐฉํ–ฅ๋ฐ˜๋ณต์ž์ค‘ ๊ฐ€์žฅ ํฐ ์š”์†Œ ! (reverse) 
    //๋˜‘๊ฐ™์ด ํฐ๊ฐ’์—์„œ ์ž‘์€๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•จ. 
}

 

2) map !!!!

// 10์˜ ๋ฐฐ์ˆ˜๋Š” 10,0 ์ผ์ผ€ / ์•„๋‹ˆ๋ฉด 12,10 ์ผ์ผ€ 
void multiplesOfTen(map<int, int>& aMap)// map์˜ ํ˜•ํƒœ ๊ธฐ์–ตํ•˜๊ธฐ~
{
   int multiple = 0;

   for(auto& pair : aMap)
   {
      if(pair.first % 10 == 0)
      {
         multiple = pair.first;
      }
      else 
      {
         pair.second = multiple;
      }
   }
}

 

3) Set!!!

void copyAfterFive(const multiset<int>& aSet, multiset<int>& aEmptySet)
{
   //auto aSetElem = aSet.begin(); 
   

   for(auto elem : aSet)
   {
      if(elem > 5 && elem > -10000)
      {
         aEmptySet.insert(elem);
      }
   }
}

 

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

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 +

 

1. front() 

#include "DoublyList.h"

int DoublyList::front() const
{
	return first -> getData();
}

 

2. back()

#include "DoublyList.h"

int DoublyList::back() const
{
	return last -> getData();
}

 

3.insertBack()

#include "DoublyList.h"

void DoublyList::insertBack(int newData)
{
   //Node* newNode = new Node(newData, nullptr);
   if(first == nullptr)
   {
      first = new DLLNode(newData, nullptr, nullptr);
      last = first;
   }
   else 
   {
      last -> setNext(new DLLNode(newData, last, nullptr));
      last = last -> getNext();
   }
   ++count;
}

 

4)moveToList()

void DoublyList::moveToList(DoublyList& toList)
{
    // Assume parameter object is empty.
    // Do NOT assume the calling object is empty.
    // Do nothing if the calling object is empty.


    if (count != 0)
    {
        toList.first = first;
        toList.last = last;
        toList.count = count;


        // Detach the calling object from the nodes that
        // now belong to the parameter object.
        first = nullptr;
        last = nullptr;
        count = 0;


        // The calling object still exists, but it is empty
        // as it was when it was created before inserting
        // any elements.
    }
}

 

5) deleteElem()

 

void DoublyList::deleteElement(int elementToDelete)
{
	// Case 1: List is empty
	if (first == nullptr)  // OR: if (count == 0)					   
	{
		// Use cerr, rather than cout (why?)
		cerr << "The list is empty.\n";
	}
	else
	{
		// Set a Boolean value to keep track of 
		// whether the item is found or not.
		bool found = false;


		// There is only one node in the list.
		if (first == last)	// OR: if (count == 1)							
		{
			// Node to be deleted is the first node.
			if (first->getData() == elementToDelete)
			{
				delete first;
				first = last = nullptr;
				found = true;
			}
			
			// No need to make a case if the only node 
			// does not store the data to delete.
		}
		else // If there is more than one node...
		{
			// Check if the first node or the last node needs
			// to be deleted. Why should you check this 
			// separately? Because these are special case, 
			// where pointer first and last must be updated.


			if (first->getData() == elementToDelete)
			{
				first = first->getNext();
				delete first->getPrev();
				first->setPrev(nullptr);
				found = true;
			}
			else if (last->getData() == elementToDelete)
			{
				last = last->getPrev();
				delete last->getNext();
				last->setNext(nullptr);
				found = true;
			}
			else // Node to delete might be somewhere in between 
			     // the first node and the last node.
			{
				// Create a pointer to traverse the list.
				// Start pointing at the second node, because
				// you already know that it is not the first node,
				// and stop before checking the last node for the
				// same reason.


				DLLNode* current = first->getNext();
				while (!found && current != last)
					// Don't let the WHILE loop continue if 
					// the element is found!
				{
					if (current->getData() == elementToDelete)
					{
						// connect previous and next node
						current->getPrev()->setNext(current->getNext());
						current->getNext()->setPrev(current->getPrev());
						// NOTE: It would be more efficient to create 
						// a pointer pointing to the previous node and 
						// another pointer pointing to the next node,
						// because we would not have to call too many 
						// functions, but this is good practice for the exam.


						delete current;
						current = nullptr;
						// Don't leave a dangling pointer!


						found = true;
					}
					else
						current = current->getNext();
				}
			}
		}


		if (!found)
			cout << "Element is not in the list." << endl;
		else
			--count; // This statement is needed only once.
	}
}
'เซฎโ‚หถแต” แต• แต”หถโ‚Žแƒโ™ก/coding' Related Articles +

1) 

#include "AnyList.h"
// 1. insertBack -> ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๋ชฉ๋ก์˜ ๋งจ๋์— val์„ ๋„ฃ๋Š” ํ•จ์ˆ˜ 
// 2. new node ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— parameter ์— ์ €์žฅ๋œ ๊ฐ’์„ ์ €์žฅ -> Node* newNode = new Node(val);
// 3. insertBack() ์‚ฌ์šฉ๋ฒ• !!

void AnyList::insertBack(int val)
{
	Node* newNode = new Node(val, nullptr);
	if(first == nullptr)
    {
    	first = newNode;
    }
    else
    {
    	Node* current = first;
        for(int i = 1; i < count ;++i)
        {
        	current = current -> getNext();
        }
        current ->setNext(newNode);
    }
    count++;
}

 

<insertBack ์‚ฌ์šฉ๋ฒ•>

  1. Node* newNode = new Node(newData, nullptr) <- ๊ธฐ๋ณธ ํ˜•ํƒœ ! 
  2. ์šฐ์„  ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€, ์•„๋‹ˆ๋ฉด ๋ญ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•ด์•ผ๋จ 
    1. ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋‹ค -> first๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค
    2. ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•ˆ๋น„์–ด์žˆ๋‹ค -> iteratorํˆด๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋ถ€ ๋Œ๋ ค์„œ ๋งจ๋(back)์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ํ•œ๋‹ค 
  3. ๋งˆ์ง€๋ง‰์— count++; ์žŠ์ง€์•Š๊ธฐ 

2)

#include "AnyList.h"

int AnyList::getNumOfElements() const
{
	return count; 
}

 

-> const ๋ฅผ ์–ธ์ œ์–ธ์ œ ๋„ฃ์–ด์•ผ๋˜๋Š”์ง€, ๋‹ฌ๋ผ์ง€๋Š” ์ ์ด ๋ญ”์ง€ ์•Œ์•„๋‘์ž ! 

 

3)

 

  1. while๋ฌธ(๋งŒ์•ฝ์— element์„ ์ฐพ์œผ๋ฉด) ์œผ๋กœ true ์ด๋ฉด return ํ•˜๊ณ  ๋จธ ์ด๋Ÿฐ์‹์œผ๋กœ 
  2. parameter์€ int, return ๊ฐ’์„ boolean. 
  3. ์ผ๋‹จ ๋ชจ๋“  elem์„ ๋‹ค ๋Œ์•„๋ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ ์จ์•ผํ•จ
    1. while(int elem : ์ „์ฒด? ) ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฐ‘์— if else ์œผ๋กœ bool ๊ฐ’ ์ง€์ • ( count๋งŒํผ ๋ฐ˜๋ณต๋  ์˜ˆ์ •) 
    2. *** ์ด๊ฑด node ์ด๊ธฐ ๋•Œ๋ฌธ์—  "-> getData" ์ด๋Ÿฐ๊ฑฐ ๋‹ค์จ์•ผ๋Œ!!!!!! " ***
bool AnyList::search(int elem) const 
{
   if(count == 0)
   {
      cerr << "The list is empty." << endl; 
      return false;
   }
   else 
   {
      Node* current = first; 
      
      while(current != nullptr)
      {
         if(current -> getData() == elem)
         {
            return true;
         }
         else
         {
            current = current -> getNext(); 
         }
      }
      return false;
   }
}

 

4)

void AnyList::replaceData(int oldKey, int newKey)
{
	bool found = false; // to stop the loop when key is found
	Node* current = first;
	while (current != nullptr && !found)
	{
		if (current->getData() == oldKey)
			found = true;
		else
			current = current->getNext();
	}


	if (current == nullptr) // key was not found
		cout << "Element is not in the list." << endl;
	else
		current->setData(newKey);
}
// ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ๊ฑฐ ๊นŒ์ง€๋Š” 
void AnyList::replaceData(int oldData, int newData)
{
   Node* current = first; 
   
   while(current != nullptr)
   {
      if(current ->getData() == oldData)
      {
         current -> setData(newData);
      }
      else
      {
         cout << "Element is not in the list." << endl;
         current = current -> getNext();
      }
   }
} // ์ด๊ฑฐ์˜€์–ด ๊ทผ๋ฐ,, ์ด๋Ÿฌ๋ฉด element is not in the list ๊ฐ€ ๊ฐœ๋งŽ์ด ๋‚˜์˜ด ์™œ๋ƒ๋ฉด์€ ์•ˆ๋‚˜์˜ฌ๋•Œ๋งˆ๋‹ค ๊ณ„์† ๋˜์•ผ๋˜๋‹ˆ๊นŒ ...

 

... ํ”Œ๋ž˜๊ทธ ์“ฐ๋Š”๊ฑฐ ๋„ˆ๋ฌด ์–ด๋ ต๋‹ค ใ…  .... ์—ฐ์Šตํ•˜์Ÿˆ ! 

 

5) 

void AnyList::deleteElem(int elem)
{
    if (count == 0) // OR: (first == nullptr)
    {
	  // Review the Course Style Guide to learn when to use cerr/cout.
        cerr << "The list is empty." << endl;
    }
    else
    {
        // Search if the elem is in the list.
        // When found, delete the node.


        // if it is the first node
        if (first->getData() == elem)
        {
            Node* current = first;
            first = first->getNext();
            delete current;
            current = nullptr;
            --count;
        }
        // else (could be any of the other nodes)
        else
        {
            bool found = false;
            Node* trailCurrent = first;
            Node* current = first->getNext();
            // OR: Node* current = trailCurrent->getNext();


            while (!found && current != nullptr)
            {
                if (current->getData() == elem)
                {
                    trailCurrent->setNext(current->getNext());
                    delete current;
                    current = nullptr;
                    --count;
                    found = true;
                }
                else
                {
                    trailCurrent = current;
                    current = current->getNext();
                }
            }


           if (!found) // Do NOT write (found == false) !!!
                       // Redundant statement.
               cout << "Element " << elem 
                   << " is not in the list.\n";
        }
    }
}

 

์ด๊ฒƒ๋„ ์–ด๋ ต๋‹ค .. ๋‹ค์‹œ๋‹ค์‹œ ใ…  

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

STL Pair 

  • pair combines two values in a single unit
  • pair ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋‘๊ฐœ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง - first, second (์ „๋ถ€ public)
  • #include <utility> ๋ฅผ ๊ผญ ๋„ฃ์–ด์•ผ๋Œ 
default constructor pair<T1, T2> pairObj;
overloaded constructor (with two parameters) pair<T1, T2> pairObj(T1, T2);
copy constructor pair<T1, T2> pairObj(otherPairObj);

 

EX1)

#include <utility>
...
void someFuction()
{
	pair<int, double> pair1(3, 5.4);
    	cout << pair1.first() << ", " << pair1.second() <<endl;
}

 

EX2)

#include <utility>

pair<int, double> p1;
p1.first() = 3;
p1.second() = 2.5;

pair<int, double> p2(13, 4.5);
pair<string, int> student("Bob", 123);

cout << student.first();
cout << student.second();

 

EX3)

Pair's memebr variables should be PUBLIC ! The program will CRASH!!!

#include <utility>
...
void someFunction()
{
	pair<int,MyClass> pair1; //default values
    cout << pair1.first() << pair1.second(); 
}

class MyClass
{
	public:
    	...
    private:
    	string str;
        double d;
}

Nested Containers ์ค‘์ฒฉ์ปจํ…Œ์ด๋„ˆ 

  • An STL container can store instances of other containers.
    • A vector of vectors
    • A list of pointers to vectors
    • A map of strings and sets
    • ๋Œ€์ถฉ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๋œป .. 

EX1) Vectors of Vectors

vector<vector<int>> studentGrades = {
		{98, 90, 100},	// student 1
        	{75, 97, 87},	// student 2
        	{96, 74, 88}	// student 3
};

cout << "Total number of students: " <<	studentGrades.size() << endl;
		// studentGrades ๋ฒกํ„ฐ์— ์ €์žฅ๋œ ํ•™์ƒ์˜ ์ˆ˜ -> 3
        
//iterating the elements in a vector of vectors
int row = 0;
for (const auto& students : studentGrades)
{
	cout << "Student " << row << "grades: ";
    for (auto grade : students)
    {
    	cout << grade << " ";
    }
    ++row;
    cout << "\n";
}

 

EX2) Map of Strings and Vectors

map<string, vector<int>> studentGrades = {
            {"Jane", {98, 90, 100}},
            {"Bob", {75, 97, 87}},
            {"Jill", {96, 74, 88}},
            };
            
cout << "Total number of students: " << studentGrades.size() << endl;

for (const auto& grades : studentGrades)
{
    cout << grades.first << ": ";
    for (auto grade : grades.second)
    {
        cout << grade << " ";
    }
    cout << "\n";
}

 

.. ๋€ผ

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