Skip to content

Latest commit

ย 

History

History
1383 lines (798 loc) ยท 35.9 KB

File metadata and controls

1383 lines (798 loc) ยท 35.9 KB

[์•ž์œผ๋กœ ๋‚ด๊ฐ€ ํ•  ์ผ]

  1. ๊ต์ˆ˜๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„์„œ HeaderํŒŒ์ผ๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ฝ”๋”ฉํ•  ์ˆ˜ ์ž‡๋„๋ก ํ•˜๊ธฐ!
  2. ๊ธฐ์กด ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ฝ”๋“œ ๋ถ„์„ => try & catch
  3. ํŠน์ • ํด๋ž˜์Šค ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ฝ”๋“œ ์ถ”๊ฐ€, ์ฝ”๋ฉ˜ํŠธ ์“ฐ๊ธฐ, PPT ๋งŒ๋“ค๊ธฐ
  4. Clone(Deep copy, Shallow Copy) ์ถ”๊ฐ€
  5. Distribution learning์ฝ”๋“œ ์งœ๊ธฐ (๋ณ‘๋ ฌ์ฒ˜๋ฆฌ)
  6. Master Slave ๋ชจ๋ธ

์ง€๋„๊ต์ˆ˜ ํŒ€์› ๊ตฌ์„ฑ ํ”„๋กœ์ ํŠธ ์ฃผ์ œ ์‚ฌ์šฉํ•  ๊ธฐ์ˆ  ํ˜„์žฌ๊นŒ์ง€ ์ง„์ฒ™๋„ ํ–ฅํ›„ ๊ณ„ํš ํŒ€์›๊ณผ์˜ ๋ถ„๋ฐฐ

๊น€์ธ์ค‘ ์ผ์‹ฌ๋™์ฒด WICWIU(๋”ฅ๋Ÿฌ๋‹ ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ) Exception Handling ๋”ฅ๋Ÿฌ๋‹๊ณผ C++

์ฝ”๋“œ ๋ถ„์„ ์ค‘, try&catch ์ถ”๊ฐ€ ์ผ์‹ฌ๋™์ฒด

C++ ๊ณต๋ถ€

[ ๊ฐœ๋… ]

0. ์ „์ฒ˜๋ฆฌ๋ฌธ ์ •๋ฆฌ
1. #ifdef => 'n' ์€ no์ด๋‹ค.
#ifdef TEST       // TEST๊ฐ€ #define์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด ์‹คํ–‰
#endif

#ifndef TEST     // TEST๊ฐ€ ์ •์˜ ์•ˆ๋˜์–ด ์žˆ์œผ๋ฉด ์‹คํ–‰
#endif
1-1. #define
๋‹จ์ˆœํžˆ ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ˆซ์ž์ฒ˜๋ฆฌ ํ•ด์ฃผ๋Š”๊ฒŒ ๋‹ค๊ฐ€ ์•„๋‹ˆ๋‹ค.

1556898632897

2. #includeํ•  ๋•Œ, <>์™€ ""
#include <>   // ์‹œ์Šคํ…œ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ include
#include ""   // ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ include

cf) ์ฐธ๊ณ ๋กœ mainํ•จ์ˆ˜๋Š” iostream์— ์žˆ๋‹ค.

3. inline ํ•จ์ˆ˜๋ž€?

: headerํŒŒ์ผ์—์„œ ์„ ์–ธ๊ณผ ๋™์‹œ์— ๊ตฌํ˜„ํ•ด๋‘๋Š” ๊ฒƒ.

4. headerํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

: ์šฐ์„  ํ—ค๋”ํŒŒ์ผ์ด๋ž€ function prototype(ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋‘๋Š” ๊ฒƒ)์„ ๋ชจ์•„๋‘” ๊ฒƒ์ด๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์žฌํ™œ์šฉ์„ ์œ„ํ•ด์„œ.

5. reference, ๋ณ„๋ช… ๋ถ™์ด๊ธฐ
int a = 3;
int &k = a;   // k๋Š” a๋ž‘ ์™„์ „ ๋˜‘๊ฐ™์•„์ง€๋Š” ๊ฑฐ์ž„
int &k = 3;   // ์ด๊ฑฐ๋Š” ์•ˆ๋œ๋‹ค. ๋‘˜๋‹ค rvalue(๋ฉ”๋ชจ๋ฆฌ์ฃผ์†Œ๊ฐ€ ์—†๋Š”๊ฒƒ)๋ผ์„œ..
6. ์ปดํŒŒ์ผ
/*
	.cppํŒŒ์ผ  =>(์ปดํŒŒ์ผ)=> .oํŒŒ์ผ  =>(link)=>  .exeํŒŒ์ผ  
*/


// ๊ฐ€๋ น, ํŒŒ์›Œ์‰˜์—์„œ
=> g++ -c hello.cpp
// ํ•˜๋ฉด compile๋งŒ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ .oํŒŒ์ผ ์ƒ์„ฑ๋จ
7. enum : ์‚ฌ์šฉ์ž์ •์˜ ์ž๋ฃŒํ˜• ไธญ ์—ด๊ฑฐํ˜•

=> ํŠน์ •ํ•œ ์ƒํƒœ ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋‚ด์•ผ ํ•  ๋•Œ ์ฝ”๋“œ ๋ฌธ์„œํ™” ๋ฐ ๊ฐ€๋…์„ฑ ๋ชฉ์ ์œผ๋กœ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค.

enum DeepLearning{
    CNN,   // ์ˆœ์„œ๋Œ€๋กœ ์ •์ˆ˜๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.(default 0)
    RNN,
    GAN,
}; // ์ด ์ƒํƒœ๋Š” ๋ฉ”๋ชจ๋ฆฌํ• ๋‹นX

DeepLearning d1 = CNN;  // ๋ฉ”๋ชจ๋ฆฌํ• ๋‹น
8. magic number๋ž€?

: ์„ค๋ช… ์—†์ด ๋“ฑ์žฅํ•˜๋Š” ์ƒ์ˆ˜

ex) ํ•™์ƒ์˜ ํ•™์  ์ƒํƒœ๋ฅผ "1์ด๋ฉด ์žฌํ•™, 2์ด๋ฉด ํœดํ•™, 3์ด๋ฉด ์ œ์ , 4์ด๋ฉด ์กธ์—…"์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด ๊ฒฝ์šฐ, "์ด ํ•™์ƒ์ด ์กธ์—…ํ–ˆ๋Š”๊ฐ€"๋ฅผ ๋”ฐ์ง€๊ธฐ ์œ„ํ•ด "ํ•™์ƒ.์ƒํƒœ == 1" ๋”ฐ์œ„๋กœ ๋‚˜ํƒ€๋‚ผ ๊ฒฝ์šฐ์— 1,2,3 ์ด ๋งค์ง๋„˜๋ฒ„์— ํ•ด๋‹น๋œ๋‹ค.

๊ทธ๋ž˜์„œ 7.์˜ enum์„ ์‚ฌ์šฉํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š”๋ฐ ์ข‹๋‹ค.

9. argc์™€ argv
int main(int argc, char *argv[]){}

// argc : ์ฝ˜์†” ์ฐฝ์— ์ž…๋ ฅ๋˜๋Š” ๋ฌธ์ž์˜ ๊ฐฏ์ˆ˜
// argv : ์ฝ˜์†” ์ฐฝ์— ์ž…๋ ฅ๋˜๋Š” ๋ฌธ์ž์˜ ๋ฐฐ์—ด(char array๋กœ ๋ฐ›์•„์•ผํ•จ)
10. prompt์— ๋Œ€ํ•˜์—ฌ
int GetInt(std::string prompt = "Enter an integer: ");
// GetIntํ•  ๋•Œ๋Š” prompt๊ฐ€ ํ•„์š”ํ•œ๋ฐ,
// = ๋ถ€ํ„ฐ๋Š” optional argument๋กœ, GetInt()๋„ ๋˜๊ณ , GetInt(" sdf ")๋„ ๋œ๋‹ค๋Š” ๊ฒƒ.




 int GetInt(std::string prompt = "Enter an integer: ") {  
   int keyin;
   std::string line;
   std::cout << prompt;                   // display a prompt
   while (true) {
     if (std::getline(std::cin, line)) {  // get an input
       std::stringstream sstr(line);      // make sstream obj
       if (sstr >> keyin) break;          // scan for int
     }
     std::cout << "Retry: ";              // not an int, retry
   }
   return keyin;
 }
