let’s look at a more challenging example. Suppose the container retains pointers to objects and has a different model for access and traversal. For example, you might access a container like the one below:
This style is not uncommon; we can map it to a sequence like [b,e):
Note that this modification is inconsequential: I did not modify the container or the hierarchy of container classes supported by the C++ standard library for mapping containers to models for traversal. This is a form of rewriting rather than refactoring.
I chose this example to illustrate that these generic programming techniques are not limited to popular standard libraries. They also conform to common definitions of “object-oriented”, but they are not strictly object-oriented.
The view that C++ code must be object-oriented (meaning that every place uses hierarchy and virtual functions) deeply affects people’s evaluation of C++ performance. Some believe that when it comes to solving various types of runtime problems, only object-oriented is best. In the past, I thought so too. However, in fact, it also has a rigid side (for example, not all related types belong to the same hierarchy) and virtual functions cannot be inline functions (which leads to a lot of time wasted when handling many simple and important tasks).
① Copy “Xiāng Nóng Xìn Xī“, paste the search number in “Add Friend” to follow.
② Click the “+” in the upper right corner of WeChat, and select “Add Friends”, enter “Xiāng Nóng Xìn Xī” to find it.
③ If the article helps you, please click the button in the upper right corner, and select “Share to Moments” to share the article to your Moments.
④ Scan the code for more excitement to come!
