U E D R , A S I H C RSS

AcceleratedC++/Chapter6

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

  • 5μž₯μ—μ„œ κ³΅λΆ€ν•œ 것 쀑에 μ£Όμ–΄μ§„ string을 곡백을 κΈ°μ€μœΌλ‘œ μž˜λΌμ„œ, vector에닀 넣은 λ‹€μŒ λ¦¬ν„΄ν•΄μ£ΌλŠ” ν•¨μˆ˜κ°€ μžˆμ—ˆλ‹€.(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;
    }
      
  • 훨씬 μ•Œμ•„λ³΄κΈ° μ‰¬μ›Œμ‘Œλ‹€. 5μž₯의 split은 find_ifλ§ˆλ‹€ μ—΄μ‹¬νžˆ 루프λ₯Ό λŒλ Έμ—ˆλ‹€. 이제 μ°¨κ·Όμ°¨κ·Ό μ‚΄νŽ΄λ³΄μž.
  • find_if의 인자λ₯Ό 보면, μ•žμ˜ λ‘κ°œμ˜ μΈμžλŠ” λ²”μœ„λ₯Ό μ˜λ―Έν•œλ‹€. 첫인자~λ‘λ²ˆμ§ΈμΈμž 말이닀. λ§ˆμ§€λ§‰ μΈμžλŠ” boolν˜•μ„ λ¦¬ν„΄ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ„£μ–΄μ€λ‹€. 즉 predicater이닀. 그러면 find_ifλŠ” μ£Όμ–΄μ§„ λ²”μœ„ λ‚΄μ—μ„œ predicatorλ₯Ό λ§Œμ‘±ν•˜λŠ” λΆ€λΆ„μ˜ 반볡자λ₯Ό 리턴해 μ€λ‹€.
  • isspaceλŠ” ν‘œμ€ λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μ§€μ›ν•˜λŠ” ν•¨μˆ˜μž„μ—λ‹€ λΆˆκ΅¬ν•˜κ³ , μ™œ λ”°λ‘œ λ§Œλ“€μ—ˆμ„κΉŒ? λ°”λ‘œ isspaceλŠ” μ—¬λŸ¬ μ–Έμ–΄ λ²„μ ΌμœΌλ‘œ μ˜€λ²„λ‘œλ”© λ˜μ–΄ 있기 λ•Œλ¬Έμ΄λ‹€. ν…œν”Œλ¦Ώ ν•¨μˆ˜μ˜ 인자둜 μ˜€λ²„λ‘œλ”©λœ ν•¨μˆ˜λ₯Ό λ„˜κ²¨μ£ΌλŠ” 것은 쉽지 μ•Šλ‹€. μ–΄λ–€ 버젼인지 μ•Œμˆ˜κ°€ μ—†κΈ° λ•Œλ¬Έμ΄λ‹€. 이것이 μš°λ¦¬κ°€ isspace역할을 ν•˜λŠ” ν•¨μˆ˜λ₯Ό μƒˆλ‘œ λ§Œλ“  μ΄μœ λ‹€.
  • 5μž₯μ—μ„œλŠ” string(i,j) λŒ€μ‹ μ—, substrμ΄λΌλŠ” ν•¨μˆ˜λ₯Ό μ΄μš©ν–ˆμ—ˆλŠ”λ°, μ΄λ²ˆμ— μ“°μ§€ μ•Šμ€ μ΄μœ λŠ” substr은 반볡자λ₯Ό 인자둜 λ°›μ§€ μ•ŠκΈ° 떄문이닀.
  • λ˜ν•œ μ œλ„€λ¦­ μ•Œκ³ λ¦¬μ¦˜μ€ end()λ₯Ό κΉ”λ”ν•˜κ²Œ μ²˜λ¦¬ν•΄μ€λ‹€. μš°λ¦¬κ°€ μ‹ κ²½μ•ˆμ¨λ„ λœλ‹€λŠ” 것이닀.

  • 1.1.2. 6.1.2 Palindromes

  • Palindromeμ΄λž€ μ•žμ—μ„œλΆ€ν„° 읽어도 λ’€μ—μ„œλΆ€ν„° 읽어도 λ˜‘κ°™μ€ 단어λ₯Ό μ˜λ―Έν•œλ‹€.
    ~cpp 
    bool isPalindrome(const string& s)
    {
     return equal(s.begin(), s.end(), s.rbegin());
    }
      
  • μ°Έ κΉ”λ”ν•˜λ‹€. rbegin()은 μ—­μ‹œ 반볡자λ₯Ό λ¦¬ν„΄ν•΄μ£ΌλŠ” ν•¨μˆ˜μ΄λ‹€. 거꾸둜 κ°„λ‹€. equalν•¨μˆ˜λŠ” λ‘κ°œμ˜ ꡬ간을 λΉ„κ΅ν•΄μ„œ 같을 경우 bool 의 true 값을 λ¦¬ν„΄ν•œλ‹€. νŒŒλΌλ§€ν„°λ‘œ 첫번째 κ΅¬κ°„μ˜ μ‹œμž‘κ³Ό 끝, λ‘λ²ˆμ§Έ κ΅¬κ°„μ˜ μ‹œμž‘ iterator λ₯Ό λ°›λŠ”λ‹€. λ‘λ²ˆμ§Έ κ΅¬κ°„μ˜ 끝을 λ‚˜νƒ€λ‚΄λŠ” iterator λ₯Ό μš”κ΅¬ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ”, λ‘κ°œμ˜ κ΅¬κ°„μ˜ 길이가 κ°™λ‹€κ³  κ°€μ •ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€. μ΄λŠ” equal ν•¨μˆ˜μ˜ λ™μž‘μ„ 생각해 λ³Όλ•Œ ν•©λ‹Ήν•œ μ²˜λ¦¬μ΄λ‹€.
  • 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;
    }
      
  • 이 μ˜ˆμ œλŠ” string STL을 μ΄μš©ν•΄μ„œ λ¬Έμžμ—΄μ„ κ²€μƒ‰ν•˜κ³ , μš°λ¦¬μ—κ²Œ λ§žλŠ” 정보λ₯Ό κ°€κ³΅ν•˜λŠ” 것이 λͺ©μ μ΄λ‹€.
  • protocol-type://domainname 의 ν˜•νƒœλ₯Ό κ²€μ‚¬ν•œλ‹€.
  • search(b, e, b2, e3) [b, e)의 λ¬Έμžμ—΄ μ‹œν€€μŠ€μ—μ„œ [b2, e3) λ¬Έμžμ—΄ μ‹œν€€μŠ€λ₯Ό μ°ΎλŠ”λ‹€.
  • find(b, e, t) λ¬Έμžμ—΄ μ‹œν€€μŠ€ [b, e)μ—μ„œ κ°’ tλ₯Ό μ°ΎλŠ”λ‹€.
  • find_if(b, e, p) λ¬Έμžμ—΄ μ‹œν€€μŠ€ [b, e)μ—μ„œ ν•¨μˆ˜ pλ₯Ό 톡해 ν…ŒμŠ€νŠΈν•œλ‹€.
  • static μŠ€ν† λ¦¬μ§€ μ§€μ •μžλŠ” ν•¨μˆ˜μ˜ 졜초 μƒμ„±μ‹œ μ €μž₯곡간에 단 ν•œλ²ˆλ§Œ ν• λ‹Ήλ˜λ©°, λ‹€μ‹œ ν˜ΈμΆœμ„ ν•˜μ—¬λ„ μƒˆλ‘œ ν• λ‹Ήλ˜μ§€ μ•ŠλŠ”λ‹€.

  • 1.2. 6.2 Comparing grading schemes

    Chapter 4.2μ—μ„œ μ œμ‹œλœ 쀑앙값을 μ΄μš©ν•œ λ°©μ‹μœΌλ‘œ 성적을 계산할 경우 μ•…μ˜μ μœΌλ‘œ
    κ³Όμ œλ¬Όμ„ μ œμΆœν•˜μ§€ μ•ŠλŠ” ν•™μƒμ˜ λ°œμƒμ΄ μ—Όλ €λœλ‹€.
    κ³Όμ—° μ–΄λŠ μ •λ„λ‘œ 결과에 영ν–₯을 μ£ΌλŠ”μ§€ μ‹€μ œλ‘œ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ—¬ 확인해본닀.
    1. 쀑앙값 λŒ€μ‹  평균을 μ‚¬μš©ν•˜λ©°, μ œμΆœν•˜μ§€ μ•Šμ€ κ³Όμ œμ—λŠ” 0점을 μ£ΌλŠ” 방식(6.2.3)
    2. μ‹€μ œλ‘œ μ œμΆœν•œ κ³Όμ œμ— λŒ€ν•΄μ„œλ§Œ 쀑앙값을 μ μš©ν•˜λŠ” 방법(6.2.4)
    3. 쀑앙값을 μ΄μš©ν•˜μ—¬ 평균을 이용(6.2.2)

    이λ₯Ό μœ„ν•œ μ„ΈλΆ€μž‘μ—…
    1. λͺ¨λ“  ν•™μƒμ˜ λ ˆμ½”λ“œλ₯Ό 읽어듀여, λͺ¨λ“  과제λ₯Ό μ œμΆœν•œ 학생듀과 κ·Έλ ‡μ§€ μ•Šμ€ 학생듀을 κ΅¬λΆ„ν•©λ‹ˆλ‹€.(6.2.1)
    2. 두 계산법을(μœ„μ˜1,2λ₯Ό 의미, 3도 포함해야할듯..@,.@) 각 그룹의 λͺ¨λ“  ν•™μƒλ“€μ—κ²Œ 각각 μ μš©ν•˜κ³ , 각 그룹의 쀑앙 값을 좜λ ₯ν•©λ‹ˆλ‹€.(6.2.2)

    1.2.1. 6.2.1 Working with student records


    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. 6.2.2 Analyzing the grades

    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번째 μ£Όμ†ŒλΆ€ν„° λ„£μŒ(?)
    문제점
    1. gradeν•¨μˆ˜λŠ” μ˜€λ²„λΌμ΄λ”©λœ ν•¨μˆ˜μ΄λ€λ‘œ μ»΄νŒŒμΌλŸ¬κ°€ μ „λ‹¬μΈμžλ₯Ό νŒŒμ•…ν•˜μ§€ λͺ»ν•¨
    2. 과제λ₯Ό ν•˜λ‚˜λ„ λ‚΄μ§€ μ•Šμ€ ν•™μƒμΌκ²½μš° 였λ₯˜ λ°œμƒ

    1.2.2.2. ν•΄κ²°μ±…

    μƒˆλ‘œμš΄ ν•¨μˆ˜ grade_aux μž‘μ„±
    ~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을 μ‚¬μš©ν•˜λ©΄ μ •μˆ˜λ‘œ 인식 μ†Œμˆ˜μ  뢀뢄을 버렀버림)
  • μ΄ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” <numeric>을 include ν•΄μ€˜μ•Ό ν•œλ‹€.
  • 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. 6.2.4 Median of the completed homework

    μ™„λ£Œλœ 과제의 쀑앙 κ°’

    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.2.4.2. optimistic_median_analysisν•¨μˆ˜

    μˆ™μ œλΌλ„€μš”..γ…Žγ…Ž
    AcceleratedC++/Chapter6/Code

    1.3. 6.3 Classifying students, revisited

    5μž₯μ—μ„œ μ‚¬μš©ν•œ listλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³ , vectorλ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ—¬ 그와 λΉ„μŠ·ν•œ μ„±λŠ₯의 μ•Œκ³ λ¦¬μ¦˜

    1.3.1. 6.3.1 A two -pass solution

    ==== extract_fails ν•¨μˆ˜====
    ~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 은 λͺ¨λ‘ μš”μ†Œλ₯Ό μƒˆλ‘œμš΄ μœ„μΉ˜λ‘œ μ΄λ™μ‹œν‚€μ§€λ§Œ, μ»¨ν…Œμ΄λ„ˆ 자체의 속성인 크기λ₯Ό λ³€κ²½ν•˜μ§€λŠ” μ•ŠλŠ”λ‹€.
    μ‚­μ œλ₯Ό ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ‹€μŒκ³Ό 같은 λ°©μ‹μœΌλ‘œ μ»¨ν…Œμ΄λ„ˆμ˜ λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ•Όν•œλ‹€.
    ~cpp v.erase(remove_if(students.begin(), students.end(), fgrade), students.end());

    μ»¨ν…Œμ΄λ„ˆμ™€ 반볡자의 관계
    partiton, remove_if, erase, insert와 같은 연산은 erase된 반볡자λ₯Ό λ¬΄νš¨ν™”μ‹œν‚¨λ‹€.
    λ”°λΌμ„œ μ €μž₯된 λ°˜λ³΅μžμ— κ΄€ν•΄μ„œ ν”„λ‘œκ·Έλž˜λ°μ„ ν•˜λ©΄μ„œ 쑰심해야할 ν•„μš”κ°€ μžˆλ‹€.
    λ”°λΌμ„œ 상기와 같은 ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ λ’€μ—λŠ” 이전에 ν• λ‹Ήλœ λ°˜λ³΅μžκ°€ μœ νš¨ν•˜λ‹€κ³  보고 ν”„λ‘œκ·Έλž¨μ˜ λ‘œμ§μ„ λ§Œλ“€μ–΄μ„œλŠ” μ•ˆλœλ‹€.

    Valid XHTML 1.0! Valid CSS! powered by MoniWiki
    last modified 2021-02-07 05:22:25
    Processing time 0.0369 sec