Valid STL notes

Source: Internet
Author: User
# Estl 50th: familiar with STL-related web sites. Three: www.sgi.com/tech/stlw.www.stlport.org and www.boost.org.
# 49th pieces of estl: Learn to analyze compiler diagnostic information related to STL. Well, the first step is to replace the big method, and then introduce the container, insert iterator, binder, output iterator or Algorithm What are the common mistakes.
# Estl 48th: always contains (# include) the correct header file. The C ++ standard does not specify the mutual inclusion relationship between header files, so different STL implementations are different. Remember that containers are basically declared in files with the same name. The algorithms are algo... And num.... The iterator is in iterator, and the function and adapter are in functional.
# Estl 47th: Avoid "Write-only" Code . That is, code that is easy to write but hard to read and understand, such as calling a function 12 times in a row, 10 of which are different from each other.
# Estl 46th: Consider using function objects rather than functions as parameters of STL algorithms. Well, because function objects are easier to enable the compiler to inline, the speed will be faster. From the extent that the code is accepted by the compiler, they are more stable and reliable.
# Estl 45th: correctly distinguishes count, find, binary_search, lower_bound, upper_bound, and pai_range. Well, this is related to whether the input range has been sorted, to your purpose, and to the container. In short, it is complicated. You have to check this section twice.
Googollee I always think that this should be done by reload RT @ laiyonghao: # estl 44th: Container member functions take precedence over algorithms with the same name. Cause: It is faster and more closely integrated with the container, and can be more consistent with the behavior of the container.
# 44th pieces of estl: Container member functions take precedence over algorithms with the same name. Cause: It is faster and more closely integrated with the container, and can be more consistent with the behavior of the container.
# 43rd pieces of estl: algorithm calls take precedence over handwritten loops. Three reasons: higher efficiency, less error-prone, and better maintainability.
# Estl 42nd: Make sure that less <t> and operator <t> have the same semantics. Truth is always so plain ...... What else can I say?
# Estl 41st: Understand ptr_fun, mem_fun and mem_fun_ref. Cough, think of the headache when I understood. * And-> ......
# Estl 40th: If a class is a function Child, it should be made compatible. Because the STL function adapter requires some special type definitions, such as argument_type and result_type. It is a good solution to compile a function child to inherit from unary_function or binary_function.
# Estl 39th: Make sure that the discriminant is "pure function ". A pure function is a function whose return value only depends on its parameters. It is estimated that many people have crossed the ship in this gutter, hahaha.
# 38th pieces of estl: design the function subclass following the principle of passing by value. In other words, they are made small and standalone. The significance of this clause is to propose a solution for the problems brought about by the sub-function of the weight and polymorphism.
# 37th pieces of estl: Use accumulate or for_each for Interval statistics. The code of the former is clearer, and the important thing is that they accept different sub-Requirements for functions.
# Estl 36th: Understand the correct implementation of the copy_if algorithm. A correct implementation is provided in this Article. Note that function sub-functions that cannot be used are compatible, and STL algorithms are all like this.
# 35th estl records: mismatch or lexicographical_compare is used to perform simple case-insensitive string comparison.
# Estl 34th: Find out which algorithm requires the sorting interval as the parameter. Well, many STL algorithms have intervals to be sorted. If the real parameters are not the same, performance will decrease. If the real parameters are heavy, logic errors will occur.
# Estl 33rd: Be careful when using the Remove Algorithm for containers containing pointers. As CPP Program Personnel, always be vigilant against resource leaks. Boost: shared_ptr is a good choice.
# Estl 32nd: if you really need to delete an element, you need to call erase after removing this type of algorithm. Well, it is about the origin of erase-Remove's usage. In addition, the method for deleting elements from different containers is different.
# Estl 31st: Learn about various sorting-related options. In short, this article introduces the usage and Application of partition/stable_partition/nth_element/partial_sort/sort/stable_sort.
The book is half done here. Next is the major play: algorithms. # Estl 30th: ensure that the target interval is large enough. In particular, you must note that you can use resize to increase the coverage. Back_inserter, front _..., inserter, and ostream_iterator are used for insertion.
# Estl 29th: Use istreambuf_iterator for character-by-character input. Istream_iterator will skip the blank space, and then introduce... buf ..., the performance of the latter will be better. Well, the string in the example uses the interval constructor, and another recommended usage.
# Estl 28th: correctly understand the iterator usage generated by the Base () member function of reverse_itrator. In short, the iterator returned by base () has an offset. Pay attention to the insertion and deletion operations. This article introduces the usage of V. Erase (++ RI). Base.
# Estl 27th: use distance and advance to convert the const_iterator of the container to iterator. In fact, this is an extension of the previous article. It describes the corresponding conversion methods and precautions. For example, explicitly specifying the distance type parameter as const _... to avoid compiler inference.
I went on reading a little book at home over the weekend and pushed it up. # Estl 26th: iterator takes precedence over const_iterator, reverse _... And const_reverse .... Because insert/Erase and other functions are required and compared with each other, iterator can be converted flexibly.

