Difficult problems and solutions of c++/vc++ programming (II.)

Source: Internet
Author: User
Tags constructor

Standard library questions, and then talk about the list of the iterator can be randomly moved?

Can the "list iterator" move randomly in the previous article? "A review of the problem:

Because the list's internal implementation is a two-way linked list, the list requires that the iterator (pointer) move from front to back (or forward) in turn, moving one position at a time, so that the list defines the + + and-operator, and does not define + 、-、 + = and-= operators. So to the list of the iterator to move a distance, you need to programmatically implement, with a small loop on the line, the code is as follows:

#include <list>
using namespace Std;
List<int> myList;
...//MyList initialization and other operations
List<int>::const_iterator itlist = Mylist.begin ();
Itlist move Len Distance forward
for (int i= 0; i < len; i++)
..//Other operations
The above to the STL list pointer random movement problem explanation is not very good, thanks Zhou Xingshing's reminder, we may use the STL advance operation, I give the code is advance for list's one possible realization method. Here I suggest using the advance action instead of my piece of code.

The advance operation is a generic iterator move operation for all container types, which automatically chooses the appropriate move method based on the different container types, and for random access containers such as vectors and deque, the iterator can move directly to the desired location. For containers that are not randomly accessed (such as LIST,MAP, etc.), the iterator needs to move slowly back until it is moved to the desired position. However, different STL implementation versions may be different for advance implementations. We don't need to know how it's going to work.

Standard library problem, the difference between vector resize () and reserve () functions

First of all, there are essential differences between the two functions. The reserve is a container reservation, but does not really create an element object and cannot refer to elements within the container until the object is created, so when you add a new element, you need to use the push_back ()/insert () function.
Resize is to change the size of the container and create the object, so after calling the function, you can refer to the object within the container, so when you add the new element, use the operator[operator, or use an iterator to refer to the element object.

Furthermore, two functions in the form of a difference, the reserve function after a parameter, that is required to reserve the space of the container; The resize function can have two parameters, the first parameter is the new size of the container, and the second parameter is the new element to be added to the container, if the argument is omitted, Then the default constructor for the element object is invoked. The following are examples of the use of these two functions:
Vector<int> Myvec;
Myvec.reserve (100); The new element is not yet constructed
for (int i = 0; i < i++)
Myvec.push_back (i); The new element is then constructed
Myvec.resize (102); Two new elements are constructed with the default constructor of the element
MYVEC[100] = 1; Directly manipulate new elements
MYVEC[101] = 2;
Standard library problem, vector memory redistribution problem

When using vectors, it is important to note that vectors are dynamically allocating memory. While it is convenient to use vectors, the consequences are bad if you don't pay attention to related problems. For example, the following program:
struct Forwardprob
String M_ss;
String M_dictitem;
int m_index;
float M_forwardprob;
Forwardprob *PREFP;
Vector<forwardprob> Myvec;
for (int i = 0; i < count; i++)
Forwardprob THISFP;
THISFP->PREFP = Some previous pointer in Myvec;
Myvec.push_back (THISFP);
In this code, the pointer prefp in each element may become invalid because each THISFP is a new addition to the Myvec, which may require a reallocation of memory, where the Myvec's position in memory may change.
Solution: Before using the Myvec, reserve function for vector reserved enough space, or the PREFP needle to the subscript.

Dynamic link libraries and static link libraries

dynamic-link libraries and static-link libraries are built in much the same way that when visual C++.net uses the build project, selecting a DLL or this static library in the application settings allows you to establish an empty dynamic link library/static link library.

Establishment and use of static link libraries

Suppose to establish a staticlinklib static link library. The interface functions in the static link library are defined in the following form:

extern "C" Return_type interfacefunctionname (parameter ...);

Note that it is necessary to add extern "C" here. Also in the dynamic link library corresponding header file, like this declaration, after compiling the dynamic link library (. lib file) and the corresponding header file submitted to the user to use. Suppose that these two files are StaticLinkLib.lib and staticLinkLib.h.

When users use a dynamic link library, they want to include the header file StaticLinkLib.h above and select the static link library file StaticLinkLib.lib in the linker/input in the project properties. The following figure:

Project property settings under Figure Vc.net

In this way, users can invoke the interface functions defined in the static link library in their own applications.

Establishment and use of dynamic link libraries

The establishment/use of dynamic link libraries is basically the same as static link libraries, and the difference is in the declarative form of interface functions. The interface function declaration of a dynamic-link library is as follows:
extern "C" __declspec (dllexport) return_type interfacefunctionname (parameter ...);

In addition, the dynamic link library is compiled to generate a dynamic link library file (DLL) and a. lib file. Submit the header file for these two files and corresponding interfaces.

When users use a dynamic link library, they also need to select the corresponding. lib file in the linker/input in the project properties, and the program automatically invokes the. dll file. The user does not need to include the header file submitted above, the user only need to declare the interface function in the form as follows:

extern "C" __declspec (dllimport) return_type interfacefunctionname (parameter ...);
This allows users to invoke the interface functions defined in the dynamic-link library in their own applications.

You cannot have screen output statements in a dynamic-link library, such as cout << ... And so on, so debugging is not convenient, there is no study of dynamic link library debugging methods.

A big difference between static and dynamic-link libraries is that you cannot include other dynamic-link libraries or static libraries in a static-link library, and you can include additional dynamic/static link libraries in a dynamic-link library.

Related Article

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.