11. ์ปดํŒŒ์ผํ•  ๋•Œ,

: include๋Š” ํด๋”๋งŒ ์•Œ๋ ค์ฃผ๋ฉด ๋˜๊ณ  library๋Š” ํด๋”๋ž‘ ํŒŒ์ผ๋ช…๋„ ์•Œ๋ ค์ค˜์•ผํ•œ๋‹ค.

g++ selection.cpp insertion.cpp quicksort.cpp bubble.cpp sortDriver2.cpp -I../../include -L../../lib -lnowic -o sort2

๋งŒ์•ฝ์— .oํŒŒ์ผ์„ ๊ฑฐ์ณ์„œ .aํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด

์˜ˆ์‹œ๋กœ, libsort.a ํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด '-l' ๋‹ค์Œ์— ํŒŒ์ผ๋ช…์ด ์˜ค๋Š”๋ฐ lib ๋นผ๊ณ  'sort'๋งŒ ํ•ด์„œ ๋งํฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

g++ sort.cpp -I../../include -L../../lib -lsort
12. Static Library

: library๋ž€ ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผ ๋˜๋Š” object file์˜ ๋ชจ์Œ์ด๋‹ค. (Static library์™€ Shared library๊ฐ€ ์žˆ๋‹ค.)

  1. Static library๋Š” ์œ ๋‹‰์Šค์—์„œ๋Š” .a(์•„์นด์ด๋ธŒ) ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๊ณ , ์œˆ๋„์šฐ์—์„œ๋Š” .lib(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.

  2. Shared library๋Š” ์œ ๋‹‰์Šค์—์„œ๋Š” .so(shared object) ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๊ณ , ์œˆ๋„์šฐ์—์„œ๋Š” .dll(dynamic link library) ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„๋‹ค. (์ฐธ๊ณ ๋กœ g++์€ ๊ธฐ๋ณธ์ ์œผ๋กœ shared library๋ฅผ ๋งํฌํ•œ๋‹ค. )

[์ค‘์š”] Static library(.aํŒŒ์ผ) ๋งŒ๋“ค๊ธฐ

: nowic.cpp ํŒŒ์ผ์„ (.oํŒŒ์ผ์„ ๊ฑฐ์ณ์„œ) libnowic.a ํŒŒ์ผ๋กœ ๋งŒ๋“ค๊ฒƒ์ด๋‹ค.

g++ -c nowic.cpp -o nowic.o -I../include
ar rcs libnowic.a nowic.o     // ar rcs๋กœ library์ด๋ฆ„ object์ด๋ฆ„ ํ•ด์„œ static library ๋งŒ๋“ฌ
ar 				         // list all the options available(์“ธ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด ๋ณด์—ฌ์คŒ)
ar t libnowic.a 	     // list ~.o files archived(aํŒŒ์ผ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” oํŒŒ์ผ๋“ค์„ ๋ณด์—ฌ์คŒ)
ar x libnowic.a 	     // extract ~.o files archived
nm nowic.o 		         // list the actual function names in .o file

cf ) ar flags:

  • -r: Insert the files member... into archive (with replacement).
  • -c: Indicates you are creating an archive file
  • -s: Write an object-file index into the archive, change is made to the archive
  • struc-t: display contents of archive (show the list of .o files, use nm ~.o to see functions in ~.o)
13. ๋ฒกํ„ฐ(vector) ์‚ฌ์šฉํ•˜๊ธฐ : array์˜ ํ™•์žฅ
#include <vector>
using namespace std;

vector<int> v;  // ๋ฐฐ์—ด(array)์™€๋Š” ๋‹ค๋ฅด๊ฒŒ delete๋ฅผ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค.
vector<int> v(10);

cf) size_t ํƒ€์ž…

: size_t ํƒ€์ž…์€ C์–ธ์–ด์—์„œ ์ž„์˜์˜ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋ถ€ํ˜ธ์—†๋Š” ์ •์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, unsigned int๋กœ ์“ฐ์ด๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

13-1. vector ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋“ค
(1) .at
v.at(index)๋ž‘ v[index]๋ž‘ ๊ฐ™๋‹ค
(2) std::out_of_range
#include <iostream>       // std::cerr
#include <stdexcept>      // std::out_of_range
#include <vector>         // std::vector

int main (void) {
  std::vector<int> myvector(10);
  try {
    myvector.at(50)=100;      // vector::at throws an out-of-range
  }
  catch (const std::out_of_range& oor) { 
    std::cerr << "Out of Range error: " << oor.what() << '\n';
  }
}

// oor์€ ์–ด๋–ค error์ธ์ง€ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.
(3) reserve (reverse ์•„๋‹˜!!!)

: ์ธ์ˆ˜๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•จ.

(4) size

: ๋ฒกํ„ฐ ์›์†Œ ๊ฐฏ์ˆ˜

(5) push_back

: ๋ฒกํ„ฐ ๋’ค์— (์ธ์ˆ˜) ์ถ”๊ฐ€

14. ํ…œํ”Œ๋ฆฟ (ํด๋ž˜์Šค ํŽธ)
template<typename DTYPE>    // ํด๋ž˜์Šค๋ฅผ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์œตํ†ต์„ฑ์„ ๋†’์ด๊ฒ ๋‹ค. ์ด๋•Œ๋Š” Tensorholderํด๋ž˜์Šค๋ฅผ ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค.
class Tensorholder : public Operator<DTYPE>{  // ์ƒ์†๋ฐ›๋Š” Operator์˜ ํƒ€์ž…์„ ์ง€์ •ํ•ด๋„ ๋˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋„ DTYPE์œผ๋กœ ๋‘์–ด ์œตํ†ต์„ฑ์„ ๋†’์ด๊ณ  ์žˆ๋‹ค.
    Tensorholder(int pTimeSize, int pBatchSize, int pChannelSize, int pRowSize, int pColSize, std::string pName, int pTrainable = TRUE) : Operator<DTYPE>(pName) {
    } 	    // ์ƒ์„ฑ์ž
   	~Tensorholder() {
	}       // ์†Œ๋ฉธ์ž
}

// ์‹ค์ œ๋กœ main์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
Tensorholder<float> *x = new Tensorholder<float>(1, BATCH, 1, 1, 784, "x"); // Tensorholder๋ฅผ floatํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.
15. linked list(๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ)
struct Node{
  int item;
  Node *next;

  Node(int i, Node *n){
    item = i;
    next = n;
  }

  ~Node(){}
};

using pNode = Node*;

bool empty(pNode p){
  return p == nullptr;
}

