๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿฐ42์„œ์šธ/CPP_Module

CPP_Module_01 ๊ฐ์ฒด ๋ฐฐ์—ด ์ƒ์„ฑ, ํฌ์ธํ„ฐ์™€ ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…, ํ•จ์ˆ˜ํฌ์ธํ„ฐ, fstream

CPP module 01

ex00

  • zombie ๊ฐ์ฒด๋ฅผ ๋‘๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ.
    • ์Šคํƒ์— zombie ๊ฐ์ฒด ์ƒ์„ฑ (randomChump())
    • ํž™์— ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ธฐ(newZombie())

heap์— ๋งŒ๋“ค๊ธฐ : new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ž๋™์œผ๋กœ ์†Œ๋ฉธ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‹ค ์“ฐ๋ฉด delete๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•จ

ex01

๊ฐ์ฒด ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๊ธฐ๋ž‘ ํ• ๋‹น ํ•ด์ œํ•˜๊ธฐ

//zombie ๊ฐ์ฒด n๊ฐœ๋ฅผ ๋ฅผ ํ•œ๋ฒˆ์— ํ• ๋‹น
Zombie *z = new Zombie[N];
delete [] z;

ex02

pointer ์™€ reference ํƒ€์ž…์˜ ์ฐจ์ด ์ดํ•ดํ•˜๊ธฐ

int n = 0;
int *ptr = &n;
int &ref = n;
  • ptr ์•ˆ์—๋Š” n์ด ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ณณ์˜ ์ฃผ์†Œ๊ฐ’์ด ์žˆ์Œ. ptr์„ ํ†ตํ•ด n์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„ ๋ฟ ptr๊ณผ n์€ ์ „ํ˜€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜
  • ref๋Š” n๊ณผ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ๋ณด๋ฉด๋ ๋“ฏ? ๋ณ€์ˆ˜ n์— ref๋ผ๋Š” ๋‹ค๋ฅธ ์ด๋ฆ„์ด ์ƒ๊ฒผ์„ ๋ฟ ๋‘˜์ด๋Š” ๊ฐ™๋‹ค !

ex03

ref type์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ธ์ž๋ฅผ refํƒ€์ž…์œผ๋กœ ๋„ฃ๋Š” ๊ฒƒ์˜ ์ด์ ์ด ๋ญ˜๊นŒ?

์ฐธ์กฐ ์ „๋‹ฌ์‹œ ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ์„ ์•ˆํ•ด์„œ ๋น ๋ฆ„

ex04

  • s1์ด null์ผ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. s2๋Š” null์ด ์˜ค๋ฉด ๊ทธ ๋ฌธ์ž๋ฅผ ์ง€์›Œ์ฃผ๋Š” ๊ฑธ๋กœ!
int main(int argc, char **argv) {
    if (argc != 4) {
        std::cout << "[Usage]: ./SedIsForLosers <file_name> <s1> <s2>" << std::endl;
        return 0; 
    }

    std::string    s1 = argv[2];
    std::string    s2 = argv[3];
    if (s1.empty()) {
        std::cout << "String1 must NOT be empty." << std::endl;
        return 0;
    }

Constructs an ifstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.If the file cannot be opened, the stream's failbit flag is set.

cplusplus ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด๋ฉด โ€ฆ! ifstream์„ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋™์‹œ์— ์ธ์ž๋กœ filename์„ ๋„ฃ์œผ๋ฉด open์ด ํ˜ธ์ถœ๋จ. ์•„๋งˆ ofstream๋„ ๋งˆ์ฐฌ๊ฐ€์ง€

  • file์„ open ํ•˜๋ ค๋Š”๋ฐ c++98์€ file name์„ ๋„ฃ์„ ๋•Œ char * ๋งŒ์„ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ํ™•์ธํ•จโ€ฆใ…Žใ…Ž std::string์„ ์ธ์ž๋กœ ๋„ฃ๋Š” ๊ฒƒ์€ c++11๋ถ€ํ„ฐ๋ผ๊ณ  ๋˜์–ด์žˆ๋‹คโ€ฆ.! ๊ทธ๋ž˜์„œ ์–ด์ฉ”์ˆ˜ ์—†์ดโ€ฆ. char * ํ˜•์„ ์ธ์ž๋กœ ๋„ฃ๊ธฐ๋กœ!
std::ifstream ifs_src(argv[1]);
    if (ifs_src.fail()) {
        std::cout << "No such file" << std::endl;
        return 0;
    }

    std::ofstream ofs_dst((std::string(argv[1]) + ".replace").c_str());
    if (ofs_dst.fail()) {
        std::cout << "Open file failed." << std::endl;
        return (0);
    }
  • file์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™€์„œ s1์„ s2๋กœ ๋ฐ”๊ฟ”์•ผํ•˜๋Š”๋ฐ.. getline์„ ์“ฐ๋ฉด delim์˜ ๋ฌธ์ž๊นŒ์ง€ ๊ฐ€์ ธ์˜ค๊ณ  ์ œ๊ฑฐํ•ด๋ฒ„๋ฆผ. ๋””ํดํŠธ๊ฐ€ โ€˜\nโ€™์œผ๋กœ ๋˜์–ด์žˆ์œผ๋‹ˆ s1์„ โ€˜\nโ€™๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋• ์“ธ ์ˆ˜๊ฐ€ ์—†์Œ! ๊ทธ๋Ÿฌ๋ฉด delim์„ โ€˜\0โ€™๋กœ ํ•˜๋ฉดโ€ฆ! ํŒŒ์ผ ๋๊นŒ์ง€ ์ฝ์–ด์„œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ๋œ๋‹นโ€ฆ.. ํŒŒ์ผ ๋๊นŒ์ง€ string์— ๋‹ด๋Š” ์ˆ˜ ๋ฐ–์— ์—†๊ฒ ๋‹น.. while์„ ๋Œ๋ ค ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ input stream์„ ๋„˜์–ด๋„ ๊ดœ์ฐฎ๋„๋ก ํ•จ!
  • replace๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์‹ ํ•ด์„œ erase์™€ insert๋ฅผ ์‚ฌ์šฉ !
std::string line;
    while(std::getline(ifs_src, line, '\0')) {
        size_t pos = 0;
        while ((pos = line.find(s1, pos)) != std::string::npos) {
            line.erase(pos, s1.length());
            line.insert(pos, s2);
            pos += s2.length();
        }
        ofs_dst << line;
    }
    ifs_src.close();
    ofs_dst.close();
    return 0;
}

ex05

ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ์‚ฌ์šฉํ•˜๊ธฐ

int (*fp1)();
int (*fp2)(int);
int (*fp3)(int, int);
bool (*fp4)();
void (*fp5)();

๋ฐ˜ํ™˜๊ฐ’ (*๋ณ€์ˆ˜์ด๋ฆ„)(์ธ์ž๊ฐ’);
๋ฐ˜ํ™˜๊ฐ’ (*๋ณ€์ˆ˜์ด๋ฆ„[n])(์ธ์ž๊ฐ’); //๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๋•Œ