==================== Half of the split line ==============================
# Estl 25th: familiar with non-standard hash containers. Well, these things are non-standard. After C ++ 0x comes out, there will be a standard ......
RT @ googollee: the advantage of using vector is that the memory layout is controllable and can be directly imported to c api. It should be because the vector performance is not good to use the associated container RT @ laiyonghao # estl 23rd: consider replacing the associated container with a sorted vector ....... // It is costly to maintain a sorted_vector.
# Estl 24th: When efficiency is critical, make a careful choice between map: operator [] and map: insert. In short, insert is used for adding and [] is used for updating.
# Estl 23rd: consider replacing the associated container with a sorted vector. Well, I don't think it should be considered unless it proves that the performance of the associated container is not good. Hashmap cannot be used for map.
# Estl 22nd: do not directly modify the keys in set or Multiset. The key is to remember that if you modify the container directly, make sure that the container is still sorted. The same is true for python and other languages.
# Estl 21st: always let the comparison function return false in the case of equality. It sounds like nonsense? Well, remember it! (A <B) means a> = B. Remember to write the former as B <. Reply to push back
# Estl 20th: Specify the comparison type for the associated containers that contain pointers. This article has been agreed to be vulgar and there is nothing to say.
# Estl 19th: understand the differences between equality and equivalence. Equal, based on operator =, equivalent, based on operator <, when! (A <B )&&! (B <A) is equivalent to the two.
# After reading chapter 1 of estl, I lament that I am new. In addition, Twitter is a good tool for Reading Notes.
# Estl 18th: Avoid using vector <bool>. This is also a golden rule, no need to say more.
# 17th pieces of estl: use the "swap technique" to remove excess capacity. This article is too common and I have nothing to say.
# 16th pieces of estl: Learn how to pass the vector and string data to the old API. Well, once again, I think & V [0] is really disgusting. Because only vector ensures the same memory layout as the array, if you want to pass the container content such as map to the C-API, you may need to transfer the vector.
# Estl 15th: note the diversity of string implementations. We talked about four different implementations and analyzed their memory size, reference and sharing capabilities. Very interesting.
# Estl 14th: use reserve to avoid unnecessary reallocation. First, we analyzed the memory policy and related methods of vector/string, and then talked about reserve usage.
# 13th pieces of estl: vector and string take precedence over the dynamically allocated array. It is mainly because of their memory management and various nested Type Definitions for easy programming. In addition, it is discussed that string based on reference count may lose performance in the multi-threaded environment.
Re-reading # estl has read the first chapter. I have to say that C ++ books will be poisoned. Now I have a strong desire to re-read ecpp and mecpp ...... It's hard for me to write a three-year copy of Py ......
# 12th pieces of estl: do not rely on the thread security of STL containers practically. We can only expect (no dependency) that multi-thread reading is safe. It is safe to write data to different containers in multiple threads.
# Estl reads 10th more times. Article 11th: Understand the rational usage of user-defined assignment subtasks.
# Article 3 of estl: Understand the conventions and restrictions of allocating subobjects. Indeed, it is time consuming me to compile code ...... You may still need to re-read it several times ......
# Estl 8th: do not create a container object containing auto_ptr; 9th: select the method to delete the element with caution. Key Point: auto_ptr transfers ownership during copy. The deletion methods of sequence containers and associated containers are different, especially list.
RT @ googollee: 7th. Check the situation. If the items in the container still need to exist after the container is destroyed, delete is not allowed. Is ecpp embedded CPP? RT @ laiyonghao: # ecpp 7th ...... // I used the wrong tag, valid STL, And I will change it later # estl
# Ecpp 7th: if the container contains a pointer created through the new operation, delete the pointer before the container object structure; Article 8th: do not create a container object containing auto_ptr.
# Ecpp Article 6th: Beware of the most annoying analysis mechanism of the C ++ compiler. This probably means to avoid using anonymous objects as real parameters when calling a function to eliminate the ambiguity of the compiler.
# Ecpp 5th: The interval member function takes precedence over the corresponding single-element member function. It is easier to write range member functions, better express your intentions, and they show higher efficiency.
# Ecpp 3rd: ensure that the objects in the container are copied correctly and efficiently; 4th: Call empty instead of checking whether size () is 0.
Repeat the simplified STL Chinese version. 1st: carefully select the container type; 2nd: do not try to write code independent of the container type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.