Valid STL clause 50

Source: Internet
Author: User
Article 50: familiarize yourself with STL-related websites

The Internet is full of STL information. Search for "STL" using your favorite search engine. It will certainly return several hundred links, some of which may actually be related. However, for most STL programmers, there is no need to search. The following websites should be upgraded to the top of the most frequently used list of almost everyone:

  • Sgi stl website,Http://www.sgi.com/tech/stl /.
  • Stlport website,Http://www.stlport.org /.
  • Boost website,Http://www.boost.org /.If you cannot access it, try http://boost.sourceforge.net /)

The following is a brief description of why these websites are worthy of favorites.

Sgi stl website

SGI's STL website is among the top, and there are good reasons. It provides comprehensive documentation for each STL component. For many programmers, no matter which STL platform they use, this website is their online reference manual. (The reference document was compiled by Matt austern and later expanded and refined to generic programming and the STL [4].) The materials here include not only the STL components themselves. For example, the discussion about container thread security in objective STL (see article 12) is a topic based on the sgi stl website.

The SGI website provides STL programmers with other things: an STL implementation that can be freely downloaded. This implementation is only transplanted to a few compilers, but the widely transplanted stlport distribution is also based on SGI distribution, and I will soon write more about stlport. In addition, the SGI Implementation of STL provides many non-standard components that make STL programming more powerful, flexible, and interesting. The most famous ones are:

  • Hash associated containerHash_set, hash_multiset, hash_map, and hash_multimap. For more information about these containers, refer to Clause 25.
  • Single-chain table container, Slist. As you think, the iterator points to the list nodes you want them to point. Unfortunately, this makes implementing the insert and erase member functions expensive because both require adjusting the nodes pointed by the iterator.PreviousThe next pointer of the node. In a two-way linked list (such as a standard list container), this is not a problem, but in a single list table, "backward" a node is a linear time operation. For slist of SGI, insert and erase take linear rather than constant time, which is quite a disadvantage. SGI solves this problem through non-standard (but constant time) member functions insert_after and erase_after. SGI annotation,

    If you find that insert_after and erase_after cannot meet your needs, and you often need to use insert and erase in the middle of the list, you may need to use list instead of slist.

    Dinkumware also provides a single-chain table container called slist, but it uses a different iterator Implementation of insert and erase to maintain linear time performance. For more information about dinkumware, see Appendix B.

  • A string-like container for ultra-long strings. This container is called rope because the rope (rope) is a heavy line (string). Have you seen it? SGI describes rope in this way:

    Rope is a scalable string implementation: they are designed to treat string as a whole for efficient operations. For example, the time spent in assigning values, concatenating strings, and substrings is almost independent of the length of strings. Different from the string of C, rope is a reasonable expression of a super-long string, such as editing the buffer or mail information.

    At the backend, rope is implemented as a tree for referencing and counting substrings, and each substring is stored as a character array. One interesting aspect of the rope interface is that the begin and end member functions always return const_iterator. This is to prevent the customer from modifying a single character. This operation is expensive. Rope is optimized for operations involving the entire string (for example, assigning values, concatenating strings, and obtaining substrings). Single Character operations are poorly performed.

  • MultipleNon-standard function objects and adapters. The original hp stl Implementation contains more function objects than the standard C ++. Two of them are missed by many old STL hackers, select1st and select2nd, because they are very useful for map and multimap. Given a pair, select1st returns its first component, while select2nd returns its second component. These non-standard function-like templates can be used as follows:
    Map
       
        
    M;... // write all the map keys to couttransform (M. Begin (), M. End (), ostream_iterator
        
         
    (Cout, "/N"), select1st
         : Value_type> (); // creates a vector and copies all values in the map to the vector.
          
           
    V; transform (M. Begin (), M. End (), back_inserter (V), select2nd
           : Value_type> ());
          
        
       

    As you can see, select1st and select2nd Simplify Algorithm calling instead of writing your own loop (See Clause 43). However, if you use these functions, the fact that they are non-standard allows you to write codes that are not portable and cannot be maintained (see section 47 ). The avid STL fans don't care. They think it is unfair that select1st and select2nd do not enter the standard first.

    Other non-standard function objects that are part of SGI implementation include identity, project1st, project2nd, compose1, and compose2. You can access the website to find the example of using compose2 on page 1 of this book. Now, I hope you will understand that accessing SGI is beneficial.

The library implementation of SGI goes beyond STL. Their goal is to develop a complete implementation of a standard C ++ library, except for the part inherited from C (SGI thinks you already have a standard C library that is available ). Therefore, another noteworthy download that can be obtained from SGI is the implementation of the C ++ iostream library. As you expected, this implementation works well with the SGI Implementation of STL, but it is superior to many iostream implementations with C ++ compilers in terms of feature performance.

Stlport website

The main selling point of stlport is that it provides a modified version that can be transplanted to sgi stl implementations (including iostream) of more than 20 compilers. Like the SGI library, stlport can be downloaded for free. If the code you write must work on multiple platforms, you can use the stlport implementation as the standard and make all your compilers use it to save yourself a lot of trouble.

