| 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.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.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.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된 반복를 무다.
따라 된 반복 관 로그래밍 면 가 다.
따라 기 같 를 뒤는 당된 반복가 다고 보고 로그램 로 만들는 된다.