pNode find(pNode p, int val){
  if(empty(p)) return nullptr;  // ์• ์ดˆ์— ๋…ธ๋“œ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์œผ๋ฉด ์ฐพ์ง€๋„ ๋งˆ๋ผ.
  pNode x = p;  // x => p1->data, p1->next => p2->data
                // ์ฆ‰, x๋Š” ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ์˜ data๋ฅผ ํฌ์ธํŒ… ํ•˜๊ณ  ์žˆ๋Š” ํ˜„์žฌ ๋…ธ๋“œ์˜ next๊ฐ’์ด๋‹ค.
  while(x != nullptr){   // x == nullptr์ด๋ผ๋Š” ๋ง์€ ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค. ํ˜„์žฌ ๋…ธ๋“œ์˜ next๊ฐ’์ด nullptr์ด๋‹ค.
    if(x->item == val) return x;   // ์ด ๋•Œ์˜ x๋Š” ๋‚ด๊ฐ€ ์ฐพ๋Š” ๊ฐ’์ด ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ทธ ์ „ ๋…ธ๋“œ์˜ next๊ฐ’์ด๋‹ค.
    x = x->next; // x๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” data๊ฐ€ ์žˆ๋Š”0, ์ฆ‰ ๋‹ค์Œ ๋…ธ๋“œ์˜ next๊ฐ’์œผ๋กœ x๋ฅผ ๋ฐ”๊ฟˆ
  }
  return x; // ๊ฒฐ๊ตญ, ๋ฆฌํ„ด ๊ฐ’์€ ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๊ฑฐ๋‚˜, nullptr์ด๋‹ค.
}
16. autoํƒ€์ž…
auto a = 0;  // a์˜ ํƒ€์ž…์„ ๋ณด๊ณ  ์•Œ์•„์„œ ํƒ€์ž…์„ ๊ฒฐ์ •
17. const
int get_size const{
    return size;
}  // size๊ฐ’์€ ๋ฆฌํ„ด ๋ฐ›์•„์„œ๋„ ๋ฐ”๊ฟ€์ˆ˜ ์—†๋‹ค.
18. ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž ํ™•์‹คํžˆ ์•Œ์ž

: new๋ฅผ ํ•˜๋ฉด ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , delete๋ฅผ ํ•˜๋ฉด ์†Œ๋ฉธ์ž๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ๊ฐ€์ƒํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ์†Œ๋ฉธ์ž๋„ ๊ฐ€์ƒํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด ๊ฐœ๋…์ด ์ดํ•ด๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์œ„ ํด๋ž˜์Šค ํฌ์ธํ„ฐ ํƒ€์ž…์œผ๋กœ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋™์ ํ• ๋‹น ํ•˜๋Š” ๊ฒƒ์„ ์„ ์ดํ•ด ํ•ด์•ผํ•œ๋‹ค. ์ด๋•Œ, ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์ด Contaioner๊ฐœ๋…์ด๋‹ค.

18-2. Container

: ํด๋ž˜์Šค ํ…œํ”Œ๋ฆฟ์œผ๋กœ, ๊ฐ™์€ ํƒ€์ž…์˜ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ์ผ์ข…์˜ ์ง‘ํ•ฉ

  1. Sequence Container(๋ฐ์ดํ„ฐ๋ฅผ ์„ ํ˜•์œผ๋กœ ์ €์žฅํ•˜๋ฉฐ, ํŠน๋ณ„ํ•œ ์ œ์•ฝ์ด๋‚˜ ๊ทœ์น™์ด ์—†๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์ปจํ…Œ์ด๋„ˆ)

    => array, vector, list, deque

  2. Associative Container(๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ • ๊ทœ์น™์— ๋”ฐ๋ผ ์กฐ์งํ™”ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ)

    => set, multiset, map, multimap

18-3. promotion

: ๋ถ€๋ชจ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์ž๋…€ ํƒ€์ž…ํด๋ž˜์Šค ์ƒ์„ฑ

=> ์–ด์จ‹๋“  new๋กœ ์ƒ์„ฑํ•˜์ž–์•„. ๋‹ค์‹œ๋งํ•ด์„œ

int *a = new int[5];   // intํƒ€์ž… ๊ณต๊ฐ„์„ 5๊ฐœ ๋งŒ๋“ค๊ณ  ๊ทธ array๋ฅผ a๊ฐ€ ๊ฐ€๋ฆฌํ‚ด

์—ฌ๊ธฐ์„œ a๋ผ๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์ ์–ด๋„ ์ž๊ธฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” element๋ณด๋‹ค๋Š” ์ƒ์œ„๊ฐœ๋…์ด์–ด์•ผ ํ’ˆ์„ ์ˆ˜ ์žˆ๋‹ค? ๋ถ€๋ชจ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ newํ•˜๋Š” ๊ฒƒ์€ ์ž์—ฐ์Šค๋Ÿฌ์šด ์ผ์ด๋‹ค. ๋‹จ, JAVA์—์„œ๋Š”

ex) Person p = new Student

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Student์•ˆ์— ์žˆ๋Š” ๊ณ ์œ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ์›๋ž˜์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผํ•œ๋‹ค.(ํƒ€์ž… ์บ์ŠคํŒ…) ๊ธฐ๋ณธ์ ์œผ๋กœ Person์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ!

Base *b = new Derived();  // Derived๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๊ณ  ์ด๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ b๋ฅผ ์ •์˜ํ•œ๋‹ค.
19. Semaphore & mutex

: critical-section(thread๊ฐ€ ๊ณตํ†ต ๋ณ€์ˆ˜๋ฅผ ๋ฐ”๊พธ๋ ค๋Š” ๊ตฌ๊ฐ„) ๋ฌธ์ œ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ solution์ด ๋™๊ธฐํ™”์ด๋‹ค. (๋™๊ธฐํ™” ํˆด์€ Semaphores, Monitors, ..)

: ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด!! thread1์ด ๋ณ€์ˆ˜ a์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, thread2๋„ ๋ณ€์ˆ˜ a์— ์ ‘๊ทผํ•ด์„œ ๊ฐ’์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋Š” ์ค‘์ด๋‹ค. ์ด ๋•Œ, ๋ณ€์ˆ˜ a๋ฅผ ๋ฐ”๊พธ๋Š” ๊ตฌ๊ฐ„์˜ ์ฝ”๋“œ๋ฅผ ciritical-section(CS)๋ผ๊ณ  ํ•˜์ž. CS์—๋Š” ํ•˜๋‚˜์˜ thread๋งŒ ๋“ค์–ด๊ฐ€์„œ ์ž‘์—…์„ ํ•ด์•ผ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ Semaphore(Sem)๋ผ๋Š” ๊ฐœ๋…์„ ๋„์ž…ํ•˜๋Š”๋ฐ Sem์˜ ๊ฐ’์ด ์Œ์ˆ˜๊ฐ€ ๋˜๋ฉด CS์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•˜์—ฌ ์œ„์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Sem๊ฐ’์„ ์ฒ˜์Œ์— 1๋กœ ๋‘๊ณ , thread๊ฐ€ CS๊ตฌ๊ฐ„์— ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— Sem๊ฐ’์„ -1ํ•ด์ฃผ๊ณ , CS๊ตฌ๊ฐ„์„ ๋‚˜์˜ค๋ฉด Sem๊ฐ’์„ +1ํ•ด์ฃผ๋Š”๊ฒŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

  1. Mutual exclusion(์ƒํ˜ธ๋ฒ ํƒ€) : ์˜ค์ง ํ•œ thread๋งŒ ์ง„์ž…
  2. Progress(์ง„ํ–‰) : ์ง„์ž… ๊ฒฐ์ •์€ ์œ ํ•œ ์‹œ๊ฐ„ ๋‚ด
  3. Bounded waiting(์œ ํ•œ๋Œ€๊ธฐ) : ์–ด๋А thread๋ผ๋„ ์œ ํ•œ ์‹œ๊ฐ„ ๋‚ด

busy waiting : Sem๊ฐ’ ๋•Œ๋ฌธ์— ๊ณ„์† while๋ฌธ ๋Œ๊ณ ์žˆ๋Š” ์ƒํ™ฉ

block : ํ”„๋กœ์„ธ์Šค๋ฅผ waiting Q์— ๋‘๋Š” ๊ฒƒ

wakeup : ํ”„๋กœ์„ธ์Šค๋ฅผ ready Q๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ

20. ๊ฐ€์ƒ ์†Œ๋ฉธ์ž // virtual ~CLASS();

