本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
cs:programming:cpp:cpp_primer:answers:chpt_10 [2024/01/14 13:46] – 移除 - 外部编辑 (未知日期) 127.0.0.1 | cs:programming:cpp:cpp_primer:answers:chpt_10 [2024/01/14 13:46] (当前版本) – ↷ 页面programming:cpp:cpp_primer:answers:chpt_10被移动至cs:programming:cpp:cpp_primer:answers:chpt_10 codinghare | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======Chapter.10====== | ||
+ | Answers for chapter 10 | ||
+ | ---- | ||
+ | ====Ex10.1-10.10==== | ||
+ | ==ex.10.1== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.2== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.3== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.4== | ||
+ | > | ||
+ | Well, The calculation will be performed as long as the elements can be added to the third parameter. However, the calculation will truncate all " | ||
+ | <code cpp> | ||
+ | vector< | ||
+ | accumulate(vd.cbegin(), | ||
+ | </ | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.5== | ||
+ | > | ||
+ | As C-string does not overload the '' | ||
+ | |||
+ | CODE: [[https:// | ||
+ | |||
+ | <code bash> | ||
+ | #output | ||
+ | root@Hare:/ | ||
+ | 1 #two vectors refer to the same c-string | ||
+ | 0 #two vectors refer to the different c-strings but same content | ||
+ | </ | ||
+ | ==ex.10.6== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.7== | ||
+ | > | ||
+ | <code cpp> | ||
+ | /* (a) */ | ||
+ | vector< | ||
+ | while (cin >> i) | ||
+ | lst.push_back(i); | ||
+ | copy(lst.cbegin(), | ||
+ | </ | ||
+ | It is illegal to copy elements to an empty vector since generic algorithms do not check the containers. A back_inserter will be a better choice: | ||
+ | <code cpp> | ||
+ | vector< | ||
+ | while (std::cin >> i) | ||
+ | lst.push_back(i); | ||
+ | copy(lst.cbegin(), | ||
+ | </ | ||
+ | <code cpp> | ||
+ | /* (b) */ | ||
+ | vector< | ||
+ | vector< | ||
+ | vec.reserve(10); | ||
+ | fill_n(vec.begin(), | ||
+ | </ | ||
+ | A problem is that the // | ||
+ | <code cpp> | ||
+ | vector< | ||
+ | vec.resize(10, | ||
+ | fill_n(vec.begin(), | ||
+ | </ | ||
+ | ==ex.10.8== | ||
+ | > | ||
+ | In reality, algorithms only work with the iterator in this case. Two steps are actually involved here: | ||
+ | * The algorithm created an iterator and bound it to the container. | ||
+ | * A new element was added to the container by the iterator itself using the container memeber // | ||
+ | Obviously, the operation of algorithms is limited to the iterators. From this perspective, | ||
+ | ==ex.10.9== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.10== | ||
+ | > | ||
+ | Algorithms are designed to operate iterators only. In reality, they aren't generic if they could do the container size change operation, | ||
+ | ====Ex.10.11-10.20==== | ||
+ | ==ex.10.11== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.12== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.13== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.14== | ||
+ | > | ||
+ | <code cpp> | ||
+ | [](int a, int b) { return a + b; } ; | ||
+ | [](int a, int b) -> int { return a + b; }; //with explicit return type | ||
+ | </ | ||
+ | ==ex.10.15== | ||
+ | > | ||
+ | |||
+ | <code cpp> | ||
+ | void | ||
+ | sum(const int i) { | ||
+ | auto i_sum = [i](int ib) { return i + ib; }; | ||
+ | } | ||
+ | </ | ||
+ | ==ex.10.16== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.17== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.18== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.19== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.20== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ====Ex.10.21-10.30==== | ||
+ | ==ex.10.21== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.22== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.23== | ||
+ | >How many arguments does bind take? | ||
+ | //bind()// will take 1 callable, plus the number of the placeholder, | ||
+ | ==ex.10.24== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.25== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.26== | ||
+ | > | ||
+ | |||
+ | * inserter: It takes an iterator and uses it to denote the initial insert position. Assignments to the inserter will insert an element before the insert position. call the // | ||
+ | * back_inserter: | ||
+ | * front_inserter: | ||
+ | ==ex.10.27== | ||
+ | > | ||
+ | |||
+ | CODE: [[https:// | ||
+ | ==ex.10.28== | ||
+ | > | ||
+ | |||
+ | CODE: [[https:// | ||
+ | ==ex.10.29== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.30== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ====Ex.10.31-10.40==== | ||
+ | ==ex.10.31== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.32== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.33== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.34== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.35== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.36== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.37== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.10.38== | ||
+ | > | ||
+ | |||
+ | * Inuput Iterator: single pass read, '' | ||
+ | * Output Iterator: | ||
+ | * Forward iterator: all above plus multi-ass r/w | ||
+ | * Bidirectional Iterator: all above plus backward read & write ('' | ||
+ | * Random access Iterator: all ablove plus '' | ||
+ | ==ex.10.39== | ||
+ | > | ||
+ | * list iterators: Bidirectional iterator | ||
+ | * vector iterators: Random Access iterator | ||
+ | ==ex.10.40== | ||
+ | > | ||
+ | <code cpp> | ||
+ | //std::copy needs an output_iterator | ||
+ | template< | ||
+ | OutputIt copy(InputIt first, InputIt last, | ||
+ | OutputIt d_first) | ||
+ | { | ||
+ | while (first != last) { | ||
+ | *d_first++ = *first++; | ||
+ | } | ||
+ | return d_first; | ||
+ | } | ||
+ | |||
+ | // | ||
+ | template< | ||
+ | constexpr // since C++20 | ||
+ | void reverse(BidirIt first, BidirIt last) | ||
+ | { | ||
+ | using iter_cat = typename std:: | ||
+ | | ||
+ | if constexpr (std:: | ||
+ | if (first == last) | ||
+ | return; | ||
+ | for (--last; first < last; (void)++first, | ||
+ | std:: | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | while ((first != last) && (first != --last)) { | ||
+ | std:: | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | template< | ||
+ | ForwardIt unique(ForwardIt first, ForwardIt last) | ||
+ | { | ||
+ | if (first == last) | ||
+ | return last; | ||
+ | |||
+ | ForwardIt result = first; | ||
+ | while (++first != last) { | ||
+ | if (!(*result == *first) && ++result != first) { | ||
+ | *result = std:: | ||
+ | } | ||
+ | } | ||
+ | return ++result; | ||
+ | } | ||
+ | </ | ||
+ | ====Ex.10.41-10.42==== | ||
+ | ==ex.10.41== | ||
+ | > | ||
+ | <code cpp> | ||
+ | //replace all old_val in [beg, end) to new_val | ||
+ | replace(beg, | ||
+ | //replace all elements that satisfied pred to new_val | ||
+ | replace_if(beg, | ||
+ | //replace all old_val in [beg, end) to new_val, then write the sequence to dest | ||
+ | replace_copy(beg, | ||
+ | //replace all elements that satisfied pred to new_val, then write the sequence to dest | ||
+ | replace_copy_if(beg, | ||
+ | </ | ||
+ | ==ex.10.42== | ||
+ | > | ||
+ | CODE: [[https:// | ||