VC + + Library

Source: Internet
Author: User
Tags comparison contains file size functions interface ole sort access
C++


A large number of programmers make as much use of existing code as possible. Programmers often buy code that is packaged into libraries, and many successful companies are developed by producing really good code libraries, such as Rogue Wavesoftware (www.roguewave.com).
When the C language is popular, the code base is the function library. For example, you can buy a math library that contains functions to complete calculus and algebraic operations. In general, you can use a containing file in your program code to specify a library of functions that you can link statically or dynamically.
The static link means that the library code is directly integrated into the program. In this case, the program is not dependent on other files, but the file size may be large. Dynamic linking means that the program has the version information of the library, and its code is stored in a separate file called a dynamic-link library (DLL). DLLs are loaded into memory only when a function in the DLL is invoked during the program run. DLLs exist in memory as a separate entity and can be accessed by multiple programs at the same time.
After C + +, the function library is transformed into a class library. The difference is that the library contains only a series of functions, and the class library is designed with object-oriented programming, for example, to make a class library of data structures that includes a list of linked classes. If you use a function library instead, the list of links is independent of the function that operates it. On the other hand, when you use a class library, the link list and the functions that manipulate it exist in the same class as the same file. However, just as using a function library, using a class library involves including files and linking a static library.
Recently, it has evolved from a class library to a template library. The reason is that the C + + compiler is now able to handle C + + templates. Template libraries provide a range of advantages over class libraries. To use the template Gallery, simply include the inclusion file in your program without having to link to the library because all library code is already contained in the inclusion file.
This doesn't sound very efficient, as it did early on, because every file that contains a template file gets a separate copy of the code. Therefore, the compiled program will be very large. However, in order to adapt to the template, the compiler has been optimized to make this method very efficient.
VC has three libraries for developers to use: A class library (MFC), two template libraries (ATL and STL).
17.2.1 Microsoft Foundation Class Library
Microsoft Foundation Class Library (MFC) is a class library built by Microsoft to help C + + developers, and contains many classes that support various functions, including implementing complex data structures, operating windows, and accessing databases. However, should MFC be used in server components? The answer is in the negative!
This is wise, do not use MFC in server components, server components can not access win 3.2, so do not need the MFC library, database support is best implemented in ATL.
ATL supports ADO and OLE DB, which MFC does not support. The STL has better data structure support than MFC. Even the most commonly used MFC classes, such as CString, have equivalent classes in the standard C library. MFC classes, such as CSocket, are occasionally used, but they can also be replaced by a win-only API.
MFC is used to build separate Windows applications, not for lightweight COM components. You should use an MFC class only if you are interfacing with an existing code that uses MFC. Other scenarios use ATL and STL classes, which make the components smaller and run faster.
17.2.2 Activity Template Gallery
You can tell that the Active Template Library (ATL) is newer than MFC because there is the word "active" in ATL, and the other term is "Microsoft Template Library" (Microsoft Templete LIBRARY,MTL).
ATL is used to create COM components quickly and easily, and takes up only a small amount of memory, and the key components of a component are done by C + + templates. In fact, the ATL design used the STL in the use of the template.
C + + templates provide a type-safe way to implement generic interfaces. Luckily, Microsoft provided some wizards to generate ATL code, so when you build a simple component, you don't have to know the template or even all the details of the ActiveX. Another advantage of ATL is that you do not rely on DLLs, which means that when you load an application, you do not have to load DLLs and static links, both of which increase the need for memory space.
ATL makes COM development easier, ATL also supports data access through OLE DB consumers and providers, and there are special uses, such as supporting the creation of Microsoft Management Console (MMC) plug-in components.
Compared to ATL, MFC relies on additional run-time DLLs or static links, resulting in code that is larger and slower than ATL. Therefore, if you do not have a user interface when building a COM component, it is better to choose ATL than MFC.
17.2.3 Standard Template Library
The Standard Template Library (STL) is one of the most underutilized useful tools for VC + + programmers, but it is the only standard and Cross-platform VC + + library.
One of the reasons why the use of STL is not much is that Microsoft's STL literature is difficult to understand. There is no STL written for VC + + programmers, written by Musser and Sauni "STL Tutorial and Reference Guide" is a good book about STL.
STL is useful for C + + component development because it contains many data structures and algorithms. In many cases, the purpose of the server component is to process the data, and the STL is suitable for this purpose.
Microsoft initially implemented the STL standard in VC + + 4.2, but the implementation of this STL is so poor that it cannot be used. Some people solve this problem by using the STL provided by HP or SGI. Since STL is a iso/ansi standard, it is cross-platform, as long as the compiler supports the structure required by the STL. However, Microsoft's updated STL implementation (provided in VC + + 5.0 and 6.0) is more stable and easier to use than the previous STL.
The STL is a perfect addition to ATL, and ATL has no data structures and algorithms. Also, STL and ATL have similar implementations, which use C + + templates, so they work well together and do not need to link additional dynamic or static link libraries, which makes them smaller and less dependent on DLLs.
Let's introduce the key concepts of STL and use these concepts when building server components.
1. STL Traversal Device
When you use C programming, you typically use two methods to traverse a data structure: using a subscript or incrementing a pointer, the two methods achieve the same effect. But writing a generic algorithm to handle different types is impossible, with STL traversal to solve this problem, the following example is the best explanation.
The following program shows two standard C traversal methods, and finally the STL traversal method. The task of the program is to compute the number of "T" in a string.