: ํด๋ž˜์Šค์— ๊ฐ€์ƒ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ์†Œ๋ฉธ์ž๋„ ๊ฐ€์ƒํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ฐ€๋ น,

class Student : public Person{
    
}

Person p = new Student;

๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, Person์—์„œ virtualํ•จ์ˆ˜๊ฐ€ ์žˆ์–ด์„œ Student์—์„œ virtual์„ ์ •์˜ํ–ˆ๋‹ค๋ฉด Person์—์„œ ์†Œ๋ฉธ์ž๋„ virtual๋กœ ํ•ด์ฃผ์–ด์•ผ ๋‚˜์ค‘์— p๋ฅผ deleteํ•  ๋•Œ, Student๋ฅผ ๋จผ์ € deleteํ•˜๊ณ , Person์„ deleteํ•œ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด์„œ, ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋„ ์™„์ „ํžˆ ํ•ด์ œํ•˜๊ธฐ ์œ„ํ•œ ํ•˜๋‚˜์˜ ์Šคํ‚ฌ์ด๋‹ค.

21. overriding(์žฌ์ •์˜) vs. overloading(๋‹คํ˜•์„ฑ)

Overriding => ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ํŠน์„ฑ์ด ๋‹ค๋ฅผ ๋•Œ, ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ.(์ƒ์† ๊ธฐ๋ฐ˜)

Overloading => ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ ๊ฐ™๊ฒŒ!! ํ•˜๋ฉด์„œ ํ•จ์ˆ˜ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ธ์ž ๊ฐ’์„!! ๋‹ค๋ฅด๊ฒŒ ํ•ด์„œ ์œตํ†ต์„ฑ์„ ๋†’์ด๋Š” ๊ฒƒ. (ํ•˜๋‚˜์˜ ํด๋ž˜์Šค ๋‚ด์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ)

22. ๋ณต์‚ฌ ์ƒ์„ฑ์ž(copy constructor)

: ์ƒ์„ฑ์ž ์ค‘์— ๋ณธ์ธ ํด๋ž˜์Šค๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž. ์ด ๋•Œ, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ํด๋ž˜์Šค๋Š” const๋กœ ๋ฐ›๊ธฐ.

cf) ๊ทธ๋Ÿฐ๋ฐ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ฉค๋ฒ„ ๋Œ€ ๋ฉค๋ฒ„์˜ ๋ณต์‚ฌ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋ณต์‚ฌ ์ƒ์„ฑ์ž๊ฐ€ ์ž๋™์œผ๋กœ ์‚ฝ์ž…๋œ๋‹ค. ์ด๋ฅผ ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์ •์ž๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด๋Š” ์–•์€ ๋ณต์‚ฌ(shallow copy)๊ฐ€ ๋˜์–ด ์†Œ๋ฉธ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

cf) explicit๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œ ์ดˆ๊ธฐํ™” ๊ฐ’์„ ์ค„๋•Œ, '=' ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

// class A ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, public์—์„œ ๋ณต์‚ฌ ์ƒ์„ฑ์ž

A(const A &copy)
  : num(copy.num)
{
    
}

: ํ˜ธ์ถœ์‹œ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ๊ธฐ์กด์— ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ
  2. call-by-value ๋ฐฉ์‹์˜ ํ•จ์ˆ˜ํ˜ธ์ถœ ๊ณผ์ •์—์„œ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ
  3. ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋˜, ์ฐธ์กฐํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์ƒ์†๋ฐ›๋Š” ์ƒํ™ฉ์œผ๋กœ๋„ ํ™•์ธํ•ด๋ณด์ž

// class Student๊ฐ€ Person์„ ์ƒ์† ๋ฐ›๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, Student์˜ public์— ๋ณต์‚ฌ ์ƒ์„ฑ์ž
Student(char* myname, int myage, char* mymajor)
  : Person(myage, myname)     // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๊นŒ์ง€ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•  ์˜๋ฌด๊ฐ€ ์žˆ๋‹ค.
{
  strcpy(major, mymajor);
}
23. ์ƒ์†

: ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ƒ์„ฑ์€ 1)๋ถ€๋ชจ์ƒ์„ฑ์ž 2)์ž๋…€์ƒ์„ฑ์ž, ์†Œ๋ฉธ์€ 1)์ž๋…€์ƒ์„ฑ์ž 2)๋ถ€๋ชจ์ƒ์„ฑ์ž

cf) ์ƒ์†์„ ํ•˜๋Š” ๋‘๊ฐ€์ง€ ์ผ€์ด์Šค

  1. is-a : ์ž๋…€๋Š” ๋ถ€๋ชจ๋‹ค. ๋ผ๋Š” ๋ง์ด ์„ฑ๋ฆฝ๋ ๋•Œ ex) ์‚ฌ๊ณผ๋Š” ๊ณผ์ผ์ด๋‹ค.
  2. has-a : ์ž๋…€๋Š” ๋ถ€๋ชจ๋ฅผ ๊ฐ€์ง„๋‹ค. ๋ผ๋Š” ๋ง์ด ์„ฑ๋ฆฝ๋ ๋•Œ ex) ๊ฒฝ์ฐฐ์€ ์ด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

cf) ์ฐธ์กฐ์ž

: const int &ref = 50; ์€ ๊ฐ€๋Šฅ. ๋˜ํ•œ ์ž„์‹œ๋ณ€์ˆ˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ.

int Adder(const int &num1, const int &num2) return num1+num2;

int main(){
    cout << Adder(3,4) << endl;
}

cf) ์–ด๋ ค์› ๋˜๊ฑฐ : return ํƒ€์ž…์ด A&๊ณผ *this ๊ฐ™์€์ด์œ 

class A
{
private:
    int num;
public:
    A& add(int n){
        num+=n;
		return *this;
    }
}
24. ๊ฐ€์ƒํ•จ์ˆ˜(virtual function)

