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

๐Ÿ’Ž/C++9

[C++] ๊ณต๋ฐฑ ํฌํ•จ ๋ฌธ์ž์—ด ์ž…๋ ฅ | stringstream ๋ฌธ์ž์—ด ์ถ”์ถœ ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋จผ์ €, ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ๋ฌธ์ž์—ด์„ ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค. 1 2 3 4 5 6 7 // string ๊ณต๋ฐฑ ํฌํ•จ ๋ฌธ์ž์—ด ์ž…๋ ฅ string str; getline(cin, str); // char ๊ณต๋ฐฑ ํฌํ•จ ๋ฌธ์ž์—ด ์ž…๋ ฅ char ch[100]; cin.getline(ch, 100); cs string ์ž๋ฃŒํ˜•์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ—ค๋” ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ istream ๊ฐ์ฒด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ž…๋ ฅ ๋ฐ›์„ string ๋ณ€์ˆ˜ ์ด๋ฆ„ ์„ธ ๋ฒˆ์งธ ์ธ์ž๋กœ delimitChar(์ƒ๋žต๊ฐ€๋Šฅ. default '\n') delimitChar์— ' '๋ฅผ ์ค€๋‹ค๋ฉด, ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋Š์–ด ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. char ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” cin.geline์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ์ค„๋กœ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ ํ—ค.. 2021. 3. 2.
[C++] ํ˜•์‹ ์ง€์ •์ž, ์„œ์‹ ์ง€์ •์ž %[ํ”Œ๋ž˜๊ทธ][ํญ][.์ •๋ฐ€๋„][๊ธธ์ด]์„œ์‹์ง€์ •์ž ์˜ˆ์ œ 1 ํญ๊ณผ ํ”Œ๋ž˜๊ทธ๋ฅผ ํ•จ๊ป˜ ์จ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํญ ์•ž์— 0์„ ๋„ฃ์œผ๋ฉด ๋‚จ๋Š” ๊ณต๊ฐ„์—๋Š” ๊ณต๋ฐฑ ๋Œ€์‹  0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. printf("%06d\n", 20); // ์ถœ๋ ฅ ํญ์„ 6์นธ์œผ๋กœ ์ง€์ •, ๋‚จ๋Š” ๊ณต๊ฐ„์€ 0์œผ๋กœ ์ฑ„์›€ printf("%06d\n", 2000); // ์ถœ๋ ฅ ํญ์„ 6์นธ์œผ๋กœ ์ง€์ •, ๋‚จ๋Š” ๊ณต๊ฐ„์€ 0์œผ๋กœ ์ฑ„์›€ ํญ์„ 6์นธ, ๋‚จ๋Š” ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ฑ„์šฐ๋„๋ก ํ–ˆ์œผ๋ฏ€๋กœ 20 ์•ž์—๋Š” 0์ด 4๊ฐœ, 2000 ์•ž์—๋Š” 0์ด 2๊ฐœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ํญ์„ 6์นธ, ๋‚จ๋Š” ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ฑ„์šฐ๋„๋ก ํ–ˆ์œผ๋ฏ€๋กœ 20 ์•ž์—๋Š” 0์ด 4๊ฐœ, 2000 ์•ž์—๋Š” 0์ด 2๊ฐœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ ↓ 0์ด 4๊ฐœ ์ฑ„์›Œ์ง 000020 002000 ↑ 0์ด 2๊ฐœ ์ฑ„์›Œ์ง ์˜ˆ์ œ 2 ์‹ค์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” %f, %e๋Š” ์ •๋ฐ€.. 2021. 1. 13.
[C++] cin, cout ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฌธ์ œ ํ•ด๊ฒฐ std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); or ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ์œ„์˜ ์ฝ”๋“œ๋ฅผ main() ์•ˆ์— ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 0์ด๋“ , NULL์ด๋“  ๊ฐ™์€ ์˜๋ฏธ๋กœ ์“ฐ์ธ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ๋ฐฉ๋ฒ•์€ ๋งŒ๋Šฅ์€ ์•„๋‹ˆ๋ผ๊ณ  ํ•˜๋‹ˆ ๋ถˆ๊ฐ€ํ”ผํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ๋˜๋„๋ก scanf, printf๋ฅผ ์‚ฌ์šฉํ•˜์ž. algospot.com :: ์ž์œ ๊ฒŒ์‹œํŒ: ๊ฐ ์–ธ์–ด๋ณ„ input method ๋น„๊ต ๊ฐ ์–ธ์–ด๋ณ„ input method ๋น„๊ต 13๊ฐœ์˜ ๋Œ“๊ธ€์ด ์žˆ์Šต๋‹ˆ๋‹ค. algospot.com cin ๊ณผ scanf ์— ๋Œ€ํ•ด์„œ ACM-ICPC, Olympiad ๋ฌธ์ œ ์ •๋ฆฌ ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ.. 2020. 10. 16.
[C++] priority_queue ์šฐ์„ ์ˆœ์œ„ ํ ๋‚ด๋ฆผ์ฐจ์ˆœ less๊ฐ€ default ๋งจ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” pq.top() ์œผ๋กœ ์ ‘๊ทผ X๋Š” ์ž๋ฃŒํ˜•์ผ ๋•Œ, ์˜ค๋ฆ„์ฐจ์ˆœ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๐Ÿ‘‰ priority_queue pq; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include #include using namespace std; typedef pair p; priority_queue pq; // ๋‚ด๋ฆผ์ฐจ์ˆœ default priority_queue pq1; // ๋‚ด๋ฆผ์ฐจ์ˆœ defau.. 2020. 3. 25.
[C++] ๋ฐฐ์—ด๋ณต์‚ฌ memcpy, copy memcpy #include // C++ ์—์„œ๋Š” void* memcpy(void* destination, const void* source, size_t num); ์˜ˆ์ œ memcpy(arr, tarr, sizeof(arr)); copy #include copy(initArray, initArray + n, v.begin()); // initArray๋‚ด์šฉ์„ n๋งŒํผ v์— ๋ณต์‚ฌ copy(v.begin(), v.end(), v2.begin()); // v์˜ ๋‚ด์šฉ์„ v2์— ๋ณต์‚ฌ ์ƒ์„ธ template OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result) { while (first!=last) { *result = *f.. 2020. 3. 23.
[C++] map iterator(๋ฐ˜๋ณต์ž) ํ™œ์šฉ map์˜ ์›์†Œ๋Š” pair ๊ฐ์ฒด๋กœ ์ €์žฅ๋˜๋ฉฐ pair ๊ฐ์ฒด์˜ first ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” key๋กœ second ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” value์ด๋‹ค. ์˜ˆ์ œ #include #include using namespace std; map m;// map ์„ ์–ธ int main() { m[10] = 4; m[8] = 9; m[100]++; map::iterator iter;// iterator ์„ ์–ธ for (iter = m.begin(); iter != m.end(); iter++) { printf("์ฃผ์†Œ๊ฐ’: %d ", iter); printf("key: %d ", *iter); printf("key: %d value: %d\n", iter->first, iter->second); } map::iterator iter2 = m.be.. 2020. 3. 23.
[C++] string ๋ฉ”์†Œ๋“œ ์ •๋ฆฌ string.size() string.length() char string::at() char c = string[0]; string.front() = string.at(0) string.back() string.insert(์ถ”๊ฐ€ํ•  ์œ„์น˜, ๋ฌธ์ž์—ด) string string::find() string.clear() void clear(); bool empty() const; string substr (size_t pos, size_t len) const; string::iterrator it; string& string::erase (size_type pos) pos ์ดํ›„๋กœ ์ „๋ถ€ ์‚ญ์ œ 2๋ฉด 3๋ถ€ํ„ฐ ์‚ญ์ œ. string& string:: erase (iterator pos) ํ•œ ๊ธ€์ž ์‚ญ์ œ string& rep.. 2020. 3. 17.
[C++] vector ๋ฐฐ์—ด ์ค‘๋ณต ์ œ๊ฑฐ ํ•˜๋Š” ๋ฒ• ๋ฐฐ์—ด ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฒ• vector v ๋ฅผ ์œ ์ผํ•œ ์›์†Œ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด, ์ •๋ ฌ์„ ํ•œ๋‹ค. : sort ์—ฐ์†๋œ ์ค‘๋ณต ์›์†Œ๋ฅผ vector์˜ ์ œ์ผ ๋’ท๋ถ€๋ถ„(์“ฐ๋ ˆ๊ธฐ ๊ฐ’)์œผ๋กœ ๋ณด๋‚ด๋ฒ„๋ฆฐ๋‹ค. : unique ์ค‘๋ณต๋œ ์›์†Œ๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๋’ท๋ถ€๋ถ„์„ ์‚ญ์ œํ•œ๋‹ค : erase unique ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด vector ์“ฐ๋ ˆ๊ธฐ ๊ฐ’์˜ ์ฒซ๋ฒˆ์งธ ์œ„์น˜๊ฐ€ ๋˜๋ฏ€๋กœ ๋ฐ”๋กœ erase ํ•จ์ˆ˜ ์•ˆ์— ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include #include #include using namespace std; int main() { vector v; v.push_back(3); v.push_back(4); v.push_ba.. 2020. 3. 10.
[C++] int to string, string to int | to_string, stoi โ— int → string # to_string int num = 12345; string str = to_string(num); โ— string → int # stoi string str = "abcde"; int num = stoi(str); โ— char* → int # ๋˜๋Š” # atoi() char* cStr = "abcde"; int num = atoi(cStr); โ— string → char* → int #, # c_str() → atoi() string s = "abcde"; int num = atoi(s.c_str()); 2020. 3. 10.