C + + Learning Note (16): More operations on vectors-generic algorithms

Source: Internet
Author: User

First of all, the generic algorithm here is actually not only the operation of the vector, for the "sequential container" can be.

But what is a sequential container:

As we all know, a container is a collection of certain types of objects. Sequential containers provide programmers with the ability to control the storage and access of elements. A notable feature of this container is that the order of the elements in the container does not depend on the value of the element, but rather on the position when the container is added. Common sequential containers are vector, deque (double-ended queue), list (doubly linked list), Forward_list (unidirectional list), array (fixed size array), String.


Learn about the sequential container, and now take the vector as an example to illustrate the following.

The order container itself defines only a few operations that I have written in the previous blog (point here). such as adding, deleting elements, accessing elements, getting iterators, and so on. But in reality, our users certainly want to have more operations for themselves, such as finding, replacing, deleting specific elements, rearranging elements, and so on. To implement the above functionality, the programmer may need to write a large piece of code to implement it.

To solve this problem, the standard library gives a set of "generic Algorithms (Generic algorithm)" to increase the type of vector operation. Called its "algorithm" because they implemented some of the common interfaces of classical algorithms, such as sorting, searching. Called generics because they can be used for different types of elements and multiple container types.

Here's a look at the specific generic algorithms.

Most of the algorithms are defined in the header file algorithm, and the standard library also defines a set of numeric generic algorithms in the header file numeric. In general, these algorithms do not directly manipulate the container, but rather traverse a range of elements specified by the two iterators.

These algorithms can be broadly divided into the following categories:

one, read-only algorithm

As the name implies, this algorithm reads only the elements within its input range and does not change the elements.

<1>find (Vec.begin (), Vec.end (), Val)

In Vec, search for Val, which returns an iterator that is the first element equal to Val. If there are no matching elements within the range, place the Vec.end () back.

<2>count (Vec.begin (), Vec.end (), Val)

Searches for Val in Vec, returning the number of times that Val appears in the VEC.

<3>accumulate (Vec.begin (), Vec.end (), Val)

The sum of the elements in the VEC, and the initial value is set to Val. The type of VAL determines which addition operator the function uses and the return value type.

<4>equal (Vec1.begin (), Vec1.end (), Vec2.begin ())

Compares two vectors for equality, and the return value is type bool.

Second, the writing container element algorithm

<1>fill (Vec.begin (), Vec.end (), Val)

Resets elements in Vec to Val


<2>fill_n (dest, N, Val)

Resets n elements starting with dest to Val. The dest here is an iterator that points to an element. Note: The VEC here cannot be empty, and if it is empty, then the above operation will have an unknown result.

<3>back_inserter (VEC)

An iterator that adds an element to a container, so called an insert iterator. Typically, when we assign a value to a container through an iterator, the value is given to the element that the iterator points to. When we assign a value through an insert iterator, an element equal to the value on the right of the assignment number is added to the container. Back_inserter receives a reference to the container that returns an insert iterator bound to the container. When we assign a value through this iterator, the assignment operator calls Push_back to add an element with the given value to the container. Like what:

Vector<int> VEC; Empty vector

Auto it = Back_inserter (VEC); Assigning a value to it will add the element to the VEC.

*it = 42; The VEC now has an element with a value of 42

We often use Back_inserter to create iterators, which are used as the destination for a generic algorithm. Like what:

Vector<int> VEC; Empty vector

Fill_n (Back_inserter (VEC), 10, 0); Add 10 elements to a VEC.

Can be found, the front said Fill_n the VEC can not be empty, otherwise there will be unknown results, and here Back_inserter equivalent to VEC push_back 10 positions, and then fill, so the usage here is possible.

<4>copy (Vec1.begin (), Vec1.end (), Vec2.begin ())

The contents of the VEC1 are copied to the VEC2, so the vec2 length must be greater than or equal to the length of the VEC1.

<5>replace (Vec.begin (), Vec.end (), Val1, Val2)

Replace all val1 in the VEC with Val2.


<6>replace_copy (Vec1.begin (), Vec1.end (), Vec2.begin (), Val1, Val2)

Instead of changing the value in Vec1, the VEC1 is copied to VEC2 and all val1 in VEC2 are replaced with Val2.

Third, sort

Sort (Vec.begin (), Vec.end ());

Sort the contents of the VEC in dictionary order

Iv. Delete the same data

Unique (Vec.begin (), Vec.end ());

Removes duplicate data from the VEC, returning an iterator to a location after the non-repeating region. It is important to note that this delete operation simply empties the data in the VEC, but the size of the VEC does not change. That is, assuming that there are 40 elements in the original VEC, of which there are 5 duplicates, then after a unique operation, the duplicated parts of the repeating elements are gone, the VEC non-empty data becomes 35, the remaining 5 space inside the data is empty. But Vec's size is still 40. To really delete these elements, you need to erase the container itself.

C + + Learning Note (16): More operations on vectors-generic algorithms

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.