[๋จผ์ € ์•Œ์•„์•ผํ•  ๊ฒƒ]

  1. ๊ฐ์ฒด ํฌ์ธํ„ฐ ๋ณ€์ˆ˜

    Person *ptr;

    : Personํ˜• ํฌ์ธํ„ฐ ptr์€ Person ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, Person์„ ์ƒ์†ํ•˜๋Š” ์œ ๋„ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํฌ์ธํ„ฐ ์—ฐ์‚ฐ ๊ฐ€๋Šฅ์„ฑ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ๋•Œ, ํฌ์ธํ„ฐ์˜ ์ž๋ฃŒํ˜•์„ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค. ๊ฐ€๋ น,

    int main(){
        Base* bptr = new Derived();   // compile OK
        Derived* dptr = bptr;         // compile X
    }

    : ๋‘๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์ปดํŒŒ์ผ๋˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” dptr์ด Derived๊ฐ์ฒด์™€ Derived๋ฅผ ์ƒ์†ํ•˜๋Š” ์ž๋…€ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ bptr์€ Baseํ˜• ํฌ์ธํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— Base๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ์ด๋Š” ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค. ๋‹ค์Œ์˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.

    int main(){
        Derived* dptr = new Derived();   // compile OK
        Base* bptr = dptr;               // compile OK
    }

    : ํฌ์ธํ„ฐ ์—ฐ์‚ฐ์˜ ๊ฐ€๋Šฅ์„ฑ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ๋•Œ, ํฌ์ธํ„ฐ์˜ ์ž๋ฃŒํ˜•์„ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จํ•˜์ง€, ์‹ค์ œ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด์˜ ์ž๋ฃŒํ˜•์„ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จํ•˜์ง€ ์•Š๋Š”๋‹ค.

    int main(void)
    {
        Base *bptr = new Derived();     // compile OK
        bptr->DerivedFunc();            // compile Error
    }
  2. ๊ณ„์ธต๊ตฌ์กฐ์ƒ ๋งจ ์œ„์— ์กด์žฌํ•˜๋Š” ๊ธฐ์ดˆ ํด๋ž˜์Šค์˜ ๊ฐ€์ƒํ•จ์ˆ˜ ํ˜น์€ ์†Œ๋ฉธ์ž๋งŒ virtual๋กœ ์„ ์–ธํ•˜๋ฉด ์ด๋ฅผ ์ƒ์†ํ•˜๋Š” ์ž๋…€ ํด๋ž˜์Šค์˜ ๊ฐ€์ƒํ•จ์ˆ˜๋‚˜ ์†Œ๋ฉธ์ž๋“ค๋„ ๋ชจ๋‘ virtual๋กœ ์„ ์–ธ๋œ๋‹ค. (์ด ๋•Œ, ์ƒ์† ๋ฐ›๋Š” ํด๋ž˜์Šค์—์„œ overridingํ•  ๋•Œ, virtual์„ ์จ์ค˜๋„ ๋˜๊ณ  ์•ˆ ์จ์ค˜๋„ ๋˜๋‚˜, ์จ์ฃผ๋Š” ๊ฒƒ์ด ๋” ๋‚ซ๋‹ค.) ์†Œ๋ฉธ์ž์˜ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ ๋ฌด์กฐ๊ฑด virtual๋กœ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž. ์™œ๋ƒํ•˜๋ฉด ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋ฐœ์ƒ๋˜๋Š”๋ฐ.

    Base *b = new Derived();
    delete b;  

    ์ด๋ ‡๊ฒŒ๋˜๋ฉด Base์˜ ์†Œ๋ฉธ์ž๋งŒ ๋ถˆ๋Ÿฌ์ ธ Derived๊ฐ€ ์ œ๋Œ€๋กœ ์†Œ๋ฉธ๋˜์ง€ ์•Š์•„ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์†Œ๋ฉธ์ž๋Š” virtual๋กœ ์„ ์–ธํ•ด์ฃผ์ž.

    class First{
    public:
        First(){}
        virtual ~First(){}
    }
    
    class Second: public First{
    public:
        Second():Fist(){}
        virtual ~Second(){}
    }
    
    class Third: public Second{
    public:
        Third():Second(){}
        virtual ~Third(){}
    }
    

    ์ด๋ ‡๊ฒŒ ๋˜๋ฉด Third์˜ ์†Œ๋ฉธ์ž๊ฐ€ ๋ถˆ๋ฆฌ๊ณ , Second์˜ ์†Œ๋ฉธ์ž๊ฐ€ ๋ถˆ๋ฆฌ๊ณ , First์˜ ์†Œ๋ฉธ์ž๊ฐ€ ์ฐจ๋ก€๋กœ ๋ถˆ๋ ค ๋ชจ๋‘ ์†Œ๋ฉธ๋œ๋‹ค.

25. public, protected, private

: ์ƒ์†์„ ๋ฐ›์„ ๋•Œ ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์ ‘๊ทผ์ œํ•œ์˜ ์ฐจ์ด

์™ธ๋ถ€์ ‘๊ทผ ์ž๋…€ํด๋ž˜์Šค
public o o
protected x o
private x x

๊ทธ๋ฆฌ๊ณ  ์ƒ์†๋ฐ›์„ ๋•Œ : ๋’ค์— ๋ถ™๋Š” ๊ฑฐ๋Š”

class Derived : public Base{
    
}  // => ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ›์•„๋“ค์ด๊ธฐ

class Derived : protected Base{
    
}  // => ๊ฐ€์žฅ ๋‚ฎ์€ ์ˆ˜์ค€์˜ ์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ protected์ž„. ์ฆ‰, Base์˜ public๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ protected๊ฐ€ ๋˜๊ณ  ๊ธฐ์กด์˜ protected๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ๊ทธ๋Œ€๋กœ protected์ด๊ณ , ๊ธฐ์กด์˜ private๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋„ ๊ทธ๋Œ€๋กœ private์ด๋‹ค.

class Derived : private Base{
    
}  // => ๊ฐ€์žฅ ๋‚ฎ์€ ์ˆ˜์ค€์˜ ์ ‘๊ทผ์ œํ•œ๊ฐ€๊ฐ€ private์ž„. ์ฆ‰, Base์˜ public๋ฉค๋ฒ„๊ฐ€ ๋ณ€์ˆ˜๊ฐ€ private์ด ๋˜๊ณ protected๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋„ private์ด ๋˜๊ณ  ๊ธฐ์กด์˜ private๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ๊ทธ๋Œ€๋กœ private์ด ๋œ๋‹ค.
26. -> ์™€ . ์˜ ์ฐจ์ด

: A->Func()์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด A๋Š” ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์ด๋‹ค. ๋.

27. ์ •์  ๋ฐ”์ธ๋”ฉ
class Base{
public:
    void Print(){ 
        cout << "Base" endl;
    }
}

class Derived{
public:
    void Print(){
        cout << "Derived" << endl;
    }
}

int main(){
    Base b;
    Derived d;
    d.Print();  // ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Derived์— ์žˆ๋Š” Printํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
    d.b::Print(); // ๋ฌผ๋ก  ๋ถ€๋ชจ class์˜ ํ•จ์ˆ˜์—๋„ ์ด๋Ÿฐ์‹์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    
    Base* bptr = new Derived();
    bptr->Print();  // ์ด ๋•Œ, bptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋Š” Derived์ด์ง€๋งŒ, Base์˜ Print๋ฅผ ํ˜ธ์ถœํ•จ.
                    // bptr์˜ ํƒ€์ž…์€ ๋ฐ”๋กœ ์•Œ์ˆ˜๊ฐ€ ์žˆ๋‹ค. Base์— ์žˆ๋Š” Print๋ฅผ ํ˜ธ์ถœํ•˜์ž. ์ด๊ฒƒ์ด
    				// ์ •์  ๋ฐ”์ธ๋”ฉ.(๋ฐ”์ธ๋”ฉ : bptr์ด ์–ด๋–ค๊ฑธ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ์ธ์ง€์— ๋”ฐ๋ผ ์–ด๋–ค Print
    				// ๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š”๊ฐ€ ํ•˜๋Š” ๊ฒƒ) (์ •์  : ํฌ์ธํ„ฐ์˜ ํƒ€์ž…์— ํ•ด๋‹นํ•˜๋Š” Printํ˜ธ์ถœ)
}
28. typedef ์„ ์–ธ(C์–ธ์–ด ๊ตฌ์กฐ์ฒด)

: ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์ž๋ฃŒํ˜•์˜ ์ด๋ฆ„์— ์ƒˆ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ์„ ์–ธ์ด๋‹ค.

typedef int INT;  // int์˜ ๋˜ ๋‹ค๋ฅธ ์ด๋ฆ„ INT๋ฅผ ๋ถ€์—ฌ
typedef struct point Point;  // struct point์— Point๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ€์—ฌ

// 1)
typedef struct point{
    int a;
    int b;
} Point;

// 2)
struct point{
    int a;
    int b;
} typedef struct point Point;

cf) virtual function์„ overridingํ•˜์ง€ ์•Š์œผ๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€? => ๋ถ€๋ชจ๊บผ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋จ. ๋ฌธ์ œ ์—†์Œ.

29. ๋‹ค์ค‘ ์ƒ์†(Multiple Inheritance)

: C++์—์„œ๋Š” ๋“๋ณด๋‹ค ์‹ค์ด ๋” ๋งŽ์€ ๋ฌธ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ž๋ฐ”์—์„œ๋Š” ์™œ ๋‹ค์ค‘ ์ƒ์†์„?? ํ–ˆ๋˜๊ฑฐ ๊ฐ™์€๋ฐ. ์–ด๋А ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋กœ ์ ‘๊ทผํ•ด์•ผํ• ์ง€ ๋ชจํ˜ธ์„ฑ์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋˜๋„๋ก ์‚ฌ์šฉ X.

30. ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ
30-1) operator+, operator-, operator์—ฐ์‚ฐ์ž

: ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐ๋ง๊ณ , ๊ทธ๋ƒฅ ์ง๊ด€์ ์œผ๋กœ ๋ฐ›์•„๋“ค์—ฌ๋ณด์ž.

class Car{
    Car(){}
    ~Car(){}
    Car operator+(const Car &ref){ 
        return Carํƒ€์ž…;
    }
    Car operator-(const Car &ref){
        return Carํƒ€์ž…;
    }
}

Car A;
Car B;

A+B๋ฅผ ํ•˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ A.operator+(B)๋ผ๊ณ  ๋ฐ›์•„๋“ค์ด๊ฒ ๋‹ค.

A-B๋ฅผ ํ•˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ A.operator-(B)๋ผ๊ณ  ๋ฐ›์•„๋“ค์ด๊ฒ ๋‹ค.

cf) operator์˜ ์—ญํ• ์ด ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฑฐ๋ผ๋ฉด const๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

30-2)

cf) ๊ณ„์† ๋‚˜์˜ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

: A๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ๋•Œ, A&์™€ *this์˜ ํƒ€์ž…์€ ๊ฐ™๋‹ค.

cf) constํ•จ์ˆ˜

class Car{
private:
    int num;
public:
    void print() const {
        ;
    }
}

Car A;
const Car B;

์ด๋Ÿฐ ์ƒํ™ฉ์ด๋ผ๋ฉด Carํด๋ž˜์Šค ํƒ€์ž…์˜ ๊ฐ์ฒด ์ค‘์— B๊ฐ€ printํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅผ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

cf) friend์„ ์–ธ

: Aํด๋ž˜์Šค๊ฐ€ Bํด๋ž˜์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ friend ์„ ์–ธ์„ ํ•˜๋ฉด, Bํด๋ž˜์Šค๋Š” Aํด๋ž˜์Šค์˜ private๋ฉค๋ฒ„์— ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹จ, Aํด๋ž˜์Šค๋„ Bํด๋ž˜์Šค์˜ private๋ฉค๋ฒ„์— ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด, Bํด๋ž˜์Šค๊ฐ€ Aํด๋ž˜์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ friend ์„ ์–ธ์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.

Q. ์ƒ์†๋ฐ›์€ ๊ด€๊ณ„์˜ ๊ฐ์ฒด๋“ค์ด ์žˆ์„ ๋•Œ, ์ตœํ•˜์œ„ ํด๋ž˜์Šค๋งŒ deleteํ•˜๋Š” ๊ฒƒ? virtual๋„ ์•„๋‹Œ๋ฐ? p.345
Q. ๋ฆฌํ„ด ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์— {}์•ž์— const๋ฅผ ๋ถ™์ด๋Š” ๊ฑด ์–ด๋–ค ํšจ๊ณผ๊ฐ€ ์žˆ๋Š” ๊ฒƒ? p.402

[ ์–ด๋””์„œ๋“  ์ฝ”๋”ฉํ•˜์ž. ์•„ํ†ฐ ์„ธํŒ…]

1. Install MinGW/MSYS(32bit version recommended)

  • Follow this good installation guide available. For mac, good installation guide

  • After the installation, change (add) the following two folder names into user's PATH environment variable:

    C:\MinGW\bin
    C:\MinGW\msys\1.0\bin
    
  • To check your installation, do the following in cmd windows or in PowerShell;

    g++ --version
    
  • If you encounter the following error message during the first compilation of your program such as 'HelloWorld.cpp', Refer to this site.

    c:/mingw/bin/../lib/g++/mingw3 ........ mingw32/bin/ld.exe: cannot find -lpthread
    

2. Install Atom.

  1. Atom is a text editor that most professional programmers love nowadays.
  2. Start Atom.
  3. Install some of essential packages recommended for C/C++ programmers listed below:
    • Autosave
      • It automatically saves files when the editors loses focus, are destroyed, or when the window is closed. Believe or not, it is disabled by default. You must check enabled in config setting or from the Autosave section of the Settings view.
    • Platformio-ide-terminal
      • On successful installation, you will see a +sign at the bottom-left corner of the editor. Click + sign to open a console.
    • File-icons
    • Mini-maps
    • Markdown-preview
      • Open a rendered version of the Markdown in the current editor with ctrl-shift-m.
    • language-c
      • Adds syntax highlighting and snippets to C/C++ files
    • gpp-compiler
      • Allows you to compile and run C++ and C within the editor.
      • To compile C or C++, press F5 or right click the file in tree view and click Compile and Run.
      • To compile C or C++ and attach the GNU Debugger, press F6 or right click the file in tree view and click Compile and Debug.

[ ์ฝ”๋”ฉ SKILL ]

1. Testing ํ•  ๋•Œ ์œ ์šฉํ•œ ๊ฒƒ
#if 1      
#endif

#if 0
#endif
1-1.
#ifndef NOWIC_H     // .hํŒŒ์ผ์„ gaurd(์ค‘๋ณต include๋ฅผ ๋ฐฉ์ง€)ํ•ด์ฃผ๋Š” ๊ฒƒ.
#define NOWIC_H
1-1-2.
#ifdef DEBUG    // ์ปดํŒŒ์ผํ•  ๋•Œ, ๋””๋ฒ„๊ทธ๋ฅผ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.
#define DPRINT(func) func;   // ()์•ˆ์— ์žˆ๋Š” ๊ฒƒ์ด func๊ฐ€ ๋œ๋‹ค.
#else
#define DPRINT(func) ;      // debug๊ฐ€ ์—†์œผ๋ฉด ()์•ˆ์— ์žˆ๋Š” ๊ฒƒ์ด null์ด ๋œ๋‹ค.
#endif
1-3. assert

: (1) mallocํ•˜๊ณ ๋Š” assertํ•˜๊ธฐ (2) freeํ•ด์•ผํ•จ

char *ptr = new (nothrow) char[5];     // nothrow๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌX ๋ผ๋Š”๋ง
assert(ptr != nullptr);                // ()์ด์–ด์•ผ ํ•จ์„ ์ฃผ์žฅ. ()๊ฐ€ ์•„๋‹ˆ๋ฉด Message!
2. RANDOM(๋žœ๋ค)์˜ ๋ชจ๋“  ๊ฒƒ
1. time()
    => #include <ctime>์ด ํ•„์š”ํ•˜๋ฉฐ, ???

2. rand()
    =>
    
3. srand()
    =>
    
    
4. ๋žœ๋ค ์‹œ๋“œ ๋„˜๋ฒ„
#include <ctime> 
srand((unsigned int)time(NULL));
3. DPRINT

: ๋•Œ๋–„๋กœ debugging์„ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด, ์ž ์‹œ(์ž„์‹œ๋กœ) ๋„ฃ์–ด๋‘” ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.

์ปดํŒŒ์ผ ํ•  ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด -DDEBUG ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ”„๋ฆฐํŠธ ๋ฉ๋‹ˆ๋‹ค. ์•„์ง debugger ๊ฐ€ ๋ฐฐ์šฐ๊ธฐ ์ „๊นŒ์ง€ ๋ณต์žกํ•œ source code ์–ด๋””์—์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋Š”์ง€ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์ฃ . ๋””๋ฒ„๊น…์ด ๋๋‚œ ๋‹ค์Œ์—๋Š” ๋ฌผ๋ก  -DDEBUG์—†์ด ์ปดํŒŒ์ผํ•˜๋ฉด, ํ•ด๋‹น source code๋Š” ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์ด ์—†์Šต๋‹ˆ๋‹ค.

g++  -DDEBUG 
4. ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ
4-1. cin ๋ณด๋‹ค getline์„ ํ™œ์šฉํ•˜๋ผ!

