profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image
เซฎโ‚หถแต” แต• แต”หถโ‚Žแƒโ™ก + 24
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
list_img
[C++] STL ์—ฐ๊ด€์ปจํ…Œ์ด๋„ˆ STL Associative Container (sets and multisets, maps and multimaps)
user-img ์ฐœ๋‹
2024.03.29
ใ…Žใ…Ž ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ Associative Container automatically sorted ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ ( ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ set์ด ๋งŒ๋“ค์–ด์ง ๋ณต์‚ฌ์ƒ์„ฑ์ž copy constructor set set3(set1); set set4 = set1; ์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor set set5 = {10, 30, 20}; //Elements will be stored in ascending order ๋ฒ”์œ„ ์ƒ์„ฑ์ž Range constructor set::iterator iter = set3.begin(); ++iter; // ๋˜๋Š” auto iter = set3.begin()์œผ๋กœ ์จ๋„๋จ set set4(iter, set3.end()); // set4..

 

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 +

ใ…Žใ…Ž 

 

์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ Associative Container

automatically sorted
์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ (<)

 


Sets and Multisets

  • automatically sorted ( By default, in ascending order)
  • multisets์€ ์ค‘๋ณตํ—ˆ์šฉ, sets๋Š” ํ—ˆ์šฉ ์•ˆํ•จ !

๊ธฐ๋ณธ์ƒ์„ฑ์ž default constructor

set<int> set1;
set1.insert(6);
set1.insert(2);
set1.insert(4);  // set is: 2 4 6

-> ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ set์ด ๋งŒ๋“ค์–ด์ง

๋ณต์‚ฌ์ƒ์„ฑ์ž copy constructor

set<int> set3(set1);
set<int> set4 = set1;


์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor

set<int> set5 = {10, 30, 20};
//Elements will be stored in ascending order


๋ฒ”์œ„ ์ƒ์„ฑ์ž Range constructor

set<int>::iterator iter = set3.begin(); ++iter;
// ๋˜๋Š” auto iter = set3.begin()์œผ๋กœ ์จ๋„๋จ
set<int> set4(iter, set3.end());
// set4 is: 20 30 40

 

set::erase(value)

size_type erase(const value_type& val);
// erase() ํ•จ์ˆ˜๋Š” ์‚ญ์ œ๋œ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•จ! 

multiset<int> aMultiSet =
{ 14, 12, 13, 17, 14, 19, 18, 16, 14, 14, 19, 11, 15 };
size_t itemsDeleted = aMultiSet.erase(14);
cout << itemsDeleted << "\n"; //4, ์‚ญ์ œ๋œ 14์˜ ๊ฐฏ์ˆ˜
for (auto elem : aMultiSet) cout << elem << " "; // 11 12 13 15 16 17 18 19 19

 

set::erase(position)

iterator erase(const_iterator position);

multiset<int> aMultiset = { 11, 12, 13, 15, 17 };
auto iterSet = ++aMultiset.begin(); 
cout << *iterSet << endl; //12

iterSet = aMultiset.erase(iterSet); //-> ์‚ญ์ œ๋œ ์š”์†Œ์˜ ๋‹ค์Œ์š”์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚ด
cout << *iterSet << endl; // 13

for(auto elem : aMultiSet) 
	cout << elem << endl; // 11 13 15 17

 

set::erase(range)

iterator erase(const_iterator first, const_iterator last);

set<int> aSet = {11, 12, 13, 15, 17, 18, 19};

auto iterSet = aSet.begin();
for (int i = 1; i < 6; ++i) 
{
	++iterSet; //5๋ฒˆ๋ฐ˜๋ณต 
}
cout << *iterSet << endl; // 18 (6th element)

iterSet = aSet.erase(++aSet.begin(), iterSet); // 12 ~ 18 ์‚ฌ์ด์— ์žˆ๋Š”๊ฑฐ ๋‹ค์ง€์›€ 
cout << *iterSet << endl; // 18(์‚ญ์ œ๋œ ์š”์†Œ ๋’ค์— ์žˆ๋Š” ๋ฐ”๋กœ ์ฒซ ์š”์†Œ

for(auto elem:aSet)
	cout << elem << endl; // 11 18 19

-> ์™œ ๋งˆ์ง€๋ง‰์ด 11 12 18 19 ๊ฐ€ ์•„๋‹ˆ์ง€?? 

set::find(value)

set<int> aSet = { 23, 34, 48, 57, 68, 71, 83 };
auto iter = aSet.find(57); //iter์€ 57์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๊ฐ€๋จ 

if (iter != aSet.end())
cout << "Element found."; // set์—์„œ 57์„ ์ฐพ์œผ๋ฉด ์ถœ๋ ฅ !
else
cout << "Element not found.";

 

* find ํ•จ์ˆ˜๋Š” True and False, Boolean Value๋กœ ๋ฆฌํ„ดํ•  ์ˆ˜ ์—†๋‹ค ! 

 


Maps and Multimaps

  • elements๋ฅผ key/value์Œ์˜ ํ˜•ํƒœ๋กœ ๊ด€๋ฆฌํ•จ 
  • automatically sorted ( By default, in ascending order)
  • multimaps์€ ์ค‘๋ณตํ—ˆ์šฉ, maps๋Š” ํ—ˆ์šฉ ์•ˆํ•จ !
 

 

์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor

-> When data is immediately available !! 

map<int, string> aMap = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}};

auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";

 

 

๊ธฐ๋ณธ์ƒ์„ฑ์ž์™€ ๋Œ€๊ด„ํ˜ธ(?) Default Constructor + Subscript

-> When data is NOT immediately available !!

map<char, int> aMap;
aMap['A'] = 90;
aMap['b'] = 80;
aMap['C'] = 70;
aMap['D'] = 60;

auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";

// Output : A 90, C 70, D 60, b 80
// ASCII code ์— ๋”ฐ๋ผ์„œ b 80์ด ๋’ค๋กœ ๊ฐ

 

๊ธฐ๋ณธ์ƒ์„ฑ์ž Default Constructor + map::insert

-> insert / make_pair ์‚ฌ์šฉ !!

map<int, int> intMap;

for (int i = 1; i < 10; ++i) // insert integers
intMap.insert (make_pair (i, (100 / i)));

auto itEnd = intMap.cend();
for (auto it = intMap.cbegin(); it != itEnd; ++it)
cout << it->first << " " << it->second << "\n";

// map is: {(1,100),(2,50),(3,33),...(8,12),(9,11)}

 

map::find(value)

const_iterator find(const value_type& k) const;
map<string, int> states = {{"AK", 1959}, {"CA", 1850}, {"FL", 1845}, {"IL", 1818}, {"IO", 1846}, {"OR", 1859}};

auto iter = states.find("CA");
if (iter != states.end())
	cout << "Year: " << iter->second;
else
	cout << "State not found.";

 

 

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