Effective STL Recommendations

Source: Internet
Author: User

Vector is a type of sequence that can be used by default, and when inserting and deleting the middle of a sequence very frequently should be lit, and when most insertions and deletions occur at the head or tail of the sequence, you can choose to deque the data structure.

STL is built on generalization. Arrays generalize the container and parameterize the types of objects that are contained. function generalization is an algorithm that parameterize the types of all iterators. Pointer generalization is an iterator that parameterize the type of object being pointed to.

A separate container type is generalized to a sequence or associative container, and similar containers have similar functionality. Standard memory Neighbor containers provide a random-access iterator, and a standard node-based container provides a bidirectional iterator. The sequence container supports Push_front or push_back, but the associated container does not. The associative container provides Lower_bound, Upper_bount, and Equal_range member functions for logarithmic time complexity, but the sequence container does not.

Calling erase with an iterator as a parameter on a sequence container returns a new iterator, but nothing is returned on the associative container.

For using empty and size to determine the size of the container, use empty as much as possible, because empty is a constant-time operation, but for some list implementations, size spends linear time. Why is the list so troublesome, why not provide a constant time size? There are a lot of things to deal with for list-specific splice.

List<int> List1;

List<int> List2;

List1.splice (List1.end (), List2,find (List2.begin (), List2.end (), 5), Find (List2.rbegin (), List2.rend (), 10.base ()));

Move all nodes in List2 from 5 to the last occurrence 10 to the end of List1. If empty is used instead of checking for size () ==0, there is no error, and you should call empty () when you want to know that the container is enough to contain 0 elements.

The interval member function assign, which is valid for all flag sequence containers (vector, String, deque, and list). Whenever you have to completely replace the contents of a container, you should think of assigning values. If you are just copying a container to another container of the same type, operator= is the assignment function of choice, and when you want to give a complete new set of data to a container, assign can take advantage of it. Many people use copy, in fact, all the target range is inserted into the iterator specified by the use of the copy can be replaced by the call of the interval member function. Use interval member functions instead of single-element sibling operations, interval constructors: All standard containers provide this form of constructors

Container::container (Inputiteratorbegin,inputiterator end);

Interval insertion function: All standard sequence containers provide this form of insert:

Void Container::insert (iteratorposition,inputiterator begin,inputiterator end);

Interval delete, sequence, and associated container return types are not used, one returns the next element of the deleted element, and one returns void. The standard container provides the iterator container:erase (iterator begin,iterator end);

The associated container provides this: void Container::erase (iterator begin,iterator end);

Interval Assignment:

Void container::assign (Inputiteratorbegin,inputiterator end);

The behavior of removing a derived object by a base-class pointer without a virtual destructor is undefined, and the process does not require a class that requires destructors to be risky.

Do not create auto_ptr containers. Because when you copy a auto_ptr, the ownership of the object that auto_ptr points to is transferred to the auto_ptr of the copy, and the copied auto_ptr is set to null. That is, Auto_ptr has the sole right to ownership.

The thread safety in STL, which can be determined from the STL implementation, is:

Multiple readers are safe. Multithreading may read the contents of a container at the same time, which will execute correctly. This container cannot be manipulated by any writer at the time of reading.

It is safe for multiple writers of different containers. Multithreading can be written to different containers at the same time.

Vectors and strings can manage their own memory. When elements are added to those containers, their memory grows, and when a vector or string is destroyed, its destructor automatically destroys the elements in the container, reclaiming the memory that holds those elements.

Effective STL Recommendations

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.