find, calculate, fill, fill in C + +_ n

The standard library defines some general algorithms (i.e. generics), which can be used on the first container. However, the container has its own algorithm, so the container's own algorithm is used, because the efficiency will be higher than the general algorithm.

Most algorithm header files are defined in the header file algorithm, and a group of numerical generic algorithms are defined in the header file numeric.

Generally, these algorithms do not directly operate on the container, but traverse an element range specified by two iterators.

find algorithm:

Find the specified element in the container vec. If it exists, the return result points to it. If it does not exist, the return result is vec.cend();

Upper Code:

vector<int> v1 (100);
default_random_engine eran(time(0));
uniform_int_distribution<int> u(1,100);//Randomly generate numbers between 1 and 100
for(auto i : v1 )
{
    v1.push_back(u(eran));//Put the randomly generated number into the container
}
 auto numbers =  find(v1.cbegin(),v1.cend(),1577);
    if (numbers !=  v1.cend())
        cout<<"eureka:"<<*numbers<<endl;
    else {
   cout<<"This element is not in the container!"<<endl;
    }

The algorithm will never change the size of the underlying container. However, it may change the value of the element saved in the container, or move the element in the container, but it will never directly add or delete the element.

Calculate algorithm:

It is defined in the header file numeric. It accepts three parameters. The first two indicate the range of ground elements to be summed, and the third parameter is the initial value of sum.

    //Sum the elements in container v1, and the initial value of the sum is 0 
    int sum = accumulate(v1.cbegin(),v1.cend(), 0);

However, the type of its third parameter determines which addition operator is used in the function and the type of return value.

 vector<string> v2(2);
    v2[0] = "This is";
    v2[1] = "my phone!";
  auto nums=  accumulate(v2.cbegin(),v2.cend(),string("666"));
   cout<<nums<<endl;

Since string defines the + operator (only those with the + operator can be used), the function of the above code is to connect all strings in the v2 container:

Note here:

vector <double> v5 ={3.14,3.12,3.4545};
auto nums = accumulate(v5.cbegin(),v5.cend(),0.0);

If the third parameter of the above function passes in 0, the output result will also be converted to an integer. If it is changed to 0.0, the output result will output a decimal. However, if you turn in 0.00 and 0.0000, it is the same as passing in 0.0, which will not affect the accuracy of its calculation result.

  An algorithm that operates on two sequences: equal  

If the elements in the two containers are equal, it returns true; otherwise, it returns false.

vector<int> v1={1,2,3,4,5};
vector<int> v2={1,2,3,4,5};
cout<<boolalpha;//Let the value of bool type be output as a string
 bool same =  equal(v1.cbegin(),v1.cend(),v2.cbegin());//The third parameter passes in the first address of another container
cout<<same<<endl;
cout<<noboolalpha;

Elements must be equal. If one element is not equal, false will also be returned. The number of elements in the two containers should be the same (the two sequences or containers should be the same length or size).

Because it uses iterators to complete operations, we can use it to compare elements in two different types of containers.

 vector<string> v3={"t", "i","\0"};
    list<char*> v4={"t", "i", "\0"};
    bool same1 =  equal(v3.cbegin(),v3.cend(),v4.cbegin());

We want to ensure that the algorithm does not access elements / spaces that do not exist in the container or sequence.

Algorithm fill:

It accepts three parameters. The first two represent the operation range and accept a value as the third parameter.

vector<int> v1={1,2,3,4,5};
fill(v1.begin(),v1.end(),10);//Replace all the elements in container v1 with 10

fill_n:

Its usage depends directly on the code:

vector<int> v1={1,2,3,4,5};
    fill_n(v1.begin(),3,100);

Its usage is: the first parameter is used as the starting position, the second parameter represents the n positions after the number starting from the first parameter position, and the third parameter is a value.

The above code means to replace the v1 container with 100 from the first element to the third element.

Note: the algorithm does not check for write operations.

We do not need to call fill_n on an empty container, nor can we use an algorithm similar to write operation on an empty container.

  vector<int> v2;
    fill_n(v2.begin(), 2, 1000);//Modify 2 elements that do not exist in the container

This is a serious error and the compiler will not report an error.

The algorithm for writing data to the destination iterator should ensure that the destination is large enough to accommodate the elements to be fixed.

Tags: C++ Back-end

Posted on Fri, 22 Oct 2021 18:37:15 -0400 by mcgruff