Summary of basic operations

Common functions

max与max_element:

max(a,b), returns the larger value between a and b
max_element(first, end), returns the iterator of the maximum value between first and end (where first and end are both iterators), also Can be used to find the index of the maximum value

For the array D[10], use res = max_element(D,D+6), which returns the iterator res where the maximum value between the arrays [0, 6) is, and (res-D) is the index value.

For vector vectors, begin() and end() are used as parameters. Examples are as follows:

vector<int>D;
vector<int>::iterator res = max_element(D.begin(),D.end())
dis = '最大值为:*res, 对应索引为 res - D.begin() 或 distance(D.begin(),res )'

In the same way, min and min_element correspond to the minimum value and the index.

accumulate sum function

This function is in the header file #include <numeric>, it is mainly used to accumulate the value in the container, such as int, string, etc., you can write a for loop less

For example, directly count the sum of all elements in vector<int> v: (0 in the third parameter indicates that the initial value of sum is 0, and it also represents the type of result )

int sum = accumulate(v.begin(), v.end(), 0);

For example, directly add all the elements in vector<string> v to string str one by one: (the third element indicates that the initial value of str is an empty string)

string str = accumulate(v.begin(), v.end(), "");

Guess you like

Origin blog.csdn.net/Yanpr919/article/details/113477061