Summary of STL interval member function and interval algorithm _c language

Source: Internet
Author: User

In this paper, we summarize the interval member function and interval algorithm which can replace the loop.

The advantage of using interval member functions in comparison to single element traversal operations is:
1 fewer function calls
2 fewer elements to move
3 Less memory allocation

Interval algorithms should be used in cases where the interval member function is not applicable, at least, it is simpler, more effective and less error-prone than the handwritten loop.

Interval member function

Interval construction

The standard container supports interval constructors:

Copy Code code as follows:

Container::container (Inputiterator beginning,//Interval)
Inputiterator end); The end of the interval

For example:

Copy Code code as follows:

int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
Std::vector<int> Myvector (myints, myints+8);

The above is the common method in c++98, in c++11, vector can initialize directly:

Copy Code code as follows:

std::vector<int> second ={10, 20, 30, 30, 20, 10, 10, 20};

Or:

Copy Code code as follows:

Std::vector<int> second ({10, 20, 30, 30, 20, 10, 10, 20});

Interval insertion

The standard sequence container provides this form of insert:

Copy Code code as follows:

void Container::insert (iterator position,//range insertion Position
Inputiterator begin,//Insert the starting point of the interval
Inputiterator end); Insert end of interval

For example:

Copy Code code as follows:

int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
Std::vector<int> Myvector;
Myvector.push_back (100);
Myvector.insert (Myvector.begin (), myints,myints+8); 10 20 30 30 20 10 10 20 100

The associative container also supports interval insertion, but because its insertion position is determined by its comparison function, there is no interval insertion position for this parameter;

Interval deletion

Erase provided by the standard sequence container:

Iterator Container::erase (iterator begin, iterator end);

The C++98 standard associative container provides erase for:

void Container::erase (iterator begin, iterator end);

After the sequence container calls erase, it returns an iterator (the next one of the deleted element).
The erase of the associated container does not return an iterator after it has been deleted. "The official explanation is that if you implement a sequence container that returns to the next iterator, it will cause unacceptable performance degradation";

This distinction was finally unified in the c++11; c++11, after invoking erase on the associated container, returns an iterator (pointing to the next of the deleted element);

Iterator Container::erase (const_iterator, Const_iterator last);

Interval Assignment

All standard containers provide a member function of interval assignment:

void Container::assign (Inputiterator begin, Inputiterator end);
This function is used to assign a value to a container, replacing an existing value and allocating space as needed;
The difference with the copy () algorithm is that it does not need to allocate space beforehand and has higher performance;

Copy Code code as follows:

int myints[]={10,20,30,40,50,60,70};
Std::vector<int> Myvector;
Myvector.assign (MYINTS,MYINTS+7);

General interval algorithm

For_each interval Iteration

For_each: Traversal, performing an action on each element;
C++98 only supports the original for loop, and many languages (Java, Python, etc.) implement the Foreach interval iterative syntax, which makes C + + programmers envious for a long time;
In the age of no foreach interval iterations, we can use the For_each () algorithm instead:

Example: add 5 to each element:

Copy Code code as follows:

void MyFunction (int& i) {
i + 5;
}
Std::vector<int> Myvector;
Myvector.push_back (10);
Myvector.push_back (20);
Myvector.push_back (30);
For_each (Myvector.begin (), Myvector.end (), myfunction); 15 25 35

The addition of interval iterations in C++11 makes our reliance on for_each less and easier to use:

Copy Code code as follows:

for (auto &i:myvector)
{
i+=5;
}

Transform () interval iteration after the new value is saved as another place

After each element in the interval is executed, the modified value is written to the new interval.
It can be thought that this is the for_each () algorithm does not modify the original interval version;
Or the example in For_each:

Copy Code code as follows:

int addfunction (int i) {
return i+5;
}
void output (int i) {//Output function
Std::cout << ' << i;
}
Std::vector<int> Myvector;
Myvector.push_back (10);
Myvector.push_back (20);
Myvector.push_back (30);
Std::vector<int> Bvector;
Bvector.resize (Myvector.size ());
Transform (Myvector.begin (), Myvector.end (), Bvector.begin (), addfunction);
Output
For_each (Bvector.begin (), bvector.end (), output); Bvector:15 25 35

Copy () interval copy

Interval replication, which is generally used for data transfer between multiple containers;
This algorithm is used very commonly, in fact, many use the copy of the scene, can use the interval member function to replace (also recommended);

Example: Copy array to vector:

Copy Code code as follows:

int myints[]={10,20,30,40,50,60,70};
Std::vector<int> Myvector (7);
Std::copy (myints, Myints+7, Myvector.begin ());

Fill () interval fills

Using an element to repeat the filling interval;
This algorithm uses a low frequency;
For example: the first 4 elements of a vector are filled with 5:

Copy Code code as follows:

Std::vector<int> Myvector (8); myvector:0 0 0 0 0 0 0 0
Std::fill (Myvector.begin (), Myvector.begin () +4,5); Myvector:5 5 5 5 0 0 0 0

Replace () interval replacement

Traversal interval, for value substitution:
For example: Replace all 20 in the following interval with 99:

Copy Code code as follows:

int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
Std::vector<int> Myvector (myints, myints+8); 10 20 30 30 20 10 10 20
Std::replace (Myvector.begin (), Myvector.end (), 20, 99); 10 99 30 30 99 10 10 99

More complex versions (using an imitation function) replace_if
For example: Replace all greater than 20 in the following interval with 99:

Copy Code code as follows:

BOOL BigerThen20 (int i) {return i > 20;}
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
Std::vector<int> Myvector (myints, myints+8); 10 20 30 30 20 10 10 20
Std::replace_if (Myvector.begin (), Myvector.end (), BigerThen20, 99); 10 20 99 99 20 10 10 20

Because of the use of the imitation function, through the REPLACE_IF implementation, with For_each () is also very easy to achieve;

Remove () interval Delete

Deletes the specified element from the interval;

Copy Code code as follows:

int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
Std::vector<int> Myvector (myints, myints+8); 10 20 30 30 20 10 10 20
Std::remove (Myvector.begin (), Myvector.end (), 20); 10 30 30 10 10???

Note that remove does not actually delete the element, but only the element that needs to be deleted is dropped to the end, and a new tail iterator is returned.
For example, in the example above, when the remove is called, the value in the vector is generally//10 30 30 10 10 10 10 20
And if you want to really delete the element, you need to add the member function erase () to implement the delete "Remove-erase idiom":

Copy Code code as follows:

Myvector.erase (Std::remove (Myvector.begin (), Myvector.end (), Myvector.end ()); 10 30 30 10 10

Unique () interval to heavy

The same element is removed from the interval, and the algorithm does not actually delete the element, but instead moves the element to the end of the interval.
Use the "Unique-erase idiom":

Copy Code code as follows:

int myints[] = {10,20,20,20,30,30,20,20,10}; 10 20 20 20 30 30 20 20 10
Std::vector<int> Myvector (myints,myints+9);
Std::vector<int>::iterator it;
it = Std::unique (Myvector.begin (), Myvector.end ());  10 20 30 20 10?  ?  ? ?
Myvector.erase (It,myvector.end ());

The above mentioned is the entire content of this article, I hope you can enjoy.

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.