本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
cs:programming:cpp:cpp_primer:answers:chpt_11 [2024/01/14 13:46] – 移除 - 外部编辑 (未知日期) 127.0.0.1 | cs:programming:cpp:cpp_primer:answers:chpt_11 [2024/01/14 13:46] (当前版本) – ↷ 页面programming:cpp:cpp_primer:answers:chpt_11被移动至cs:programming:cpp:cpp_primer:answers:chpt_11 codinghare | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======Chapter.11====== | ||
+ | Answers for chapter 11 | ||
+ | ---- | ||
+ | ====Ex.11.1-11.10==== | ||
+ | ==ex.11.1== | ||
+ | > | ||
+ | There are three main difference in: | ||
+ | * **how elements are structured**: | ||
+ | * Maps are associated containers. They contain elements that have key-value pairs types. | ||
+ | * Vectors are sequential containers. They hold elements of normal type. | ||
+ | * **how elements are stored**: | ||
+ | * Map elements are organized through either an order or a hash table. | ||
+ | * Vector elements are stored in memory as a sequence. | ||
+ | * **advantange**: | ||
+ | * The map performs much better when looking up elements, as it finds contents using a key. | ||
+ | * The vector has better performance in random access, however, since its elements are associated with the index, looking up an element in one sometimes requires traversal. | ||
+ | ==ex.11.2== | ||
+ | >Give an example of when each of list, vector, deque, map, and set might be most useful. | ||
+ | * list: A place where elements need to be inserted and removed in the middle of it., for example, a file in file system. | ||
+ | * vector: Places where you need frequent to do random_access. | ||
+ | * deque: FIFO | ||
+ | * map: dictionary | ||
+ | * set: keyword black list. | ||
+ | ==ex.11.3== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.4== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.5== | ||
+ | > | ||
+ | The map element type is key-value pair and a set element contains only a key. Use map if there is any value associated with key. | ||
+ | ==ex.11.6== | ||
+ | > | ||
+ | * A set is an associated container, holding ordered and unique elements. | ||
+ | * A list is a sequential container. Its element is neither unique nor ordered. | ||
+ | A set is useful when you want better performance when looking up elements. However, Editing the contents of a set may not be a good idea since it might break the order of the elements. If this is your intent, use a list. | ||
+ | ==ex.11.7== | ||
+ | >Define a map for which the key is the family’s last name and the value is a vector of the children’s names. Write code to add new families and to add new children to an existing family. | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.8== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.9== | ||
+ | > | ||
+ | <code cpp> | ||
+ | map< | ||
+ | </ | ||
+ | ==ex.11.10== | ||
+ | > | ||
+ | The '' | ||
+ | ====Ex.11.11-11.20==== | ||
+ | ==ex.11.11== | ||
+ | > | ||
+ | <code cpp> | ||
+ | using comp_T = bool (*)(const Sales_data &, const Sales_data & | ||
+ | std:: | ||
+ | </ | ||
+ | ==ex.11.12== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.13== | ||
+ | > | ||
+ | In my opinion, the list return is the best choice. One of the primary benefits of using a list return is that it checks whether the return type matches the type of the pair, since narrowing conversion is not allowed during list initialization. \\ \\ | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.14== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.15== | ||
+ | > | ||
+ | |||
+ | * **mapped_type**: | ||
+ | * **key_type**: | ||
+ | * **value_type** : '' | ||
+ | ==ex.11.16== | ||
+ | > | ||
+ | <code cpp> | ||
+ | map_it-> | ||
+ | </ | ||
+ | ==ex.11.17== | ||
+ | > | ||
+ | <code cpp> | ||
+ | //legal, using the multiset as a destination | ||
+ | copy(v.begin(), | ||
+ | //illegal, push_back() is not avaliable for a multiset | ||
+ | copy(v.begin(), | ||
+ | //legal, using the multiset as an source sequence | ||
+ | copy(c.begin(), | ||
+ | //legal, equivalent to the above | ||
+ | copy(c.begin(), | ||
+ | </ | ||
+ | ==ex.11.18== | ||
+ | > | ||
+ | <code cpp> | ||
+ | std:: | ||
+ | </ | ||
+ | ==ex.11.19== | ||
+ | > | ||
+ | <code cpp> | ||
+ | using comp_T = bool (*)(const Sales_data &, const Sales_data & | ||
+ | std:: | ||
+ | std:: | ||
+ | </ | ||
+ | ==ex.11.20== | ||
+ | > | ||
+ | Compared to the iterator version, I prefer the subscript version because we can rely on the unique key features for the word counting, which is more understandable, | ||
+ | CODE: [[https:// | ||
+ | ====Ex.11.21-11.30==== | ||
+ | ==ex.11.21== | ||
+ | > | ||
+ | <code cpp> | ||
+ | while (cin >> word) | ||
+ | ++word_count.insert({word, | ||
+ | </ | ||
+ | According to the operator precedence, the statement in the loop will be evaluate as the following order: | ||
+ | - insert function cast: the element pair '' | ||
+ | - member access: the member call '' | ||
+ | - increament: change the value of '' | ||
+ | As the result, the program actually does the same thing as the previous word counting program. | ||
+ | ==ex.11.22== | ||
+ | > | ||
+ | <code cpp> | ||
+ | std:: | ||
+ | std:: | ||
+ | </ | ||
+ | ==ex.11.23== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.24== | ||
+ | > | ||
+ | <code cpp> | ||
+ | map<int, int> m; | ||
+ | m[0] = 1; | ||
+ | </ | ||
+ | It inserts a key-value pair element '' | ||
+ | ==ex.11.25== | ||
+ | > | ||
+ | <code cpp> | ||
+ | vector< | ||
+ | v[0] = 1; | ||
+ | </ | ||
+ | It is an undefined behavior since '' | ||
+ | ==ex.11.26== | ||
+ | >What type can be used to subscript a map? What type does the subscript operator return? Give a concrete example—that is, define a map and then write the types that can be used to subscript the map and the type that would be returned from the subscript operator. | ||
+ | <color # | ||
+ | A // | ||
+ | \\ \\ <color # | ||
+ | A // | ||
+ | Beacuse a // | ||
+ | Test code: [[https:// | ||
+ | \\ \\ | ||
+ | Result: | ||
+ | <code cpp> | ||
+ | i NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE | ||
+ | </ | ||
+ | ==ex.11.27== | ||
+ | > | ||
+ | The //count// can be used if the numbers of elements will be useful, for example, when determining loop times.The //find// may be used to find out if a key is present in the container. | ||
+ | ==ex.11.28== | ||
+ | >Define and initialize a variable to hold the result of calling find on a map from string to vector of int. | ||
+ | <code cpp> | ||
+ | //full type of the iterator that returned by find member | ||
+ | std:: | ||
+ | //or you may use auto instead | ||
+ | auto iter = msv.find(key); | ||
+ | </ | ||
+ | Test code: [[https:// | ||
+ | ==ex.11.29== | ||
+ | > | ||
+ | |||
+ | * // | ||
+ | * // | ||
+ | * // | ||
+ | ==ex.11.30== | ||
+ | > | ||
+ | In the program: | ||
+ | * '' | ||
+ | * The first iterator of '' | ||
+ | Therefore, the statment | ||
+ | <code cpp> | ||
+ | pos.first-> | ||
+ | </ | ||
+ | is actually accessing the authors' | ||
+ | ====Ex.11.31-11.38==== | ||
+ | ==ex.11.31== | ||
+ | > | ||
+ | |||
+ | **//Note: I am not sure what the " | ||
+ | CODE: [[https:// | ||
+ | ==ex.11.32== | ||
+ | > | ||
+ | |||
+ | * using set to store the book list:[[https:// | ||
+ | * using map< | ||
+ | ==ex.11.33== | ||
+ | > | ||
+ | CODE: [[https:// | ||
+ | <WRAP center round alert 100%> | ||
+ | Data files should **NOT** be created in Windows and read or written in Linux and vice versa. Different systems have different ways of interpreting the file. My case was that I created the file in Windows and then read it in Ubuntu. That resulted in print errors. The problem could be solved by converting the file using the **dos2unix** program. | ||
+ | </ | ||
+ | ==ex.11.34== | ||
+ | > | ||
+ | With subscript, if the keyword doesn' | ||
+ | ==ex.11.35== | ||
+ | > | ||
+ | <code cpp> | ||
+ | trans_map[key] = value.substr(1); | ||
+ | trans_map.insert({key, | ||
+ | </ | ||
+ | // | ||
+ | * subscript will relplace the old relationship with the new one. | ||
+ | * // | ||
+ | ==ex.11.36== | ||
+ | > | ||
+ | The program will report a // | ||
+ | Test result: | ||
+ | < | ||
+ | //changed y+space+why to y+space | ||
+ | //output | ||
+ | terminate called after throwing an instance of ' | ||
+ | what(): | ||
+ | </ | ||
+ | ==ex.11.37== | ||
+ | > | ||
+ | |||
+ | // | ||
+ | * better performence with appropriate hash function | ||
+ | * better performence when ordering cost is prohibitive | ||
+ | //**Ordered version: | ||
+ | * Doesn' | ||
+ | * must have if ordered elements is needed | ||
+ | * easy to use | ||
+ | ==ex.11.38== | ||
+ | > | ||
+ | CODE: [[https:// |