- ๊ต์๋ ์ฝ๋๋ฅผ ๋ฐ์์ Headerํ์ผ๋ง ๋จ๊ธฐ๊ณ ๋๋จธ์ง ๋ชจ๋ ์ฝ๋๋ฅผ ์ง์ ์ฝ๋ฉํ ์ ์๋๋ก ํ๊ธฐ!
- ๊ธฐ์กด ์์ธ์ฒ๋ฆฌ ์ฝ๋ ๋ถ์ => try & catch
- ํน์ ํด๋์ค ์์ธ์ฒ๋ฆฌ ์ฝ๋ ์ถ๊ฐ, ์ฝ๋ฉํธ ์ฐ๊ธฐ, PPT ๋ง๋ค๊ธฐ
- Clone(Deep copy, Shallow Copy) ์ถ๊ฐ
- Distribution learning์ฝ๋ ์ง๊ธฐ (๋ณ๋ ฌ์ฒ๋ฆฌ)
- Master Slave ๋ชจ๋ธ
์ง๋๊ต์ ํ์ ๊ตฌ์ฑ ํ๋ก์ ํธ ์ฃผ์ ์ฌ์ฉํ ๊ธฐ์ ํ์ฌ๊น์ง ์ง์ฒ๋ ํฅํ ๊ณํ ํ์๊ณผ์ ๋ถ๋ฐฐ
๊น์ธ์ค ์ผ์ฌ๋์ฒด WICWIU(๋ฅ๋ฌ๋ ์คํ์์ค ํ๋ ์์ํฌ) Exception Handling ๋ฅ๋ฌ๋๊ณผ C++
์ฝ๋ ๋ถ์ ์ค, try&catch ์ถ๊ฐ ์ผ์ฌ๋์ฒด
#ifdef TEST // TEST๊ฐ #define์ผ๋ก ์ ์๋์ด ์์ผ๋ฉด ์คํ
#endif
#ifndef TEST // TEST๊ฐ ์ ์ ์๋์ด ์์ผ๋ฉด ์คํ
#endif๋จ์ํ ๋ณ์ ํ๋๋ฅผ ์ซ์์ฒ๋ฆฌ ํด์ฃผ๋๊ฒ ๋ค๊ฐ ์๋๋ค.#include <> // ์์คํ
๋๋ ํ ๋ฆฌ์์ include
#include "" // ํ์ฌ ๋๋ ํ ๋ฆฌ์์ includecf) ์ฐธ๊ณ ๋ก mainํจ์๋ iostream์ ์๋ค.
: headerํ์ผ์์ ์ ์ธ๊ณผ ๋์์ ๊ตฌํํด๋๋ ๊ฒ.
: ์ฐ์ ํค๋ํ์ผ์ด๋ function prototype(ํจ์๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํด๋๋ ๊ฒ)์ ๋ชจ์๋ ๊ฒ์ด๋ค. ์ฌ์ฉํ๋ ์ด์ ๋ ์ฌํ์ฉ์ ์ํด์.
int a = 3;
int &k = a; // k๋ a๋ ์์ ๋๊ฐ์์ง๋ ๊ฑฐ์
int &k = 3; // ์ด๊ฑฐ๋ ์๋๋ค. ๋๋ค rvalue(๋ฉ๋ชจ๋ฆฌ์ฃผ์๊ฐ ์๋๊ฒ)๋ผ์../*
.cppํ์ผ =>(์ปดํ์ผ)=> .oํ์ผ =>(link)=> .exeํ์ผ
*/
// ๊ฐ๋ น, ํ์์์์
=> g++ -c hello.cpp
// ํ๋ฉด compile๋ง ํ๋ ๊ฒ์ผ๋ก .oํ์ผ ์์ฑ๋จ=> ํน์ ํ ์ํ ์งํฉ์ ๋ํ๋ด์ผ ํ ๋ ์ฝ๋ ๋ฌธ์ํ ๋ฐ ๊ฐ๋ ์ฑ ๋ชฉ์ ์ผ๋ก ๋งค์ฐ ์ ์ฉํ๋ค.
enum DeepLearning{
CNN, // ์์๋๋ก ์ ์๊ฐ์ด ํ ๋น๋๋ค.(default 0)
RNN,
GAN,
}; // ์ด ์ํ๋ ๋ฉ๋ชจ๋ฆฌํ ๋นX
DeepLearning d1 = CNN; // ๋ฉ๋ชจ๋ฆฌํ ๋น: ์ค๋ช ์์ด ๋ฑ์ฅํ๋ ์์
ex) ํ์์ ํ์ ์ํ๋ฅผ "1์ด๋ฉด ์ฌํ, 2์ด๋ฉด ํดํ, 3์ด๋ฉด ์ ์ , 4์ด๋ฉด ์กธ์ "์ผ๋ก ๋ํ๋ธ๋ค๊ณ ๊ฐ์ ํ์. ์ด ๊ฒฝ์ฐ, "์ด ํ์์ด ์กธ์ ํ๋๊ฐ"๋ฅผ ๋ฐ์ง๊ธฐ ์ํด "ํ์.์ํ == 1" ๋ฐ์๋ก ๋ํ๋ผ ๊ฒฝ์ฐ์ 1,2,3 ์ด ๋งค์ง๋๋ฒ์ ํด๋น๋๋ค.
๊ทธ๋์ 7.์ enum์ ์ฌ์ฉํด์ฃผ๋ ๊ฒ์ด ๊ฐ๋ ์ฑ์ ๋์ด๋๋ฐ ์ข๋ค.
int main(int argc, char *argv[]){}
// argc : ์ฝ์ ์ฐฝ์ ์
๋ ฅ๋๋ ๋ฌธ์์ ๊ฐฏ์
// argv : ์ฝ์ ์ฐฝ์ ์
๋ ฅ๋๋ ๋ฌธ์์ ๋ฐฐ์ด(char array๋ก ๋ฐ์์ผํจ)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;
}: 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
: library๋ ๋ฏธ๋ฆฌ ์ปดํ์ผ ๋๋ object file์ ๋ชจ์์ด๋ค. (Static library์ Shared library๊ฐ ์๋ค.)
-
Static library๋ ์ ๋์ค์์๋ .a(์์นด์ด๋ธ) ํ์ฅ์๋ฅผ ๊ฐ์ง๊ณ , ์๋์ฐ์์๋ .lib(๋ผ์ด๋ธ๋ฌ๋ฆฌ) ํ์ฅ์๋ฅผ ๊ฐ์ง๋ค.
-
Shared library๋ ์ ๋์ค์์๋ .so(shared object) ํ์ฅ์๋ฅผ ๊ฐ์ง๊ณ , ์๋์ฐ์์๋ .dll(dynamic link library) ํ์ฅ์๋ฅผ ๊ฐ์ง๋ค. (์ฐธ๊ณ ๋ก g++์ ๊ธฐ๋ณธ์ ์ผ๋ก shared library๋ฅผ ๋งํฌํ๋ค. )
: 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 filecf ) 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)
#include <vector>
using namespace std;
vector<int> v; // ๋ฐฐ์ด(array)์๋ ๋ค๋ฅด๊ฒ delete๋ฅผ ํด์ค ํ์๊ฐ ์๋ค.
vector<int> v(10);cf) size_t ํ์
: size_t ํ์ ์ C์ธ์ด์์ ์์์ ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ ์๋ ์ต๋ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ฉฐ, ๋ถํธ์๋ ์ ์๋ฅผ ์ฌ์ฉํ๋ฉฐ, unsigned int๋ก ์ฐ์ด๋ ๊ฒฝ์ฐ๋ ์๋ค.
v.at(index)๋ v[index]๋ ๊ฐ๋ค#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์ธ์ง ์ถ๋ ฅํด์ฃผ๋ ๊ฒ์ด๋ค.: ์ธ์๋งํผ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํจ.
: ๋ฒกํฐ ์์ ๊ฐฏ์
: ๋ฒกํฐ ๋ค์ (์ธ์) ์ถ๊ฐ
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ํ์
์ผ๋ก ์ฌ์ฉํ๊ฒ ๋ค.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์ด๋ค.
}auto a = 0; // a์ ํ์
์ ๋ณด๊ณ ์์์ ํ์
์ ๊ฒฐ์ int get_size const{
return size;
} // size๊ฐ์ ๋ฆฌํด ๋ฐ์์๋ ๋ฐ๊ฟ์ ์๋ค.: new๋ฅผ ํ๋ฉด ์์ฑ์๊ฐ ํธ์ถ๋๊ณ , delete๋ฅผ ํ๋ฉด ์๋ฉธ์๊ฐ ํธ์ถ๋๋ค. ๊ทธ๋ฐ๋ฐ, ๊ฐ์ํจ์๋ฅผ ์ ์ํ ๋๋ ์๋ฉธ์๋ ๊ฐ์ํจ์๋ก ๋ง๋ค์ด์ผ ํ๋ค. ์ด ๊ฐ๋ ์ด ์ดํด๊ฐ ๋๊ธฐ ์ํด์๋ ์์ ํด๋์ค ํฌ์ธํฐ ํ์ ์ผ๋ก ํ์ ํด๋์ค๋ฅผ ๋์ ํ ๋น ํ๋ ๊ฒ์ ์ ์ดํด ํด์ผํ๋ค. ์ด๋, ๋ฑ์ฅํ๋ ๊ฒ์ด Contaioner๊ฐ๋ ์ด๋ค.
: ํด๋์ค ํ ํ๋ฆฟ์ผ๋ก, ๊ฐ์ ํ์ ์ ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๋ ์ผ์ข ์ ์งํฉ
-
Sequence Container(๋ฐ์ดํฐ๋ฅผ ์ ํ์ผ๋ก ์ ์ฅํ๋ฉฐ, ํน๋ณํ ์ ์ฝ์ด๋ ๊ท์น์ด ์๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์ปจํ ์ด๋)
=> array, vector, list, deque
-
Associative Container(๋ฐ์ดํฐ๋ฅผ ์ผ์ ๊ท์น์ ๋ฐ๋ผ ์กฐ์งํํ์ฌ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ ์ปจํ ์ด๋)
=> set, multiset, map, multimap
: ๋ถ๋ชจ ํด๋์ค ํ์ ์ผ๋ก ์๋ ํ์ ํด๋์ค ์์ฑ
=> ์ด์จ๋ 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๋ฅผ ์ ์ํ๋ค.: 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ํด์ฃผ๋๊ฒ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
- Mutual exclusion(์ํธ๋ฒ ํ) : ์ค์ง ํ thread๋ง ์ง์
- Progress(์งํ) : ์ง์ ๊ฒฐ์ ์ ์ ํ ์๊ฐ ๋ด
- Bounded waiting(์ ํ๋๊ธฐ) : ์ด๋ thread๋ผ๋ ์ ํ ์๊ฐ ๋ด
busy waiting : Sem๊ฐ ๋๋ฌธ์ ๊ณ์ while๋ฌธ ๋๊ณ ์๋ ์ํฉ
block : ํ๋ก์ธ์ค๋ฅผ waiting Q์ ๋๋ ๊ฒ
wakeup : ํ๋ก์ธ์ค๋ฅผ ready Q๋ก ์ด๋ํ๋ ๊ฒ
: ํด๋์ค์ ๊ฐ์ ํจ์๋ฅผ ์ ์ํ ๋๋ ์๋ฉธ์๋ ๊ฐ์ํจ์๋ก ๋ง๋ค์ด์ผ ํ๋ค. ๊ฐ๋ น,
class Student : public Person{
}
Person p = new Student;๋ผ๊ณ ํ๋ค๋ฉด, Person์์ virtualํจ์๊ฐ ์์ด์ Student์์ virtual์ ์ ์ํ๋ค๋ฉด Person์์ ์๋ฉธ์๋ virtual๋ก ํด์ฃผ์ด์ผ ๋์ค์ p๋ฅผ deleteํ ๋, Student๋ฅผ ๋จผ์ deleteํ๊ณ , Person์ deleteํ๋ค. ์ฝ๊ฒ ๋งํด์, ์์๋ฐ์ ํด๋์ค๋ ์์ ํ ํด์ ํ๊ธฐ ์ํ ํ๋์ ์คํฌ์ด๋ค.
Overriding => ํ์ ํด๋์ค์์ ํน์ฑ์ด ๋ค๋ฅผ ๋, ๊ฐ์ ์ด๋ฆ์ผ๋ก ์ฌ์ ์ ํ๋ ๊ฒ.(์์ ๊ธฐ๋ฐ)
Overloading => ํจ์์ ์ด๋ฆ์ ๊ฐ๊ฒ!! ํ๋ฉด์ ํจ์ ์์ ๋ค์ด๊ฐ๋ ์ธ์ ๊ฐ์!! ๋ค๋ฅด๊ฒ ํด์ ์ตํต์ฑ์ ๋์ด๋ ๊ฒ. (ํ๋์ ํด๋์ค ๋ด์์ ์ผ์ด๋๋ ์ผ)
: ์์ฑ์ ์ค์ ๋ณธ์ธ ํด๋์ค๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ์์ฑ์. ์ด ๋, ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ํด๋์ค๋ const๋ก ๋ฐ๊ธฐ.
cf) ๊ทธ๋ฐ๋ฐ ๋ณต์ฌ ์์ฑ์๋ฅผ ์ ์ํ์ง ์์ผ๋ฉด, ๋ฉค๋ฒ ๋ ๋ฉค๋ฒ์ ๋ณต์ฌ๋ฅผ ์งํํ๋ ๋ณต์ฌ ์์ฑ์๊ฐ ์๋์ผ๋ก ์ฝ์ ๋๋ค. ์ด๋ฅผ ๋ํดํธ ๋ณต์ฌ ์์ ์๋ผ๊ณ ํ๋๋ฐ, ์ด๋ ์์ ๋ณต์ฌ(shallow copy)๊ฐ ๋์ด ์๋ฉธ๊ณผ์ ์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค.
cf) explicit๋ ๊ฐ์ฒด๋ฅผ ์์ฑ์ ์ด๊ธฐํ ๊ฐ์ ์ค๋, '=' ์ ์ฌ์ฉํ ์ ์๋ค.
// class A ๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์ ๋, public์์ ๋ณต์ฌ ์์ฑ์
A(const A ©)
: num(copy.num)
{
}: ํธ์ถ์์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ๊ธฐ์กด์ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๊ฒฝ์ฐ
- call-by-value ๋ฐฉ์์ ํจ์ํธ์ถ ๊ณผ์ ์์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฒฝ์ฐ
- ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋, ์ฐธ์กฐํ์ผ๋ก ๋ฐํํ์ง ์๋ ๊ฒฝ์ฐ
์์๋ฐ๋ ์ํฉ์ผ๋ก๋ ํ์ธํด๋ณด์
// class Student๊ฐ Person์ ์์ ๋ฐ๋๋ค๊ณ ๊ฐ์ ํ์ ๋, Student์ public์ ๋ณต์ฌ ์์ฑ์
Student(char* myname, int myage, char* mymajor)
: Person(myage, myname) // ๋ถ๋ชจ ํด๋์ค์ ๋ฉค๋ฒ๊น์ง ์ด๊ธฐํํด์ผํ ์๋ฌด๊ฐ ์๋ค.
{
strcpy(major, mymajor);
}: ์์ฑ๊ณผ ์๋ฉธ์ ๋ค์๊ณผ ๊ฐ๋ค.
์์ฑ์ 1)๋ถ๋ชจ์์ฑ์ 2)์๋ ์์ฑ์, ์๋ฉธ์ 1)์๋ ์์ฑ์ 2)๋ถ๋ชจ์์ฑ์
cf) ์์์ ํ๋ ๋๊ฐ์ง ์ผ์ด์ค
- is-a : ์๋ ๋ ๋ถ๋ชจ๋ค. ๋ผ๋ ๋ง์ด ์ฑ๋ฆฝ๋ ๋ ex) ์ฌ๊ณผ๋ ๊ณผ์ผ์ด๋ค.
- 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;
}
}[๋จผ์ ์์์ผํ ๊ฒ]
-
๊ฐ์ฒด ํฌ์ธํฐ ๋ณ์
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 }
-
๊ณ์ธต๊ตฌ์กฐ์ ๋งจ ์์ ์กด์ฌํ๋ ๊ธฐ์ด ํด๋์ค์ ๊ฐ์ํจ์ ํน์ ์๋ฉธ์๋ง 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์ ์๋ฉธ์๊ฐ ์ฐจ๋ก๋ก ๋ถ๋ ค ๋ชจ๋ ์๋ฉธ๋๋ค.
: ์์์ ๋ฐ์ ๋ ๋ฉค๋ฒ๋ณ์ ์ ๊ทผ์ ํ์ ์ฐจ์ด
| ์ธ๋ถ์ ๊ทผ | ์๋ ํด๋์ค | |
|---|---|---|
| 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์ด ๋๋ค.: A->Func()์ผ๋ก ์ฌ์ฉํ๋ค๋ฉด A๋ ํฌ์ธํฐ ๋ณ์์ด๋ค. ๋.
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ํธ์ถ)
}: ๊ธฐ์กด์ ์กด์ฌํ๋ ์๋ฃํ์ ์ด๋ฆ์ ์ ์ด๋ฆ์ ๋ถ์ฌํ๋ ๊ฒ์ ๋ชฉ์ ์ผ๋ก ํ๋ ์ ์ธ์ด๋ค.
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ํ์ง ์์ผ๋ฉด ์ด๋ป๊ฒ ๋๋๊ฐ? => ๋ถ๋ชจ๊บผ๋ฅผ ์ฌ์ฉํ๊ฒ ๋จ. ๋ฌธ์ ์์.
: C++์์๋ ๋๋ณด๋ค ์ค์ด ๋ ๋ง์ ๋ฌธ๋ฒ์ด๋ผ๊ณ ํ๋๋ฐ, ์๋ฐ์์๋ ์ ๋ค์ค ์์์?? ํ๋๊ฑฐ ๊ฐ์๋ฐ. ์ด๋ ๋ถ๋ชจ์ ๋ฉ์๋๋ก ์ ๊ทผํด์ผํ ์ง ๋ชจํธ์ฑ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ๋๋๋ก ์ฌ์ฉ X.
: ๋ณต์กํ๊ฒ ์๊ฐ๋ง๊ณ , ๊ทธ๋ฅ ์ง๊ด์ ์ผ๋ก ๋ฐ์๋ค์ฌ๋ณด์.
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๋ก ์ ์ธํ๋ ๊ฒ์ด ์ข๋ค.
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
-
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
- Atom is a text editor that most professional programmers love nowadays.
- Start Atom.
- 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.
- Open a rendered version of the Markdown in the current editor with
- 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.
- Autosave
#if 1
#endif
#if 0
#endif#ifndef NOWIC_H // .hํ์ผ์ gaurd(์ค๋ณต include๋ฅผ ๋ฐฉ์ง)ํด์ฃผ๋ ๊ฒ.
#define NOWIC_H#ifdef DEBUG // ์ปดํ์ผํ ๋, ๋๋ฒ๊ทธ๋ฅผ ์ฒดํฌํ ์ ์๋ค.
#define DPRINT(func) func; // ()์์ ์๋ ๊ฒ์ด func๊ฐ ๋๋ค.
#else
#define DPRINT(func) ; // debug๊ฐ ์์ผ๋ฉด ()์์ ์๋ ๊ฒ์ด null์ด ๋๋ค.
#endif: (1) mallocํ๊ณ ๋ assertํ๊ธฐ (2) freeํด์ผํจ
char *ptr = new (nothrow) char[5]; // nothrow๋ ์์ธ์ฒ๋ฆฌX ๋ผ๋๋ง
assert(ptr != nullptr); // ()์ด์ด์ผ ํจ์ ์ฃผ์ฅ. ()๊ฐ ์๋๋ฉด Message!1. time()
=> #include <ctime>์ด ํ์ํ๋ฉฐ, ???
2. rand()
=>
3. srand()
=>
4. ๋๋ค ์๋ ๋๋ฒ
#include <ctime>
srand((unsigned int)time(NULL));
: ๋๋๋ก debugging์ ์ฝ๊ฒ ํ๊ธฐ ์ํด, ์ ์(์์๋ก) ๋ฃ์ด๋ ์ฝ๋ ์ ๋๋ค.
์ปดํ์ผ ํ ๋, ์๋์ ๊ฐ์ด -DDEBUG ์ต์ ์ ์ถ๊ฐํ๋ฉด, ํด๋นํ๋ ์ฝ๋๊ฐ ํ๋ฆฐํธ ๋ฉ๋๋ค. ์์ง debugger ๊ฐ ๋ฐฐ์ฐ๊ธฐ ์ ๊น์ง ๋ณต์กํ source code ์ด๋์์ ๋ฌธ์ ๊ฐ ์๊ฒผ๋์ง ์ฝ๊ฒ ์ฐพ์ ์ ์์ฃ . ๋๋ฒ๊น ์ด ๋๋ ๋ค์์๋ ๋ฌผ๋ก -DDEBUG์์ด ์ปดํ์ผํ๋ฉด, ํด๋น source code๋ ์๋ฌด๋ฐ ์ํฅ์ด ์์ต๋๋ค.
g++ -DDEBUG ์ด์ ) 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"#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++;
}
}[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 ๋ก- ์ผ๋ฐ
for(int i=0; i<n; i++){
cout << v[i] << " ";
}- ๋ชจ๋ ์ธ๋ฑ์ค ์ ๊ทผ์ ํ์ฉ
for(int x : v){
cout << x << " ";
}- ํ์ ์ ์ตํต์ฑ์
for(auto x : v){
cout << x << " ";
}- 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๋ฅผ ์ฐ๋ ๊ฒ์ด๋ค.
}- ๊ทธ๋ฐ๋ฐ ์ด๋ง์ ๋ auto๊ฐ ๋๋ค.
// it์ pointer๋ฅผ ์ ์ฅํ ์ ์์
for(auto it =v.begin(); it!=v.end(); it++){
cout << *it << " ";
}- 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- 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์ญ์ญ ๋ค ์ธ์ค ์์์ผ๋๋ค.
| ๋ฉค๋ฒ ํจ์ | ๊ธฐ๋ฅ |
|---|---|
| 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๋ ์ ๋ ฌํ ์ ์์ต๋๋ค.
| ๋ฉค๋ฒ ํจ์ | ๊ธฐ๋ฅ |
|---|---|
| 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()ํ๋ ๊ฒ๋ณด๋ค ๋น ๋ฅด๊ธฐ ๋๋ฌธ์ ์์๋๋ ๊ฒ์ด ์ข๊ฒ ์ต๋๋ค.
| ๋ฉค๋ฒ ํจ์ | ๊ธฐ๋ฅ |
|---|---|
| s.size() | s์ ์ฌ์ด์ฆ(๋ฌผ๋ฆฌ์ ์ธ ์ ์ฅ ์ฉ๋์ด ์๋ ์์์ ๊ฐ์)๋ฅผ ๋ฆฌํด |
| s.empty() | s์ ์ฌ์ด์ฆ๊ฐ 0์ธ์ง ์๋์ง๋ฅผ ํ์ธ |
| s.top() | s์ ๊ฐ์ฅ ๋์ค์ ๋ค์ด๊ฐ ์์๋ฅผ ๋ฆฌํด |
| s.push(val) | s์ ๋ค์ val ์ถ๊ฐ |
| s.pop() | s์ ๊ฐ์ฅ ๋์ค์ ๋ค์ด๊ฐ ์์๋ฅผ ์ญ์ |
: 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 [์ฃผ๋์ด ๊ฐ๋ฐ์์ ๋๋๋ฌด์ฒ]
-
์ฝ๋ฉ ์ปจ๋ฒค์ : ์ถ์ฒํ๋ ํ์ค์ ์ฝ๋ฉ ์คํ์ผ
-
linter : ์ฝ๋ฉ ์ปจ๋ฒค์ ๊ณผ ์๋ฌ์ฒดํฌ๋ฅผ ๋์์ฃผ๋ ํด
-
์ ์ ๋ถ์ : ํ๋ก๊ทธ๋จ ์คํ ์๊ณ ์ฝ๋๋ฅผ ๋ถ์ํ๋ ๊ฒ.
-
๋์ ๋ถ์ : ํ๋ก๊ทธ๋จ ์คํํด๋ณด๊ณ ์ฝ๋๋ฅผ ๋ถ์ํ๋ ๊ฒ.
โ
-
์ํฐ ์๋ฌ ์ฝ๊ฒ ๋ณด๊ธฐ.. (compiling error txt๋ผ๋..), linter ํจํค์ง ๋ค์ด๋ก๋ ๋ฐ์๋๋ฐ... ๋ญ์ง...
-
fprint, sprintf
-
setvbuf
-
??? ํด๊ฒฐํ๊ธฐ
-
์ดํด๋ถ๊ฐ => 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; }
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;
} // ์ด๋ด๋ -> ์ฐ๋๊ฑฐ์.(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์ถ๋ ฅ -> ...