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

    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.0342 sec