[C ++ STL learning] A summary of the common capabilities and operations of containers

Source: Internet
Author: User

I. c ++ STL containers have some common capabilities

The three core capabilities are:

(1) All containers provide "value Semantics" rather than "reference Semantics ".

When the container inserts an element, the copy operation is performed internally and the element copy is stored.Therefore, each element of the container must be copied..

If the object to be stored does not have a public copy constructor or you want to make a copy of the object, the container element can only be a pointer..

(2) All elements form an order.

We can traverse each element once or multiple times in the same order. Each container provides functions that can return the iterator, which can be used to traverse elements.

(3) In general, operations are not absolutely safe.

The caller must ensure that the parameters passed to the operation function meet the requirements. Violation of requirements will lead to undefined behavior. In addition, STL usually does not run exceptions on its own.


2. Common container operations

The following table lists the common operation functions of containers:


(1) initialization

Each container category provides a default constructor, a copy constructor, and a destructor.

Initialize with the element of another container as the initial value:

List <int> intlist;
Vector <float> floatvec (intlist. Begin (), intlist. End ());

Initialize with an array element as the initial value:

Int array [] = {1, 2, 3, 4, 5 };
Set <int> intset (array, array + sizeof (array)/sizeof (array [0]);

Get elements from standard input for initialization:

Deque <int> C (istream_iterator <int> (CIN )),
(Istream_iterator <int> ()));

Note: The redundant brackets of the initialization parameters cannot be omitted:
It cannot be like this:
Deque <int> C (istream_iterator <int> (CIN ),
Istream_iterator <int> ());

Otherwise, the expression is considered a declaration of Function C. The parameter type is istream_iterator <int> and the parameter name is cin. The second parameter is unknown and the type is istream_iterator <int>. The Return Value Type of the function is istream_iterator <int>.
However, by adding a pair of parentheses, we can make the parameter istream_iterator <int> (CIN) no longer conform to the declaration syntax.

Because the syntax in the C ++ standard does not accept such a function declaration form:

T name (u), (v ));

(2) size-related operation functions

1. Size ()

Returns the number of current elements.

2. Empty ()

It is equivalent to size () = 0. But the efficiency may be higher. Therefore, use empty () whenever possible ().

3. max_size ()

Returns the maximum number of elements that a container can hold. The value varies depending on the actual version.

(3) Comparison

Including commonly used = ,! =, <, <=,>,> =.

Note: the two ends of the comparison operation must belong to the same type. If all elements of the container are in the same order, the two containers are equal. Use operator = to check whether the elements are equal. The lexicographic comparison principle is used to determine whether a container is smaller than another container.

(4) value assignment and swap Switching

When an element is assigned to a container, all elements of the source container are copied to the target container, and all elements of the latter are completely removed. Therefore, the assignment operation is costly.

If the two containers have the same type and the source container is not used after the copy, you can use swap for optimization. Its performance is much better. In fact, it only exchanges some internal pointers, so the time complexity is constant, while the complexity of the value assignment operation is linear.

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.