AcceleratedC++/Chapter5 | AcceleratedC++/Chapter7 |
Contents
|
1. Chapter 6 Using Library Algorithms ¶
- 5์ฅ์์ ๋ณธ๊ฒ์ฒ๋ผ ์ฐ๋ฆฌ๊ฐ ๋ค๋ฃจ๋ ์ปจํ
์ด๋๋ค์ ๋ด๋ถ ์ฌ์ ์ ๋ค๋ฅผ์ง๋ผ๋, ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ๋ชจ๋ฅด๊ณ ๋ ๋๊ฐ์ด ์ธ ์๊ฐ ์๋ค. ์ฆ ์ผ๊ด๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค๋ ๊ฒ์ด๋ค. ์ปจํ
์ด๋๋ ๋ฐ๋ณต์์ ๋ง์ฐฌ๊ฐ์ง๋ก ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ผ๊ด๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค. ๋ฒกํฐ๋ฅผ ๋ฐฐ์ ์ผ๋ฉด ๋ฆฌ์คํธ๋ ๊ธ๋ฐฉ ์ธ์ ์๋ ๊ฒ์ฒ๋ผ, ํ๋์ ์๊ณ ๋ฆฌ์ฆ ์ฐ๋ ๋ฒ์ ๋ฐฐ์ฐ๋ฉด, ๋ค๋ฅธ ๊ฒ ์ฐ๋ ๋ฒ๋ ๊ธ๋ฐฉ ์์๊ฐ ์๋ค.
1.1. 6.1 Analyzing strings ¶
- Chapter5์ ๋ง์ง๋ง์ ๋ฃจํ๋ฅผ ์ค์ธ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ๋ฌธ์ด ์์๋ค. ret์ ๋์๋ค๊ฐ bottom์ ์ฒ์๋ถํฐ ๋๊น์ง ๋ฃ๋๋ค๋ ๋ป์ด๋ค.
~cpp ret.insert(ret.end(), bottom,begin(), bottom.end());
- ๊ทผ๋ฐ ์ด๊ฒ๋ณด๋ค ๋ ์ผ๋ฐ์ ์ธ, (์ฆ ์ปจํ
์ด๋์ ๋
๋ฆฝ์ ์ธ) ๋ฐฉ๋ฒ์ด ์๋ค. ์ปจํ
์ด๋์ ๋ฉค๋ฒํจ์๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ์๋, ํ์ค ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ๋ ๊ฒ์ด๋ค. ์์ ๊ฒ๊ณผ ๋์ผํ ๊ธฐ๋ฅ์ ํ๋ค.
~cpp copy(bottom.begin(), bottom.end(), back_inserter(ret));
- ์. ๋ ์๋ก์ด ๊ฒ์ด ๋ณด์ด์ง ์๋๊ฐ? copy๋ generic algorithm์ ์์ด๊ณ , back_inserter๋ ๋ฐ๋ณต์ ์ด๋ํฐ์ ์์ด๋ค. ์ด๊ฒ ๋ฌด์์ธ์ง๋ ์ฐจ๊ทผ์ฐจ๊ทผ ์ดํด๋ณด๋๋ก ํ์.
- Generic algorithm์ด๋ผ๋ ์ปจํ
์ด๋์ ๋ถ๋ถ์ด ์๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ํ๋ผ๋ฉํฐ๋ก ๋ฐ๋ณต์๋ฅผ ๋ฐ๋๋ค. ๋น์ทํ์ง ์์๊ฐ? .์ด ์๋ค ๋ฟ์ด์ง ๊ทธ๋ฅ ์ฐ์.
- Postfix์ Prefix : i++๊ณผ ++i์ ์ฐจ์ด์ ์ด๋ค. ++i๋ i๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ๊ฐ์ ์ฆ๊ฐ์ํค๊ณ , i++์ i๋ฅผ ์ฌ์ฉํ ํ์ ๊ฐ์ ์ฆ๊ฐ์ํจ๋ค.
- ๋ค์์ผ๋ก ๋ฐ๋ณต์ ์ด๋ํฐ(Iterator Adapters)๋ฅผ ์ดํด๋ณด์. ๋ฐ๋ณต์ ์ด๋ํฐ๋ ์ปจํ
์ด๋๋ฅผ ์ธ์๋ก ๋ฐ์, ์ ํด์ง ์์
์ ์ํํ๊ณ ๋ฐ๋ณต์๋ฅผ ๋ฆฌํดํด์ฃผ๋ ํจ์์ด๋ค. copy์๊ณ ๋ฆฌ์ฆ์ ์ฐ์ธ back_inserter๋ ret์ ๋ค์๋ค๊ฐ copy๋ฅผ ์ํํ๋ค๋ ๊ฒ์ด๋ค. ๊ทธ๋ผ ๋ค์๊ณผ ๊ฐ์ด ์ฐ๊ณ ์ถ์ ์ฌ๋๋ ์์ ๊ฒ์ด๋ค.
~cpp copy(bottom.begin(), bottom.end(), ret.end());
- ์์์๋ ๋งํ์ง๋ง, end()์๋ ์๋ฌด๊ฒ๋ ์๋ค.
- ์ ์ด๋ ๊ฒ ์ค๊ณํ๋๊ฐ? ํ๋ก๊ทธ๋๋จธ๋ก ํ์ฌ๊ธ ์ฐ๊ณ ์ถ์ ์ฐ์ฐ์ ๊ณจ๋ผ์ ์ธ์ ์๊ฒ ํ๊ธฐ ๋๋ฌธ์ด๋ค.
1.1.1. 6.1.1 Another way to split ¶
~cpp vector<string> split(const string& str) { typedef string::const_iterator iter; vector<string> ret; iter i = str.begin(); while(i != str.end()) { i = find_if(i, str.end(), not_space); // ๊ณต๋ฐฑ์ด ์๋ ๋ถ๋ถ์ ์ฐพ๊ณ iter j = find_if(i, str.end(), space); // ๊ณต๋ฐฑ์ธ ๋ถ๋ถ์ ์ฐพ์์ if(i != str.end()) ret.push_back(string(i,j)); // ๊ทธ๋งํผ์ ๋ฌธ์์ด์ ๋ฒกํฐ์ ๋ฃ์ i = j; } return ret; }
1.1.2. 6.1.2 Palindromes ¶
~cpp bool isPalindrome(const string& s) { return equal(s.begin(), s.end(), s.rbegin()); }
1.1.3. 6.1.3 Finding URL ¶
~cpp #ifndef GUARD_urls_h #define GUARD_urls_h #include <vector> #include <string> std::vector<std::string> find_urls(const std::string& s); #endif
~cpp #include <algorithm> #include <cctype> #include <string> #include <vector> #include "urls.h" using std::find; using std::find_if; using std::isalnum; using std::isalpha; using std::isdigit; using std::search; using std::string; using std::vector; bool not_url_char(char); string::const_iterator url_end(string::const_iterator, string::const_iterator); string::const_iterator url_beg(string::const_iterator, string::const_iterator); vector<string> find_urls(const string& s) { vector<string> ret; typedef string::const_iterator iter; iter b = s.begin(), e = s.end(); // look through the entire input while (b != e) { // look for one or more letters followed by `://' b = url_beg(b, e); // if we found it if (b != e) { // get the rest of the \s-1URL\s0 iter after = url_end(b, e); // remember the \s-1URL\s0 ret.push_back(string(b, after)); // advance `b' and check for more \s-1URL\s0s on this line b = after; } } return ret; } string::const_iterator url_end(string::const_iterator b, string::const_iterator e) { return find_if(b, e, not_url_char); } // find_if ํจ์์ ํ ์คํ ์ ์ด์ฉ๋๋ ํจ์์ด๋ค. char์ string ์ iterator์ ๊ฐ์ด๋ค. bool not_url_char(char c) { // characters, in addition to alphanumerics, that can appear in a \s-1URL\s0 static const string url_ch = "~;/?:@=&$-_.+!*'(),"; // see whether `c' can appear in a \s-1URL\s0 and return the negative return !(isalnum(c) || find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); } //์ด ์์ ์ ํต์ฌ ํจ์์ด๋ค. string::const_iterator url_beg(string::const_iterator b, string::const_iterator e) { /* b ๋ protocol type์ ์์์์น๋ฅผ ๊ฐ๋ฅดํค๊ฒ๋๋ค. i ๋ :// ์ ์์ ์์น๋ฅผ ๊ฐ๋ฆฌํค๋ ์ญํ ์ ํ๋ค. e ๋ url์ ๋ง์ง๋ง ํ ์คํธ๋ฅผ ๊ฐ๋ฆฌํค๋ ์ญํ ์ ํ๋ค. */ static const string sep = "://"; typedef string::const_iterator iter; // `i' marks where the separator was found iter i = b; // string ์์ sep ์ ๋ฌธ์์ด์ ์์๋ถ๋ถ์ ์ฐพ์์ i์ iterator๋ฅผ ๋์ ํ ํ e์ ๋น๊ตํ์ฌ // ๊ฐ์ง ์์ผ๋ฉด ์คํํ๋ค. (์ฆ, sep๋ฅผ ์ฐพ์์ ๊ฒฝ์ฐ) while ((i = search(i, e, sep.begin(), sep.end())) != e) { // make sure the separator isn't at the beginning or end of the line if (i != b && i + sep.size() != e) { // `beg' marks the beginning of the protocol-name iter beg = i; while (beg != b && isalpha(beg[-1])) --beg; //protocol-typed์ ์์น์ ์กด์ฌํ๋ ๋ฌธ์์ด์ด ์กฐ๊ฑด์ ๋ง์ ๊ฒฝ์ฐ ์์ผ๋ก ํ์นธ์ฉ ์์ง์ด๋ฉด์ ๊ฒ์ฌํ๋ค. // is there at least one appropriate character before and after the separator? if (beg != i && !not_url_char(i[sep.size()])) return beg; } // the separator we found wasn't part of a \s-1URL\s0; advance `i' past this separator i += sep.size(); } return e; }
1.2. 6.2 Comparing grading schemes ¶
Chapter 4.2์์ ์ ์๋ ์ค์๊ฐ์ ์ด์ฉํ ๋ฐฉ์์ผ๋ก ์ฑ์ ์ ๊ณ์ฐํ ๊ฒฝ์ฐ ์
์์ ์ผ๋ก
๊ณผ์ ๋ฌผ์ ์ ์ถํ์ง ์๋ ํ์์ ๋ฐ์์ด ์ผ๋ ค๋๋ค.
๊ณผ์ฐ ์ด๋ ์ ๋๋ก ๊ฒฐ๊ณผ์ ์ํฅ์ ์ฃผ๋์ง ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์ฑํ์ฌ ํ์ธํด๋ณธ๋ค.
๊ณผ์ ๋ฌผ์ ์ ์ถํ์ง ์๋ ํ์์ ๋ฐ์์ด ์ผ๋ ค๋๋ค.
๊ณผ์ฐ ์ด๋ ์ ๋๋ก ๊ฒฐ๊ณผ์ ์ํฅ์ ์ฃผ๋์ง ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์ฑํ์ฌ ํ์ธํด๋ณธ๋ค.
- ์ค์๊ฐ ๋์ ํ๊ท ์ ์ฌ์ฉํ๋ฉฐ, ์ ์ถํ์ง ์์ ๊ณผ์ ์๋ 0์ ์ ์ฃผ๋ ๋ฐฉ์(6.2.3)
- ์ค์ ๋ก ์ ์ถํ ๊ณผ์ ์ ๋ํด์๋ง ์ค์๊ฐ์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ(6.2.4)
- ์ค์๊ฐ์ ์ด์ฉํ์ฌ ํ๊ท ์ ์ด์ฉ(6.2.2)
์ด๋ฅผ ์ํ ์ธ๋ถ์์
- ๋ชจ๋ ํ์์ ๋ ์ฝ๋๋ฅผ ์ฝ์ด๋ค์ฌ, ๋ชจ๋ ๊ณผ์ ๋ฅผ ์ ์ถํ ํ์๋ค๊ณผ ๊ทธ๋ ์ง ์์ ํ์๋ค์ ๊ตฌ๋ถํฉ๋๋ค.(6.2.1)
- ๋ ๊ณ์ฐ๋ฒ์(์์1,2๋ฅผ ์๋ฏธ, 3๋ ํฌํจํด์ผํ ๋ฏ..@,.@) ๊ฐ ๊ทธ๋ฃน์ ๋ชจ๋ ํ์๋ค์๊ฒ ๊ฐ๊ฐ ์ ์ฉํ๊ณ , ๊ฐ ๊ทธ๋ฃน์ ์ค์ ๊ฐ์ ์ถ๋ ฅํฉ๋๋ค.(6.2.2)
1.2.1.1. ๋ชจ๋ ๊ณผ์ ๋ฅผ ์ ์ถํ๋์ง๋ฅผ ํ๋ณํ๋ ํจ์ ¶
~cpp bool did_all_hw(const Student_info& s) { return ((find(s.homework.begin(), s.homework.end(), 0)) == s.homework.end()); }findํจ์๋ ์ฒ์๋๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์์ ์ธ๋ฒ์งธ ์ ๋ฌ์ธ์์ ๊ฐ์ ์ฐพ์ง ๋ชปํ๋ฉด 2๋ฒ์งธ ์ ๋ฌ์ธ์๋ฅผ ๋ฆฌํดํ๋ค. (์ฐพ์ผ๋ฉด ์ฒซ๋ฒ์งธ์ ๋ฌ์ธ์
๋ฅผ ๋ฆฌํดํ๋ค๋๋ฐ...@,.@์๋ชป๋๊ฑฐ ์๋๊ฐ??) ๊ณ ๋ก ์ฐพ์ง๋ชปํ๋ฉด s.homework.begin() == s.homework.begin()์ด ๋๋ฏ๋ก true๋ฅผ ๋ฆฌํดํจ
1.2.1.2. ํ์ ๋ ์ฝ๋๋ฅผ ์ฝ๊ณ ๋ถ๋ฅํ๋ ์ฝ๋ ¶
~cpp vector<Student_info> did, didnt; // read the student records and partition them Student_info student; while (read(cin, student)) { if (did_all_hw(student)) did.push_back(student); else didnt.push_back(student); } // verify that the analyses will show us something if (did.empty()) { cout << "No student did all the homework!" << endl; return 1; } if (didnt.empty()) { cout << "Every student did all the homework!" << endl; return 1; }empty๋ฉค๋ฒํจ์: ์ปจํ ์ด๋๊ฐ ๋น์ด ์์ผ๋ฉด true, ์๋๋ฉด false๋ฆฌํด
1.2.2.1. ์ด๊ธฐ median_analysis ํจ์ ¶
~cpp // ์ด ํจ์๋ ์ ๋๋ก ๋์ํ์ง ์์ต๋๋ค. double median_anlysis(const vector<Strudent_info>& students) { vector<double> grades; transform(students.begin(), students.end(), back_inserter(grades), grade); return median(grades); }transformํจ์: ์ฒ์2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์ ๊ฐ๋ค์ 4๋ฒ์งธํจ์์ ๋์ ๋ฆฌํด๊ฐ์ 3๋ฒ์งธ ์ฃผ์๋ถํฐ ๋ฃ์(?)
๋ฌธ์ ์
- gradeํจ์๋ ์ค๋ฒ๋ผ์ด๋ฉ๋ ํจ์์ด๋ฏ๋ก ์ปดํ์ผ๋ฌ๊ฐ ์ ๋ฌ์ธ์๋ฅผ ํ์
ํ์ง ๋ชปํจ
- ๊ณผ์ ๋ฅผ ํ๋๋ ๋ด์ง ์์ ํ์์ผ๊ฒฝ์ฐ ์ค๋ฅ ๋ฐ์
1.2.2.2. ํด๊ฒฐ์ฑ ¶
์๋ก์ด ํจ์ grade_aux ์์ฑ
median_anlysisํจ์ ์์
~cpp double grade_aux(const Student_info& s) { try{ return grade(s); } catch(domain_error) { return grade(s.midterm, s.final, 0); } }
median_anlysisํจ์ ์์
~cpp double median_anlysis(const vector<Strudent_info>& students) { vector<double> grades; transform(students.begin(), students.end(), back_inserter(grades), grade_aux); //grade๋ฅผ grade_aux๋ก ์์ return median(grades); }
1.2.2.3. write_analysisํจ์ ์์ฑ ¶
~cpp void write_analysis(ostream& out, const string& name, double analysis(const vector<Student_info>&), const vector<Student_infor>& did, const vector<Student_infor>& didnt) { cout << name << ": median(did) = " << analysis(did) << ", median(didnt) = " << analysis(didnt) << endl; }
1.2.2.4. mainํจ์ ¶
~cpp int main() { // students who did and didn't do all their homework vector<Student_info> did, didnt; // read the student records and partition them Student_info student; while (read(cin, student)) { if (did_all_hw(student)) did.push_back(student); else didnt.push_back(student); } // verify that the analyses will show us something if (did.empty()) { cout << "No student did all the homework!" << endl; return 1; } if (didnt.empty()) { cout << "Every student did all the homework!" << endl; return 1; } // do the analyses write_analysis(cout, "median", median_analysis, did, didnt); write_analysis(cout, "average", average_analysis, did, didnt); write_analysis(cout, "median of homework turned in", optimistic_median_analysis, did, didnt); return 0; }
1.2.3. 6.2.3 Grading based on average homework grade ¶
ํ๊ท ๊ณผ์ ์ฑ์ ์ ๊ธฐ๋ฐํ ์ฑ์ ๊ณ์ฐ
1.2.3.1. averageํจ์ ¶
~cpp double average(const vector<double>& v) { return accumulate(v.begin(), v.end(), 0.0) / v.size(); }accumulateํจ์: ์ฒ์2๊ฐ์ ์ ๋ฌ์ธ์๋ฅผ 3๋ฒ์งธ ์ ๋ฌ์ธ์์ ๋์ ์ํด(์ฃผ์ 0.0๋์ 0์ ์ฌ์ฉํ๋ฉด ์ ์๋ก ์ธ์ ์์์ ๋ถ๋ถ์ ๋ฒ๋ ค๋ฒ๋ฆผ)
1.2.3.2. average_gradeํจ์ ¶
~cpp double average_grade(const Student_info& s) { return grade(s.midterm, s.final, average(s.homework)); }
1.2.3.3. average_analysisํจ์ ¶
~cpp double average_analysis(const vector<Student_info>& students) { vector<double> grades; transform(students.begin(), students.end(), back_inserter(gardes), aveage_grade); return median(grades); }
1.2.4.1. optimistic_medianํจ์ ¶
~cpp // s์ 0์ด ์๋ ์์๋ค์ ์ค์ ๊ฐ, ๋ง์ฝ 0์ด ์๋ ์์๊ฐ ํ๋๋ ์๋ค๋ฉด ๊ฒฐ๊ณผ๋ 0์ด ๋ฉ๋๋ค. double optimistic_median(const Student_info& s) { vector<double> nonzero; remove_copy(s.homework.begin(), s.homework.end(), back_inserter(nonzero), 0); if(nozero.empty()) return grade(s.midterm, s.final, 0); else return grade(s.midterm, s.final, median(nonzero)); }
1.3. 6.3 Classifying students, revisited ¶
5์ฅ์์ ์ฌ์ฉํ list๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , vector๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ์ฌ ๊ทธ์ ๋น์ทํ ์ฑ๋ฅ์ ์๊ณ ๋ฆฌ์ฆ
1.3.1. 6.3.1 A two -pass solution ¶
==== extract_fails ํจ์====
remove_ifํจ์: ์ฒ์ 2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์ ๊ฐ๋ค์ค 3๋ฒ์งธ ์ ๋ฌ์ธ์๋ฅผ ๋ง์กฑํ๋ ํจ์๋ฅผ ์ปจํ ์ด๋์ ๋ค๋ก ์ด๋. 3๋ฒ์งธ ์ ๋ฌ์ธ์๋ฅผ ๋ง์กฑํ๋ ๊ฐ์ค ์ ค์ฒซ์งธ๊ฐ์ ๊ฐ๋ฅด์นจ
erase๋ฉค๋ฒํจ์:์ฒ์ 2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์ ๊ฐ์ ์ง์ด๋ค.
~cpp vector<Student_info> extract_fails(vector<Student_info>& students){ vector<Student_info> fail; remove_copy_if(student.begin(), stduents.end(), back_inserter(fail), pgrade); students.erase(remove_if(studens.begin(), students.end(), fgrade), stduents.end()); return fail; }remove_copy_ifํจ์: ์ฒ์ 2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์๊ฐ๋ค์ 4๋ฒ์งธ ์ ๋ฌ์ธ์ ํจ์๋ฅผ ๋ง์กฑํ์ง ์๋ ํจ์๋ง 3๋ฒ์งธ ์ ๋ฌ์ธ์์ ๋ณต์ฌ
remove_ifํจ์: ์ฒ์ 2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์ ๊ฐ๋ค์ค 3๋ฒ์งธ ์ ๋ฌ์ธ์๋ฅผ ๋ง์กฑํ๋ ํจ์๋ฅผ ์ปจํ ์ด๋์ ๋ค๋ก ์ด๋. 3๋ฒ์งธ ์ ๋ฌ์ธ์๋ฅผ ๋ง์กฑํ๋ ๊ฐ์ค ์ ค์ฒซ์งธ๊ฐ์ ๊ฐ๋ฅด์นจ
erase๋ฉค๋ฒํจ์:์ฒ์ 2๊ฐ์ ์ ๋ฌ์ธ์ ๋ฒ์์ ๊ฐ์ ์ง์ด๋ค.
1.3.1.1. pgrade ํจ์ ¶
~cpp bool pgrade(const Student_info& s) { return !fgrade(s); }fgradeํจ์๋ฅผ ๋ฐ์ ์ํจ ํจ์
1.3.2. 6.3.2 A single-pass solution ¶
~cpp vector<Student_info> extract_fails(vector<Student_info>& students) { vector<Student_info>::iterator iter = stable_partition(students.begin(), students.end9), pgrade); vector<Student_info> fail(iter, stduents.end()); students.erase(iter, students.end()); return fail; }stable_partition, partition ์ฐจ์ด์ ?? ์์๋ฅผ ์๋ฐ๊พธ๋ค๋?? @,.@
ํ์ดํผ 2ํจ์๋ ๋ง์กฑํ์ง ์๋ ๊ฐ์ ์ฒซ์งธ ์์น๋ฅผ ๋ฆฌํด
two-pass๋ณด๋ค 2๋ฐฐ๋น ๋ฆ
1.4. 6.4 Algorithms, containers, and iterators ¶
์ปจํ
์ด๋์ ์๊ณ ๋ฆฌ์ฆ์ ๊ด๊ณ
์๊ณ ๋ฆฌ์ฆ์ ์ปจํ ์ด๋ ์์๋ค์ ๋ค๋ฃน๋๋ค. ์ฆ, ์ปจํ ์ด๋๋ฅผ ๋ค๋ฃจ๋ ๊ฒ์ด ์๋๋๋ค.
sort, remove_if, partition ์ ๋ชจ๋ ์์๋ฅผ ์๋ก์ด ์์น๋ก ์ด๋์ํค์ง๋ง, ์ปจํ ์ด๋ ์์ฒด์ ์์ฑ์ธ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ์ง๋ ์๋๋ค.
์ญ์ ๋ฅผ ํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ปจํ ์ด๋์ ๋ฉ์๋๋ฅผ ์ด์ฉํด์ผํ๋ค.
์ปจํ ์ด๋์ ๋ฐ๋ณต์์ ๊ด๊ณ
partiton, remove_if, erase, insert์ ๊ฐ์ ์ฐ์ฐ์ erase๋ ๋ฐ๋ณต์๋ฅผ ๋ฌดํจํ์ํจ๋ค.
๋ฐ๋ผ์ ์ ์ฅ๋ ๋ฐ๋ณต์์ ๊ดํด์ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ฉด์ ์กฐ์ฌํด์ผํ ํ์๊ฐ ์๋ค.
๋ฐ๋ผ์ ์๊ธฐ์ ๊ฐ์ ํจ์๋ฅผ ์ด์ฉํ ๋ค์๋ ์ด์ ์ ํ ๋น๋ ๋ฐ๋ณต์๊ฐ ์ ํจํ๋ค๊ณ ๋ณด๊ณ ํ๋ก๊ทธ๋จ์ ๋ก์ง์ ๋ง๋ค์ด์๋ ์๋๋ค.
์๊ณ ๋ฆฌ์ฆ์ ์ปจํ ์ด๋ ์์๋ค์ ๋ค๋ฃน๋๋ค. ์ฆ, ์ปจํ ์ด๋๋ฅผ ๋ค๋ฃจ๋ ๊ฒ์ด ์๋๋๋ค.
sort, remove_if, partition ์ ๋ชจ๋ ์์๋ฅผ ์๋ก์ด ์์น๋ก ์ด๋์ํค์ง๋ง, ์ปจํ ์ด๋ ์์ฒด์ ์์ฑ์ธ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ์ง๋ ์๋๋ค.
์ญ์ ๋ฅผ ํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ปจํ ์ด๋์ ๋ฉ์๋๋ฅผ ์ด์ฉํด์ผํ๋ค.
~cpp v.erase(remove_if(students.begin(), students.end(), fgrade), students.end());
์ปจํ ์ด๋์ ๋ฐ๋ณต์์ ๊ด๊ณ
partiton, remove_if, erase, insert์ ๊ฐ์ ์ฐ์ฐ์ erase๋ ๋ฐ๋ณต์๋ฅผ ๋ฌดํจํ์ํจ๋ค.
๋ฐ๋ผ์ ์ ์ฅ๋ ๋ฐ๋ณต์์ ๊ดํด์ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ฉด์ ์กฐ์ฌํด์ผํ ํ์๊ฐ ์๋ค.
๋ฐ๋ผ์ ์๊ธฐ์ ๊ฐ์ ํจ์๋ฅผ ์ด์ฉํ ๋ค์๋ ์ด์ ์ ํ ๋น๋ ๋ฐ๋ณต์๊ฐ ์ ํจํ๋ค๊ณ ๋ณด๊ณ ํ๋ก๊ทธ๋จ์ ๋ก์ง์ ๋ง๋ค์ด์๋ ์๋๋ค.