์ด์œ ) cin์€ ์ŠคํŠธ๋ฆผ์—์„œ newline๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜, ํƒ€์ž…๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.(???) ๋˜ํ•œ, ๋›ฐ์–ด์“ฐ๊ธฐ ๊ฐ€ ๋  ๋•Œ ๋ฉˆ์ถ”์–ด ์ฒซ ๋‹จ์–ด๋งŒ ์ž…๋ ฅ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ getline์„.

getline(istream& is, string& str, char delim)
	=> (cin, ๋ฐ›์€ string ๋„ฃ์„ ๋ณ€์ˆ˜, tokenizeํ•  ๋ฌธ์ž์ธ๋ฐ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋จ)
#include <iostream>
#include <string>

using namespace std;

int main(){
  string last;
  string first;

  cout << "Enter your last name : ";
  cin >> last;  // cin ์œผ๋กœ๋Š” ํ•œ '๋‹จ์–ด'๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

  cout << "Enter your first name : ";
  cin.ignore(); // cin์„ ๋˜ ๋‹ค๋ฅธ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด flushํ•ด์ฃผ์–ด์•ผ ๋œ๋‹ค.
  getline(cin, first);  // getline์œผ๋กœ ํ•œ '๋ฌธ์žฅ'์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

  cout << last <<  " " << first << "! Welcome!" << endl;

  return 0;

}

[์ฐธ๊ณ ] getline์œผ๋กœ tokenize

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string line = "God is good all the time.";
  vector <string> tokens; // Vector of string to save tokens

  stringstream ss(line);   // stringstream object ss
  string word;
  while(getline(ss, word, ' '))  // Tokenize w.r.t. space ' '
      tokens.push_back(word);

  for(int i = 0; i < tokens.size(); i++) // print the tokens
      cout << tokens[i] << '\n';
}

[์ฐธ๊ณ ] string ๋ถ€๋ถ„ ์‚ฌ์šฉ

string str="abcdefghijklmnop";
str.substr(startIndex, many);

ex)
    str.substr(2,4) ๋ผ๋ฉด, "cdef"
[์ฐธ๊ณ ] string ์ž…๋ ฅ ๋ฐ›์•„์„œ ๋„์–ด์“ฐ๊ธฐ๋กœ tokenize(split)ํ•˜๊ณ  ๋ฒกํ„ฐ์— ๋„ฃ๊ธฐ
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {

  string line;
  vector <string> tokens; // Vector of string to save tokens
  vector <int> v;

  getline(cin, line);
  stringstream ss(line);   // stringstream object ss
  string word;

  while(getline(ss, word, ' '))  // Tokenize w.r.t. space ' '
      tokens.push_back(word);

  int i=0;
  while(tokens[i] != "q"){
    v.push_back(stoi(tokens.at(i)));
    cout << v[i] << endl;
    i++;
  }
}
4-2. ํƒ€์ž… ๋ณ€ํ™˜

[char => ์ •์ˆ˜] atoi // ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑด professional ๋ ˆ๋ฒจ์—์„œ๋Š” ๊ฑฐ์˜ ์•ˆ์“ฐ๊ณ  strtol ์„ ์“ด๋‹ค!!(string to long)

#include <iostream>
using namespace std;

int main() {
  char ac[20] = "012345";
  cout << ac[5];   // 5
}

[string => ์ •์ˆ˜] stoi

string str = "1234567789";
stoi(str);   // ๋ฌธ์ž์—ด "123456778" ์ด ์ •์ˆ˜ 123456778 ๋กœ
5. Function Pointer
6. for๋ฌธ ํด๋ผ์Šค ๋†’์ด๊ธฐ
  1. ์ผ๋ฐ˜
for(int i=0; i<n; i++){
    cout << v[i] << " ";
}
  1. ๋ชจ๋“  ์ธ๋ฑ์Šค ์ ‘๊ทผ์‹œ ํ™œ์šฉ
for(int x : v){
    cout << x << " ";
}
  1. ํƒ€์ž…์— ์œตํ†ต์„ฑ์„
for(auto x : v){
	cout << x << " ";
}
  1. iterator๋กœ ์ ‘๊ทผํ•˜๊ธฐ(์ด๊ฑธ ์“ฐ๋Š” ์ด์œ ๋Š” stl์—์„œ iterator๋งŒ ๋ฐ›๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ์จ์•ผ๋œ๋‹ค)
vector<int>::iterator it; // it์€ pointer๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ
for(it=v.begin(); it!=v.end(); it++){
    cout << *it << " ";
}

cf) ์ž!! ์ด๊ฒŒ ์˜ˆ์‹œ๋‹ค.

vector<int> v{3, 7, 9, 11, 8};
void foo(int *arr, int n) {
vector<int> v(arr,arr + n);
vector<int> w{0, 1};
	...
	// make w = [ w ] + [ v ] or push back w to w
	w.insert(w.end(), arr, arr + n);  //์—ฌ๊ธฐ์„œ iterator w.end()๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด์ฉ”์ˆ˜ ์—†์ด iterator๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค.
}
  1. ๊ทธ๋Ÿฐ๋ฐ ์ด๋งˆ์ €๋„ auto๊ฐ€ ๋œ๋‹ค.
// it์€ pointer๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ
for(auto it =v.begin(); it!=v.end(); it++){
    cout << *it << " ";
}
  1. first class object๋„ ์—„์ฒญ ์“ด๋‹ค.
int arr[] = { 7, 4, 3, 2 }; // standard C array
vector<int> v(arr, arr+4); // initialize vector with C array
for_each(v.begin(), v.end(), show_sqr ); // apply function show
  1. find_if
#include <vector>
#include <algorithm>
#include <iostream>
bool mytest(int n) { return (n > 2) && (n < 7); };
int main(){
    int arr[] = { 2, 3, 7, 8, 4, 6, 9 }; // standard C array
vector<int> v(arr, arr+7); // initialize vector with C array
auto iter = find_if (v.begin(), v.end(), mytest);
if(iter != v.end){
cout << "found " << *iter <<
else
cout << "not found" <<
}
}

์ด๊ฑฐ ๋ง๊ณ ๋„ find_end, count_if์ญ‰์ญ‰ ๋‹ค ์“ธ์ค„ ์•Œ์•„์•ผ๋œ๋‹ค.

STL

< vector >

๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ธฐ๋Šฅ
v.size() v์˜ ์‚ฌ์ด์ฆˆ(๋ฌผ๋ฆฌ์ ์ธ ์ €์žฅ ์šฉ๋Ÿ‰์ด ์•„๋‹Œ ์›์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ๋ฆฌํ„ด
v.resize(new_size) v๋ฅผ new_size๋กœ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ”๊ฟ”์คŒ
v.empty() v์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธ
v.begin() v์˜ 0๋ฒˆ์งธ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” iterator3 ๋ฆฌํ„ด
v.end() v์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” iterator ๋ฆฌํ„ด
v.front() v์˜ 0๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฆฌํ„ด
v.back() v์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๋ฆฌํ„ด
v.push_back(val) v์˜ ๋์— val์„ ์ถ”๊ฐ€
v.pop_back() v์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ์‚ญ์ œ
v.clear() v์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ์‚ญ์ œ

cf 1) ์— ์žˆ๋Š” **reverse()**๋ฅผ ์ด์šฉํ•˜๋ฉด, vector์— ์†ํ•œ ์›์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์›๋ž˜์˜ vector๊ฐ€ 1 5 2 3 ์˜€๋‹ค๋ฉด reverse()ํ•œ ๊ฒฐ๊ณผ๋Š” 3 2 5 1 ์ด ๋ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ๋ฒ• : reverse(v.begin(), v.end());