Most stlports focus on improving the portability of SGI code, but the STL of stlport is also the only one I know that provides the "debug mode" to help detect the incorrect usage of STL-the usage of compiled but undefined runtime behavior. For example, article 30 describes how to write this common error at the end of a container:

Int transmogrify (int x); // This function generates some new values from X // Vector
 
  
Values;... // put the data into valuesvector
  
   
Results; transform (values. begin (), values. end (), // try results. end (), // beyond the end of Results transmogrify); // write to a place!
  
 

This can be edited, but when running, it produces undefined results. If you're lucky, something terrible will happen inside the transform call, and debugging is relatively simple. If you are not lucky, the transform call will put data somewhere in your address space, but you will find it later. At that time, determine the cause of Memory Errors-will we say? --Challenges.

The stlport debugging mode eliminates this challenge. When the preceding transform is executed, the following message is generated (assuming that stlport is installed in the directory c:/stlport ):

C:/STLport/stlport/stl/debug iterator.h:265 STL assertion failure : _Dereferenceable(*this)

Then the program stops, because if the stlport debugging mode encounters a usage error, abort will be called. If you want to throw an exception, you can configure stlport as your method.

It is undeniable that the above error message is not as clear as it may be, and unfortunately, the reported files and rows correspond to the locations of internal STL assertions rather than the rows that call transform, but this is still better than running the transform call and trying to point out why your data structure is wrong. In the stlport debugging mode, all you need to do is to start your debugger, return the call stack to the code you wrote, and then determine what you did wrong. It is generally not a problem to find the source code lines that are disgusting.

The debugging mode of stlport detects a variety of common errors, including passing invalid intervals to algorithms and attempting to read from an empty container, use the iterator from a container as the real parameter of the second iterator member function, and so on. It tracks each other through the iterator and their containers to achieve this magic. Given two iterators, this makes it possible to check whether they come from the same container, and when a container is modified, this makes the appropriate iterator set invalid.

Because stlport is implemented using a special iterator In the debugging mode, the iterator of vector and string is a class object rather than the original pointer. Therefore, using stlport and compiling in debug mode is a good way to verify that no one rashly processes the difference between pointers and those container-type iterators. This alone is enough to be an opportunity for stlport debugging.

Boost website

In 1997, when the road to C ++ international standards was closed, some people were disappointed that the library feature they promoted was not included. Some of these members are members of the committee, so they began to lay the foundation for the expansion of the standard library during the second standardization round. The result is boost. A task is to "provide free and peer-reviewed C ++ libraries. The focus is on websites that can work well with the c ++ standard library with a portable library. There is a motivation behind the task:

The degree to which a database becomes an "existing practice" increases the possibility of someone submitting it to future standards. Submitting a library to boost.org is a way to establish existing practices ......

In other words, when a library may be added to the Standard C ++ library, boost uses itself as a mechanism to help distinguish between good and bad. This is an honorable service, and we should all be grateful.

Another reason to be grateful is the library set you can find in boost. I don't want to explain them all here, especially because many new libraries have been added when you read these words. However, two libraries are particularly useful for STL users. The first is the smart pointer library, which contains the shared_ptr template used to reference the counting smart pointer. Unlike the standard library auto_ptr, it can be securely stored in STL containers (see article 8 ). The boost smart pointer Library also provides shared_array, a reference counting smart pointer for dynamically allocating arrays. However, Clause 13 states that dynamic allocation of arrays is not as good as vector and string, and I hope you will find its arguments convincing.

Boost's second attraction to STL is its Group of function objects and related tools related to STL. These libraries contain the basic principle of re-designing and re-implementing the idea behind STL function objects and adapters. This result eliminates many human constraints on the efficacy of standard function imitation. As an example of such a constraint, you will find that if you try to bind2nd and mem_fun or mem_fun_ref together (refer to Article 41) to bind an object to a member function parameter, and that member function gets its parameters through reference, your code cannot be compiled. If you try to use not1, not2, and ptr_fun together and a function declares a parameter that passes the reference, you will find the same result. In the two cases, the reason is that during template instantiation, most STL platforms generate reference references, and the reference references are invalid in C ++. (The Standards Board is brewing a change in the standards to solve this problem .) Here is an example of "reference to reference:

Class widget {public: ...int readstream (istream & stream); // get the parameter by referencing...}; vector
 
  
VW ;... for_each (// Most STL platforms VW. begin (), VW. end (), // try to bind2nd (mem_fun (& Widget: readstream), CIN) in this call // generate a // reference to the reference; // such code // cannot be compiled
 

Boost function objects avoid this and other problems. In addition, they greatly increase the expressiveness of function objects.

If you are interested in the potential of the STL function object and want to explore it further, go to boost immediately. If you hate function objects and think they exist only to comfort a minority of Lisp advocates who have switched to C ++ programmers, you can turn to boost. Boost function object libraries are very important, but they are only a small part of what you can find on that website.

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.