Use the loop to troubleshoot problems with pointers or subscripts. Pointers and subscripts depend on the data, which is stored next to each other in memory, while the iterator hides the data so that the user of the data structure does not have to understand the order of the datastore. Hidden data structures allow the creation of common algorithms that work with various data structures, such as working with containers
For
For the efficiency of the algorithm, STL defines the class of five kinds of ergodicity: input, output, forward, bidirectional and random access. The algorithm is designed for the specific traversal method, so it has the best performance.
The loop is similar to the pointer, moving to the next entry and the previous one using the operator "+ +" and "--", similarly, to obtain the value of an entry using the operator "*".
2. STL Mapping
A mapping is a sorted container, where it is concerned with the ability to retrieve entries as quickly as possible based on the keys stored in the entry, rather than to store the entries in a container in a linear order.
The STL ordering container has set, Multisetmap, and Multimap. Set is used for the entry collection, and map is used for one entry to associate with another. Because of the bidirectional traversal, the sort container supports moving data entries in a linear order, although the purpose of the sorting container is to use key access.
A mapping container supports fast data retrieval based on individual keys, which are unique in mappings, which have the potential to save a lot of storage space for sparse data and to reduce many computational time, as you will see in the following narrative. Keys and values are stored in the pair type, which has two members: first and second. Retrieves the return key of the second, retrieving the value of the return key. The mapped traversal is the pair type of the traversal device.
You will see examples of using mappings later in this chapter.
3. STL Vector
One vector is an example of a sequence container, and the other sequence containers are deque and list. They have similar functionality, and the basic difference is performance. For example, a deque is no different from a vector except that the insertion and deletion at the beginning of the deque is faster than the vector. Different issues need to be accessed in different ways. Vectors provide fast, random access to different lengths of data, which are faster to insert and delete at the end of the sequence, but slower in other places.
4. STL algorithm
STL algorithm is classified according to the influence mode and interaction of the data structure of its operation. STL algorithm and its operation of STL data structure interface is STL traversal. STL traversal plays an important role because it limits how the algorithm accesses data. Many algorithms receive a function as part of its interface, and the parameter of the function returns a Boolean value representing a situation. The algorithm can be customized with function parameters.
Some STL algorithms allow functions to be passed as parameters. In most cases, these functions complete a comparison and return a Boolean value. For example, a sort algorithm uses a function as a parameter to compare two values, which compares a value to one that is smaller than the other by some criteria. Different functions have different criteria, which causes the data to be sorted in different order.
More interestingly, a function object (rather than a function) is passed as an argument. A function object can contain a state, which can enable comparison rules that exist somewhere. The comparison rules used depend on the state of the object. Later in this chapter there is an example of a comparison function of the sorting algorithm, implemented in this way.
5. The classification of STL algorithm
STL algorithm can be divided into four categories. Non-mutation sequence algorithm, the operation does not change the data structure content, the mutation sequence algorithm changes the data structure content, the correlation sorting algorithm can use in the sorting, the merging and the binary search and so on many kinds of algorithms. Finally, the STL has a generalized numerical algorithm to compute the elements in the data structure.
? Non-mutation sequence algorithm: elements that do not directly alter the data structure of their operations. Typically, they look for elements in a data structure, examine the equation of a sequence element, and compute the number of sequence elements. Find and Count is an example of a non-mutation sequence algorithm that moves through a range of data structures to look for the first one to be equal to a given value. The count algorithm calculates the number of elements in the data structure that are equal to the given value.
? Mutation sequence algorithm: Modifies the elements in the data structure of its operation. These algorithms copy, replace, transform, delete, and loop the elements in the container. The fill algorithm is an example of an algorithm that populates all locations in a sequence range with a copy of a given value.
? Related sorting algorithms: sorting, merging, and finding elements. Similarly, it is possible to set up sequential sequences, and the merge algorithm is an example of placing elements within a range of two sorted ranges without overlapping results.
An example of a generalized numerical algorithm is accumulate, which sums up the values of a specified range within a data structure.
6. Using the algorithm of the traversal device
As has been said before, STL defines the class of five kinds of traversal: input, output, forward, bidirectional and random access, the algorithm is designed for the specific traversal method to ensure optimal performance.
For example, the find algorithm can be used to look up values in various data structures, and the algorithm does not care whether the container is a vector, a list, or an array. The only condition is that the data structure must support the Inputiterator,inputiterator design to ensure the efficiency of the operations it supports. The find algorithm requires only inputiterator supported operations, so that it works efficiently.
Some algorithms require a more powerful traversal: such as sort requires a random access traversal (Random access iterator). Random access is provided at the expense of performance and other actions, such as inserting elements in the middle of a container, are affected.
Now that you have sufficient theoretical knowledge, create a component below.




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.