cf 2) ์— ์žˆ๋Š” **sort()**๋ฅผ ์ด์šฉํ•˜๋ฉด, vector์— ์†ํ•œ ์›์†Œ๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์›๋ž˜์˜ vector๊ฐ€ 1 5 3 4 ์˜€๋‹ค๋ฉด sort()ํ•œ ๊ฒฐ๊ณผ๋Š” 1 3 4 5 ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ๋ฒ• : sort(v.begin(), v.end());

๋ง) ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ๋„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, int๋กœ ์ด๋ฃจ์–ด์ง„ vector๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ์ด๋ฃจ์–ด์ง„ vector๋„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

< queue >

๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ธฐ๋Šฅ
q.size() q์˜ ์‚ฌ์ด์ฆˆ(๋ฌผ๋ฆฌ์ ์ธ ์ €์žฅ ์šฉ๋Ÿ‰์ด ์•„๋‹Œ ์›์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ๋ฆฌํ„ด
q.empty() q์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธ
q.front() q์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด๊ฐ„ ์›์†Œ๋ฅผ ๋ฆฌํ„ด
q.back() q์— ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„ ์›์†Œ๋ฅผ ๋ฆฌํ„ด
q.push(val) q์˜ ์œ„(๋’ค์— val ์ถ”๊ฐ€
q.pop() q์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด๊ฐ„ ์›์†Œ๋ฅผ ์‚ญ์ œ

swap์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์ฃผ ์“ฐ์ด์ง€๋Š” ์•Š์ง€๋งŒ, vector์™€๋Š” ๋‹ฌ๋ฆฌ queue๋Š” clear() ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ์—†์œผ๋ฏ€๋กœ swap์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด clear() ํ•˜๊ณ ์ž ํ•˜๋Š” queue๋ฅผ ๋นˆ queue์™€ swapํ•ด์คŒ์œผ๋กœ์จ clear()์™€ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ swap์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์›์†Œ๋ฅผ pop()ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

< stack >

๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ธฐ๋Šฅ
s.size() s์˜ ์‚ฌ์ด์ฆˆ(๋ฌผ๋ฆฌ์ ์ธ ์ €์žฅ ์šฉ๋Ÿ‰์ด ์•„๋‹Œ ์›์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ๋ฆฌํ„ด
s.empty() s์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธ
s.top() s์— ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„ ์›์†Œ๋ฅผ ๋ฆฌํ„ด
s.push(val) s์˜ ๋’ค์— val ์ถ”๊ฐ€
s.pop() s์— ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„ ์›์†Œ๋ฅผ ์‚ญ์ œ

< utility >

: pair๋ž€, ์ด๋ฆ„์ด 'first', 'second'์ธ ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” struct

//pair ์„ ์–ธ
pair<int,int> p;
pair<char,double> p;
 
//pair ์ƒ์„ฑ
int a = 1, b = 2;
pair<int,int> p = make_pair(a,b);
pair<int,int> p = make_pair(1,2);
 
//pair์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ ‘๊ทผ
int valA = p.first;
int valB = p.second;

์ถœ์ฒ˜: https://sarah950716.tistory.com/5 [์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž์˜ ๋Œ€๋‚˜๋ฌด์ˆฒ]

[ ๊ธฐ์ €๊นŒ์ง€ ]

1. ์šฉ์–ด๋“ค
  1. ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜ : ์ถ”์ฒœํ•˜๋Š” ํ‘œ์ค€์˜ ์ฝ”๋”ฉ ์Šคํƒ€์ผ

  2. linter : ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜๊ณผ ์—๋Ÿฌ์ฒดํฌ๋ฅผ ๋„์™€์ฃผ๋Š” ํˆด

  3. ์ •์ ๋ถ„์„ : ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ.

  4. ๋™์ ๋ถ„์„ : ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•ด๋ณด๊ณ  ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ.

โ€‹

[ ์ฐพ์•„๋ณผ๊ฒƒ ]

  1. ์•„ํ†ฐ ์—๋Ÿฌ ์‰ฝ๊ฒŒ ๋ณด๊ธฐ.. (compiling error txt๋ผ๋„..), linter ํŒจํ‚ค์ง€ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•˜๋Š”๋ฐ... ๋ญ์ง€...

  2. fprint, sprintf

  3. setvbuf

  4. ??? ํ•ด๊ฒฐํ•˜๊ธฐ

  5. ์ดํ•ด๋ถˆ๊ฐ€ => ss๋Š” ๋ฌด์—‡์ธ๊ฐ€

    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main() {
      string input = "";
      cout << "Enter a sentence(with spaces):\n>";
      getline(cin, input);
      cout << "You entered: " << input << endl << endl;
    
      int num = 0;
      while (true) {
        cout << "Please enter a number: ";
        getline(cin, input);
        // This code convert from string to number safely.
        stringstream ss(input);
        if (ss >> num) break;
        cout << "Invalid number, please try again" << endl;
      }
      cout << "You entered: " << num << endl << endl;
    }

[ ์‹œํ—˜ ]

1. struct // ๋ชจ๋‘ public์ž„. // class์˜ ๊ฒฝ์šฐ private(default), public์ด ์žˆ์Œ.
struct Car {
	string model;
	double speed;
};

Car* Cset = new Car[3];

Cset[i].model = Abante;   // Cset[i]->model ์•„๋‹˜
for (int i = 0; i < NCARS; i++, Cset++) {
		cout << "\t(" << i + 1 << ")" << Cset->model << "\t" << Cset->speed << endl;
	}  // ์ด๋Ÿด๋•Œ -> ์“ฐ๋Š”๊ฑฐ์ž„.
2. inorder, preorder, postorder, levelorder

(1) inorder

: LVR์ถœ๋ ฅ ์ˆœ์„œ์ด๋‹ค. L๋ถ€ํ„ฐ์ด๋‹ˆ ๋งจ ์™ผ์ชฝ์œผ๋กœ ์ด๋™. ๋งจ ์™ผ์ชฝ ๋…ธ๋“œ ๋„์ฐฉํ•˜๋ฉด LVR์ˆœ์„œ๋กœ ์ถœ๋ ฅ. R์€ ๊ฐ€์ง€๊ฐ€ ๋” ์žˆ๋‹ค๋ฉด ๊ทธ ์•ˆ์—์„œ LVR์ˆœ์„œ๋กœ ์ถœ๋ ฅ.

(2) preorder

: VLR์ถœ๋ ฅ ์ˆœ์„œ์ด๋‹ค. V๋ถ€ํ„ฐ์ด๋‹ˆ ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์ถœ๋ ฅํ•˜๊ณ  ์‹œ์ž‘ํ•œ๋‹ค. ๋‹ค์Œ์€ L์ด๋‹ˆ ๋งˆ์ง€๋ง‰ L๋„์ฐฉ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ฉด์„œ ์ด๋™. ๋งจ ์™ผ์ชฝ ๋…ธ๋“œ ๋„์ฐฉํ•˜๋ฉด VL์ถœ๋ ฅํ•œ ์ƒํƒœ์ด๋‹ˆ R์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ธ๋ฐ, ๋งˆ์น˜ ๋ฏธ๋กœ์—์„œ ํ•œ์ชฝ ๋ฒฝ์„ ์žก๊ณ  ์ญ‰ ๊ฐ€๋Š” ์‹์ด๋‹ค.

(3) postorder

: LRV์ถœ๋ ฅ ์ˆœ์„œ์ด๋‹ค. L๋ถ€ํ„ฐ์ด๋‹ˆ ๋งจ ์™ผ์ชฝ์œผ๋กœ ์ด๋™. ๋งจ ์™ผ์ชฝ ๋…ธ๋“œ ๋„์ฐฉํ•˜๋ฉด LRV์ˆœ์„œ๋กœ ์ถœ๋ ฅ.

(4) levelorder

: level ๋‹จ์œ„ ์ถœ๋ ฅ์ด๋‹ค. 1level์ถœ๋ ฅ -> 2level์ถœ๋ ฅ -> 3level์ถœ๋ ฅ -> ...