C ++ learning notes (16): perform more operations on vector-generic algorithms and learning notes vector

Source: Internet
Author: User

C ++ learning notes (16): perform more operations on vector-generic algorithms and learning notes vector

Emphasize that the generic algorithm here is not only for vector operations, but for "sequential containers.

But what is an ordered container:

We all know that containers are collections of certain types of objects. Ordered 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 elements in the container is independent of the value of the element, but is related to the position when the container is added. Common Ordered containers include vector, deque, list, forward_list, array, and string.


We have learned about ordered containers. Now we take vector as an example to describe the following.

The ordered container only defines a few operations. I have also written these operations in my previous blog (Click here. For example, adding or deleting elements, accessing elements, and obtaining iterators. But in reality, our users certainly want to have more operations for their own use, such as searching, replacing, deleting specific elements, and re-arranging elements. To implement the above functions, programmers may need to write a large piece of code to implement it.

To solve this problem and increase the number of vector operations, the standard library provides a set of generic algorithms (generic algorithm )". It is called an "algorithm" because they implement public interfaces of some classic algorithms, such as sorting and searching. They are called "generic" because they can be used for different types of elements and multiple container types.

 

The following describes the specific generic algorithms.

Most algorithms are defined in the header file algorithm. The standard library also defines a set of numeric generic algorithms in the header file numeric. Generally, these algorithms do not directly operate on containers, but traverse an element range specified by two iterators.

These algorithms can be roughly divided into the following types:

 

I. Read-Only Algorithm

As the name suggests, this algorithm only reads elements in the input range and does not change the elements.

<1> find (vec. begin (), vec. end (), val)

Search for val in vec and return the first iterator that is equal to val. If no matching element exists in the range, vec. end () is returned ().

<2> count (vec. begin (), vec. end (), val)

Search for val in vec and return the number of times that val appears in vec.

<3> accumulate (vec. begin (), vec. end (), val)

The element summation in vec and the initial value of the sum are set to val. The val type determines which addition operator and return value type the function uses.

<4> equal (vec1.begin (), vec1.end (), vec2.begin ())

Compare whether two vectors are equal. The return value is of the bool type.

 

Ii. Writing container element Algorithms

<1> fill (vec. begin (), vec. end (), val)

Reset the element in vec to val


<2> fill_n (dest, n, val)

Resets n elements starting from dest to val. Here, dest is an iterator pointing to an element. Note: the vec here cannot be empty. If it is empty, the above operation will show unknown results.

 

<3> back_inserter (vec)

An iterator that adds elements to a container. It is called an insert iterator. Generally, when we assign a value to the container through the iterator, the value is assigned to the element pointed to by the iterator. When we use an insert iterator to assign values, an element equal to the value on the right of the assignment number is added to the container. Back_inserter receives a reference pointing to the container and returns an insert iterator bound to the container. When we use this iterator to assign values, the value assignment operator calls push_back to add an element with a given value to the container. For example:

Vector <int> vec; // empty vector

Auto it = back_inserter (vec); // when assigned, the element is added to vec.

* It = 42; // an element in vec with a value of 42

We often use back_inserter to create an iterator and use it as the target location of the generic algorithm. For example:

Vector <int> vec; // empty vector

Fill_n (back_inserter (vec), 10, 0); // Add 10 elements to vec.

It can be found that the vec of fill_n cannot be empty, otherwise unknown results will appear. Here, back_inserter is equivalent to 10 locations for vec push_back, and then fill in, therefore, the usage here is acceptable.

 

<4> copy (vec1.begin (), vec1.end (), vec2.begin ())

Copy the content in vec1 to vec2. Therefore, the length of vec2 must be greater than or equal to the length of vec1.

 

<5> replace (vec. begin (), vec. end (), val1, val2)

Replace all val1 values in vec with val2.


<6> replace_copy (vec1.begin (), vec1.end (), vec2.begin (), val1, val2)

After replacement, the value in vec1 is not changed. Instead, vec1 is copied to vec2 and val1 in vec2 is replaced with val2.

 

Iii. Sorting

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

Sort the content in vec alphabetically

4. Delete the same data

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

Delete the duplicate data in vec, and return the iterator pointing to a location after the non-duplicate region. It is worth noting that this delete operation only clears the data in vec, but the vec size remains unchanged. Assume that the original vec contains 40 elements, five of which are repeated. After the unique operation, the repeated parts of these elements will disappear, the non-empty data in vec is changed to 35, and the data in the remaining five spaces is empty. However, the vec size is still 40. To delete these elements, you also need to perform operations on the container, erase.

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.